Home » Android » How to click on tabs of TabLayout

How to click on tabs of TabLayout

Posted by: admin November 1, 2017 Leave a comment

Questions:

In my application I have TabLayout and ViewPager in activity and into this viewPager I have other TabLayout and ViewPager.
Such as this image :
enter image description here

I write below codes, and I can show second viewPager into First viewPager, when click on Tabs b , c on second viewPager, and when click again on Tab a not selected tab a and not show me data into tab a.

My codes:

public class MovieFragment extends Fragment {

    @BindView(R.id.celebrityMovieFrag_tabLayout)
    TabLayoutPlus celebrityMovieFrag_tabLayout;
    @BindView(R.id.celebrityMovieFrag_viewPager)
    DisableSwipeViewPager celebrityMovieFrag_viewPager;
    private Context context;
    private CelebrityActivity celebrityActivity;
    private int actorCount, directorCount, writerCount;
    private BadgedTabCustomView badgedTabCustomView;
    private boolean isCollapsing;
    private LinearLayout tabStrip;

    public MovieFragment() {
        // Required empty public constructor
    }

    public static MovieFragment getInstance() {
        MovieFragment sf = new MovieFragment();
        return sf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_movie, container, false);

        //Initialize
        ButterKnife.bind(this, view);
        context = getActivity();
        celebrityActivity = (CelebrityActivity) getActivity();
        isCollapsing = celebrityActivity.getCollapsing();

        //Get counts
        actorCount = celebrityActivity.movieActorCount();
        directorCount = celebrityActivity.movieDirectorCount();
        writerCount = celebrityActivity.movieWriterCount();

        //Set actor tab
        celebrityMovieFrag_tabLayout.addTab(celebrityMovieFrag_tabLayout.newTabPlus());
        badgedTabCustomView = celebrityMovieFrag_tabLayout.getTabCustomViewAt(0);
        if (badgedTabCustomView != null) {
            badgedTabCustomView.setTabText("Tab a");
            if (actorCount > 0) {
                badgedTabCustomView.setTabCount(actorCount);
            } else {
                badgedTabCustomView.setTextColor(ContextCompat.getColor(context, R.color.unSelectTab));
            }
        }
        //Set director tab
        celebrityMovieFrag_tabLayout.addTab(celebrityMovieFrag_tabLayout.newTabPlus());
        badgedTabCustomView = celebrityMovieFrag_tabLayout.getTabCustomViewAt(1);
        if (badgedTabCustomView != null) {
            badgedTabCustomView.setTabText("Tab b");
            if (directorCount > 0) {
                badgedTabCustomView.setTabCount(directorCount);
            } else {
                badgedTabCustomView.setTextColor(ContextCompat.getColor(context, R.color.unSelectTab));
            }
        }
        //Set writer tab
        celebrityMovieFrag_tabLayout.addTab(celebrityMovieFrag_tabLayout.newTabPlus());
        badgedTabCustomView = celebrityMovieFrag_tabLayout.getTabCustomViewAt(2);
        if (badgedTabCustomView != null) {
            badgedTabCustomView.setTabText("Tab c");
            if (writerCount > 0) {
                badgedTabCustomView.setTabCount(writerCount);
            } else {
                badgedTabCustomView.setTextColor(ContextCompat.getColor(context, R.color.unSelectTab));
            }
        }
        //Disable tab
        LinearLayout tabStrip = ((LinearLayout) celebrityMovieFrag_tabLayout.getChildAt(0));
        tabStrip.getChildAt(0).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        //Tabs gravity
        celebrityMovieFrag_tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        //Setup viewpager
        setupViewPager(celebrityMovieFrag_viewPager);
        //Disable Swipe viewPager
        celebrityMovieFrag_viewPager.disableScroll(true);
        //Set Limited viewPager
        celebrityMovieFrag_viewPager.setOffscreenPageLimit(3);
        //Adding onTabSelectedListener to swipe views
        celebrityMovieFrag_tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                celebrityMovieFrag_viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });

        return view;
    }

    private void setupViewPager(ViewPager viewPager) {

        ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());

        adapter.addFrag(MovieActorFrag.getInstance());
        adapter.addFrag(MovieDirectorFrag.getInstance());
        adapter.addFrag(MovieWriterFrag.getInstance());

        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();

        ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

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

        public void addFrag(Fragment fragment) {
            mFragmentList.add(fragment);
        }
    }
}

How can I fix it? Please help me

Answers:

remove this code from your codes :

//Disable tab
LinearLayout tabStrip = ((LinearLayout) celebrityMovieFrag_tabLayout.getChildAt(0));
tabStrip.getChildAt(0).setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});