TabLayout+ViewPager取消滑动,留点击,消除滑动出现的左右移动bug

今天需求要求,实现一个tab,只能点击有不能够滑动viewpager,如需滑动,则是弹出删除LinearLayout布局,实现点击删除item。

实现效果图:

TabLayout+ViewPager取消滑动,留点击,消除滑动出现的左右移动bug_第1张图片
取消滑动事件的viewpager


网上大部分都是自定义ViewPager,没错我们这里也需要自定义ViewPager,我先贴出所有代码:


/**

* Created by caishaohua on 2016/8/12 16:04

* Email: [email protected]

*/

public class BanViewPager extends ViewPager {

private boolean isCanScroll = true;

public BanViewPager(Context context) {

      super(context);

}

public BanViewPager(Context context, AttributeSet attrs) {

        super(context, attrs);

}

public void setNoScroll(boolean noScroll) {

        this.isCanScroll = noScroll;

}

@Override

public void scrollTo(int x, int y) {

      super.scrollTo(x, y);

}

@Override

public boolean onTouchEvent(MotionEvent arg0) {

    if (isCanScroll){

            return false;

    }else{

           return super.onTouchEvent(arg0);

   }

}

@Override

public boolean onInterceptTouchEvent(MotionEvent arg0) {

      if (isCanScroll){

              return false;

      }else{

            return super.onInterceptTouchEvent(arg0);

      }

    }

}

自定义的ViewPager和其他网上的基本大同小异!起最后关键的东西要出现了,请看xml布局文件

布局文件:

        android:id="@+id/tablayout"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       app:tabIndicatorColor="@color/indigo"

       app:tabTextColor="@color/time_item_gray"

       app:tabSelectedTextColor="@color/indigo">

       android:id="@+id/viewpage"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

       android:isScrollContainer="true">

一般的这样自定义就可以解决TabLayout下面的ViewPager的滑动,但是细心的同学,依然发现有滑动出现的左右移动bug!!!并没有全部固定ViewPager,看了xml布局文件,细心的同学会发现,我在自定义的Viewpager控件中加了

android:isScrollContainer="true"

表示可以滚动的,然后我们就可以解决滑块出现的滑动出现左右移动bug!

你可能感兴趣的:(TabLayout+ViewPager取消滑动,留点击,消除滑动出现的左右移动bug)