2017年8月25日 星期五

How to disable scrolling on ViewPager

Reference:

https://stackoverflow.com/questions/7814017/is-it-possible-to-disable-scrolling-on-a-viewpager

Code snippet:

1. Create a new CustomViewPager class
public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

2. Replace the <com.android.support.V4.ViewPager> tag with <your_package_name.CustomViewPager > tag in layout.xml

3. Call setPagingEnabled(true) when you want to disable Scrolling and call setPagingEnabled(false) to enable it.

How to disable click event on TabLayout

Reference:

https://stackoverflow.com/questions/37809538/how-can-i-disable-click-on-tablayout-in-android

Code snippet:

mTabLayout = (TabLayout) findViewById(R.id.tab_layout);

LinearLayout tabStrip = (LinearLayout) mTabLayout.getChildAt(0);
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
        @Override        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}