Android scrollview嵌套问题

来源:互联网 发布:windows xp qq影音下载 编辑:程序博客网 时间:2024/05/21 22:27

        1、listview显示一行

public class NoScrollListView extends ListView {   private OnTouchBlankPositionListener mTouchBlankPosListener;   public NoScrollListView(Context context) {      super(context);   }   public NoScrollListView(Context context, AttributeSet attrs) {      super(context, attrs);   }   @Override   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {      int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,            MeasureSpec.AT_MOST);      super.onMeasure(widthMeasureSpec, expandSpec);   }   public interface OnTouchBlankPositionListener {      /**       *        * @return 是否要终止事件的路由       */      boolean onTouchBlankPosition();   }   public void setOnTouchBlankPositionListener(         OnTouchBlankPositionListener listener) {      mTouchBlankPosListener = listener;   }   @Override   public boolean onTouchEvent(MotionEvent event) {      if (mTouchBlankPosListener != null) {         if (!isEnabled()) {            // A disabled view that is clickable still consumes the touch            // events, it just doesn't respond to them.            return isClickable() || isLongClickable();         }         if (event.getActionMasked() == MotionEvent.ACTION_UP) {            final int motionPosition = pointToPosition((int) event.getX(),                  (int) event.getY());            if (motionPosition == -1) {               return mTouchBlankPosListener.onTouchBlankPosition();            }         }      }      return super.onTouchEvent(event);   }}

      2、

能够兼容ViewPager的ScrollView
 
public class MyScrollView extends ScrollView {    // 滑动距离及坐标    private float xDistance, yDistance, xLast, yLast;    public MyScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                xDistance = yDistance = 0f;                xLast = ev.getX();                yLast = ev.getY();                break;            case MotionEvent.ACTION_MOVE:                final float curX = ev.getX();                final float curY = ev.getY();                xDistance += Math.abs(curX - xLast);                yDistance += Math.abs(curY - yLast);                xLast = curX;                yLast = curY;                if(xDistance > yDistance){                    return false;                }        }        return super.onInterceptTouchEvent(ev);    } }
3、gridview只显示一行

public class NoScrollGridView extends GridView {   private OnTouchBlankPositionListener mTouchBlankPosListener;   public NoScrollGridView(Context context) {      super(context);   }   public NoScrollGridView(Context context, AttributeSet attrs) {      super(context, attrs);   }   @Override   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {      int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,            MeasureSpec.AT_MOST);      super.onMeasure(widthMeasureSpec, expandSpec);   }   public interface OnTouchBlankPositionListener {      /**       *        * @return 是否要终止事件的路由       */      boolean onTouchBlankPosition();   }   public void setOnTouchBlankPositionListener(         OnTouchBlankPositionListener listener) {      mTouchBlankPosListener = listener;   }   @Override   public boolean onTouchEvent(MotionEvent event) {      if (mTouchBlankPosListener != null) {         if (!isEnabled()) {            // A disabled view that is clickable still consumes the touch            // events, it just doesn't respond to them.            return isClickable() || isLongClickable();         }         if (event.getActionMasked() == MotionEvent.ACTION_UP) {            final int motionPosition = pointToPosition((int) event.getX(),                  (int) event.getY());            if (motionPosition == -1) {               return mTouchBlankPosListener.onTouchBlankPosition();            }         }      }      return super.onTouchEvent(event);   }}
4、打开页面listview抢占焦点,只显示到listview起始位置

解决办法:

在根layout上设置以下属性

android:focusable="true"
android:focusableInTouchMode="true"
0 0
原创粉丝点击