How to scroll tablayout programmatically - Android

前端 未结 13 2134
终归单人心
终归单人心 2021-02-07 00:31

I have created 30 scrollable tabs using tablayout.

So first three tabs are visible on screen and rest of them are invisible which can be scroll using swipe gesture.

相关标签:
13条回答
  • 2021-02-07 00:50

    Are you calling tab.select() before the TabLayout and its children have actually been measured and drawn? If so, your TabLayout won't animate to the selection with tab.select() (or Kayvan N's suggestion of scrollTo()). Using a Handler will probably work, but that's not an ideal solution.

    Provided the layout hasn't been laid out yet, a ViewTreeObserver will allow you to move to your selected tab after the layout process is finished.

    private void scrollToTabAfterLayout(final int tabIndex) {
        if (getView() != null) {
            final ViewTreeObserver observer = mTabLayout.getViewTreeObserver();
    
            if (observer.isAlive()) {
                observer.dispatchOnGlobalLayout(); // In case a previous call is waiting when this call is made
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            observer.removeOnGlobalLayoutListener(this);
                        } else {
                            //noinspection deprecation
                            observer.removeGlobalOnLayoutListener(this);
                        }
    
                        mTabLayout.getTabAt(tabIndex).select();
    
                    }
                });
            }
        }
    }
    

    Please comment if you have any suggestions.

    0 讨论(0)
  • 2021-02-07 00:53
    new Handler().postDelayed(
        new Runnable() {
            @Override public void run() {
                mTabLayout.getTabAt(TAB_NUMBER).select();
            }
        }, 100);
    
    0 讨论(0)
  • 2021-02-07 00:58

    I found the solution.

    First I had found the width of tablayout and scroll it's x position to width and than called the select() method of last tab.

    And it works fine.

    Below is the code.

    mTabLayout.setScrollX(mTabLayout.getWidth());
    mTabLayout.getTabAt(lastTabIndex).select();
    

    Updated:

    If above is not working you can use the below code as well, it is also working fine.

    new Handler().postDelayed(
            new Runnable() {
                @Override public void run() {
                    mTabLayout.getTabAt(TAB_NUMBER).select();
                }
            }, 100);
    
    0 讨论(0)
  • 2021-02-07 00:58

    I wonder if this is answer will be relevant since its coming very late. i actually achieved it in C# using Xamarin.

    tabs.GetChildAt(0).Selected = true;
     viewPager.SetCurrentItem(0, true);
    
    0 讨论(0)
  • 2021-02-07 00:59

    viewpager.setItem(position) should also set the position of the tab

    0 讨论(0)
  • If your TabLayout is used in conjunction with a ViewPager, which is common, simply add the following in the onCreate() method in your Activity:

    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout);
    

    That some of your tabs are not being shown indicates the tabMode attribute is set to app:tabMode="scrollable".

    0 讨论(0)
提交回复
热议问题