android自定义控件之禁止ListView滚动
一、禁止滚动,高度不可设
import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; /** * 禁止滚动的ListView * Created by Administrator on 2015/10/21 0021. */ public class NoScrollListview extends ListView{ public NoScrollListview(Context context, AttributeSet attrs) { super(context, attrs); } /** * 设置不滚动 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
二、禁止滚动,高度可设
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ListView; public class ScrollDisabledListView extends ListView { private int mPosition; public ScrollDisabledListView(Context context) { super(context); } public ScrollDisabledListView(Context context, AttributeSet attrs) { super(context, attrs); } public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK; if (actionMasked == MotionEvent.ACTION_DOWN) { // 记录手指按下时的位置 mPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); return super.dispatchTouchEvent(ev); } if (actionMasked == MotionEvent.ACTION_MOVE) { // 最关键的地方,忽略MOVE 事件 // ListView onTouch获取不到MOVE事件所以不会发生滚动处理 return true; } // 手指抬起时 if (actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_CANCEL) { // 手指按下与抬起都在同一个视图内,交给父控件处理,这是一个点击事件 if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) { super.dispatchTouchEvent(ev); } else { // 如果手指已经移出按下时的Item,说明是滚动行为,清理Item pressed状态 setPressed(false); invalidate(); return true; } } return super.dispatchTouchEvent(ev); } }