Disable click on tablayout, when setup with viewpager

╄→гoц情女王★ 提交于 2021-01-28 06:16:30

问题


I have a ViewPager (3 items) with some icons. When on first item, the icon is selected, there is possible to swipe to the next item. Etc. After selected second icon, there is possibility to go to the third item of viewpager. I have a page indicator (dots), which is connected with ViewPager by setupWithViewPager. The problem is that I want to set clickable on tablayout to false, and unlock steps if the icon on viewPager is selected. Now all "dots" are clickable and I can go to the all items of ViewPager.

I can manipulate with TabLayout only in OnPageSelected function... How can I change it?

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

    findViews(rootView);

    setOnClickListeners();
    buildTypeFragmentPagerAdapter = new BuildTypeFragmentPagerAdapter(getChildFragmentManager(), state);


    buildTypeViewPager.addOnPageChangeListener(new PageListenerAdapter() {
        public void onPageSelected(int position) {
            state.moveToPage(position);
            refreshViewState(false);
        }
    });

    dotsView.setupWithViewPager(buildTypeViewPager);


    return rootView;
}

I can set clickable to false for dotsView and it does not work...


回答1:


try this for api >24:

tabLayout.clearOnTabSelectedListeners();

or for <24:

tabLayout.setupWithViewPager(viewPager);

LinearLayout tabStrip = ((LinearLayout)tabLayout.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;
        }
    });
}



回答2:


You can extend the TabLayout class and override the method 'setupWithViewPager'. You need to call the super method to populate the tabs. Then this will iterate with the tabs and disable clicks:

@Override
public void setupWithViewPager(@Nullable ViewPager viewPager) {
    super.setupWithViewPager(viewPager);

    for (int i = 0; i < getTabCount(); i++) {
        Tab tab = getTabAt(i);
        if (tab != null) {
            tab.view.setClickable(false);
        }
    }
}


来源:https://stackoverflow.com/questions/46403828/disable-click-on-tablayout-when-setup-with-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!