简体   繁体   English

如何在android中按索引更改tablayout指示器颜色?

[英]how to change tablayout indicator color by index in android?

i have a view pager2 in my activity and tablayout attached to this viewpager2 as below我在我的活动中有一个视图 pager2 和 tablayout 附加到这个 viewpager2 如下

 <com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    app:tabIndicatorColor="@color/colorPrimary"
    app:tabMode="fixed"
    android:layout_height="2dp" />
<androidx.viewpager2.widget.ViewPager2
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/vpager"/>

i have attached the tablayout to viewpager as below我已将 tablayout 附加到 viewpager,如下所示

   new TabLayoutMediator(tabLayout, vpager,
            new TabLayoutMediator.TabConfigurationStrategy() {
                @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {

                        }

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

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

                        }
                    });     
                }
            }).attach();

my target is to keep the indicator color selected in previous tab seeing by the user, so if the user go to next tab, the previous one must be coloring as selected, like the images below我的目标是让用户看到在上一个选项卡中选择的指示器颜色,因此如果用户转到下一个选项卡,则前一个选项卡必须按所选颜色进行着色,如下图所示

在此处输入图片说明

在此处输入图片说明

the images show the first indicator is selected and keep it selected when go to next fragment, so it can be like a progress bar图像显示第一个指示器被选中并在转到下一个片段时保持选中状态,因此它可以像一个进度条

i choose to set the indicator color programmatically by index , how?我选择通过索引以编程方式设置指示器颜色,如何?

The TabLayout indicator color functionality is limited the current selected tab of the TabLayout ; TabLayout指示器颜色功能仅限于TabLayout的当前选定选项卡; so it affects only the current tab;所以它只影响当前选项卡; not any other tab.不是任何其他标签。

Instead, you can use a ProgressBar for that and set the progress whenever the ViewPager changes the page:相反,您可以为此使用ProgressBar并在ViewPager更改页面时设置进度:

Layout布局

<ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/intro_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Behavior行为

ProgressBar progressBar = findViewById(R.id.progress_bar);

progressBar.max = pageCount // Change pageCount to the number of pages in your ViewPager2

mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        progressBar.setProgress(position + 1);
    }

    @Override
    public void onPageSelected(int position) {
        progressBar.setProgress(position + 1);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM