项目的UE图中页面顶部是个轮播图,下面是个Listview,轮播图要跟着listview一起滑动,因而产生了viewpager滑动冲突,如果外层使用ScollView又要
处理listview的滑动事件,还有其上拉加载下拉刷新,中和考虑后在HeadView中加入ViewPager轮播,但是加入后滑动冲突,viewpager左右滑动无效
成下拉事件.
于是重写ViewPager
public class MyViewPager extends ViewPager{ /** 触摸时按下的点 **/ PointF downP = new PointF(); /** 触摸时当前的点 **/ PointF curP = new PointF(); public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent arg0) { if(arg0.getAction() == MotionEvent.ACTION_DOWN){ //记录按下时候的坐标 //切记不可用 downP = curP ,这样在改变curP的时候,downP也会改变 downP.x = arg0.getX(); downP.y = arg0.getY(); //此句代码是为了通知他的父Listview现在进行的是本控件的操作,不要对我的操作进行干扰 getParent().requestDisallowInterceptTouchEvent(true); } if (arg0.getAction()==MotionEvent.ACTION_MOVE){ getParent().requestDisallowInterceptTouchEvent(true); L.e("滑动"+"move"); curP.x = arg0.getX(); curP.y = arg0.getY(); getParent().requestDisallowInterceptTouchEvent(true); if (downP.x-curP.x>0){ setCurrentItem(getCurrentItem() + 1); L.e("左滑动" + downP.x + "+" + curP.x); return true; } if (downP.x-curP.x<0){ setCurrentItem(getCurrentItem() -1); L.e("右滑动"+downP.x+"+"+curP.x); return true; } } if(arg0.getAction() == MotionEvent.ACTION_UP){ curP.x = arg0.getX(); curP.y = arg0.getY(); getParent().requestDisallowInterceptTouchEvent(true); if (downP.x-curP.x>0){ setCurrentItem(getCurrentItem() + 1); L.e("左滑动" + downP.x + "+" + curP.x); return true; } if (downP.x-curP.x<0){ setCurrentItem(getCurrentItem() -1); L.e("右滑动"+downP.x+"+"+curP.x); return true; } } return super.onInterceptTouchEvent(arg0); } }在 onInterceptTouchEvent中处理Viewpager的滑动事件,同时在代码中设置了Viewpager的onTouch事件
/** 触摸时按下的点 **/ PointF downP = new PointF(); /** 触摸时当前的点 **/ PointF curP = new PointF(); private void setView() { iv_back.setOnClickListener(this); mVp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent arg0) { isTouch=true; switch (arg0.getAction()) { case MotionEvent.ACTION_DOWN: downP.x = arg0.getX(); downP.y = arg0.getY(); break; case MotionEvent.ACTION_MOVE: if (downP.x-curP.x>0){ L.e("左滑动"+downP.x+"+"+curP.x); isTouch=false; } if (downP.x-curP.x<0){ L.e("右滑动"+downP.x+"+"+curP.x); isTouch=false; } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: curP.x = arg0.getX(); curP.y = arg0.getY(); if (downP.x-curP.x>0){ L.e("左滑动"+downP.x+"+"+curP.x); isTouch=false; } if (downP.x-curP.x<0){ L.e("右滑动"+downP.x+"+"+curP.x); isTouch=false; } break; } return false; } });
View headView=LayoutInflater.from(RaceActivity.this).inflate(R.layout.layout_listview_head_banner,null); AbsListView.LayoutParams params=new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,DisplayUtil.dp2px(150)); headView.setLayoutParams(params); mVp= (MyViewPager) headView.findViewById(R.id.vp); tv_title= (TextView) headView.findViewById(R.id.tv_title); mLinearLayout= (LinearLayout) headView.findViewById(R.id.ll_container); mWhiteView=headView.findViewById(R.id.v_guide_white_point); lv_race.addHeaderView(headView);