Android:双层嵌套ViewPager并添加单击事件
仿网易新闻:
实现原理:
上面的导航栏是一个自定义的ViewGroup(类似ActionBar)
中间内容层是ViewPager套fragment实现左右切换滑动
fragment里面的布局是一个ListView 附带下拉刷新和下拉加载效果
HeadView是一个ViewPager
双层ViewPager如果处理子ViewPager的滑动冲突问题和点击事件
通过自定义子ViewPager的方法:
public class TeacherViewPager extends ViewPager implements OnGestureListener { private GestureDetector mDetector; public TeacherViewPager(Context context, AttributeSet attrs) { super(context, attrs); GestureDetector detector = new GestureDetector(context, this); mDetector = detector; } public GestureDetector getGestureDetector() { return mDetector; } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { LogUtil.info("json", "onSingleTapUp"); if(listener != null) { listener.setOnSimpleClickListenr(getCurrentItem()); } return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } private onSimpleClickListener listener; public interface onSimpleClickListener { void setOnSimpleClickListenr(int position); } public void setOnSimpleClickListener(onSimpleClickListener listener) { this.listener = listener; } }
onTouchEvent处理事件:
advPager.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { advPager.getGestureDetector().onTouchEvent(event); // TODO Auto-generated method stub final float x = event.getRawX(); final float y = event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: LogUtil.verbose("json", "ACTION_DOWN"); xDistance = yDistance = 0f; mLastMotionX = x; mLastMotionY = y; case MotionEvent.ACTION_MOVE: LogUtil.verbose("json", "ACTION_MOVE, x: " + x + ", y: " + y); final float xDiff = Math.abs(x - mLastMotionX); final float yDiff = Math.abs(y - mLastMotionY); xDistance += xDiff; yDistance += yDiff; float dx = xDistance - yDistance; LogUtil.info("json", "ACTION_MOVE dx= " + dx + ",xDistance: " + xDistance + ", yDistance: " + yDistance); if (xDistance > yDistance || Math.abs(xDistance - yDistance) < 0.00001f) { LogUtil.warn("json", "ACTION_MOVE interceptor."); mIsBeingDragged = true; mLastMotionX = x; mLastMotionY = y; ((ViewParent) v.getParent()).requestDisallowInterceptTouchEvent(true); } else { mIsBeingDragged = false; ((ViewParent) v.getParent()).requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: LogUtil.verbose("json", "ACTION_UP"); break; case MotionEvent.ACTION_CANCEL: LogUtil.verbose("json", "ACTION_CANCEL"); LogUtil.verbose("json", "mIsBeingDragged=" + mIsBeingDragged + " xDistance= " + xDistance + " y=" + yDistance); if(mIsBeingDragged) { ((ViewParent) v.getParent()).requestDisallowInterceptTouchEvent(false); } break; default: break; } return false; } });
源码下载链接:https://files.cnblogs.com/gongcb/%E5%8E%8B%E7%BC%A9%E5%8C%85.zip