Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to disable swiping between tablayout control by button click

i have 2 fragment in my tab layout, i manage to disable the swiping function between tab layout by button click. How to implement the disable function in my onclick button. thanks for advance

MainActivity

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{


private TabLayout tabLayout;

private ViewPager viewPager;
int ans =2;

 ------FCViewPager disable;------

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
    Tab1 tab1 = new Tab1();
    Tab2 tab2 = new Tab2();


    adapter.addFragment(tab1, "1");
    adapter.addFragment(tab2, "2");

    viewPager = (ViewPager) findViewById(R.id.pager);


    viewPager.setAdapter(adapter);


    tabLayout.setOnTabSelectedListener(this);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setupWithViewPager(viewPager);

}

----- public void testing( View view) {

        disable.setEnableSwipe(false);

}---------
@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}


@Override
public void onTabReselected(TabLayout.Tab tab) {

}
public class BasePagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    public BasePagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


    public void removeFragment(int position) {
        mFragmentTitleList.remove(position);
        mFragmentList.remove(position);
        notifyDataSetChanged();

    }
    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}

Tab1

 public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View h = inflater.inflate(R.layout.tab1, container, false);

    return  h;
}

}

Tab2

public class Tab2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View h = inflater.inflate(R.layout.tab2, container, false);
        return h;
    }
like image 743
AnthonyTang Avatar asked Feb 13 '17 03:02

AnthonyTang


People also ask

How do I turn off swipe on Android?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.

How do I disable Tablayout click?

the method has slightly different on the time of its invocation, you just have to setup your tabitem to disable its click after all viewpager fragment already added.

How do I disable swiping in specific direction in viewpager2?

Extend the viewpager class and override the functions onInterceptTouchEvent and onTouchEvent . Then identify the direction of the swipe and return false if you don't want to swipe.


2 Answers

You can disable swipe feature in your ViewPager. Create custom ViewPager as follow.

public class FCViewPager extends ViewPager {

    private boolean enableSwipe;

    public FCViewPager(Context context) {
        super(context);
        init();
    }

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

    private void init() {
        enableSwipe = true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return enableSwipe && super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return enableSwipe && super.onTouchEvent(event);

    }

    public void setEnableSwipe(boolean enableSwipe) {
        this.enableSwipe = enableSwipe;
    }
}

In your MainActivity

//R.id.pager has to be FCViewPager not default ViewPager
viewPager = (FCViewPager) findViewById(R.id.pager); 

Your button onClick function

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setEnableSwipe(false);
            }
        });
like image 83
Tixeon Avatar answered Oct 13 '22 06:10

Tixeon


User scroll disable viewpager for disabling and enabling scrolling by default and programmatically

public class ScrollDisabledViewpager extends ViewPager {
    private boolean isPagingEnabled = false;

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

    public ScrollDisabledViewpager(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;
    }
}

Button onClick event you can set this true/false to enable/ disable scrolling

viewPager.setPagingEnabled(true/ false);

like image 32
Hemanth S Avatar answered Oct 13 '22 07:10

Hemanth S