【问题标题】:Previous tab selection remains active after selecting another tab选择另一个选项卡后,上一个选项卡选择保持活动状态
【发布时间】:2018-10-24 11:07:32
【问题描述】:

我有一个TabLayout,我想自定义如下:

  • 如果未选择选项卡项,则仅显示一个图标
  • 如果选择了选项卡项,则应显示图标和文本

我已经实现了一个OnTabSelectedListener,并在未选择选项卡时将文本设置为987654325 @ @。当我运行时,“未选择”选项卡上的文本被删除,但该选项卡仍处于活动状态,如下图所示:

代码:

tabs.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener{
            override fun onTabReselected(p0: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                // If I remove this the problem is resolved.
                tab?.text = null
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
               tab?.text = "Selected"
            }

        })

xml

<com.google.android.material.tabs.TabLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabInlineLabel="true"
    app:tabMode="scrollable"
    app:tabGravity="fill">

    <com.google.android.material.tabs.TabItem
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_attach_money_black_24dp"
        android:text="Selected" />
    <com.google.android.material.tabs.TabItem
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_attach_money_black_24dp" />
    <com.google.android.material.tabs.TabItem
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_attach_money_black_24dp"
        />
    <com.google.android.material.tabs.TabItem
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_attach_money_black_24dp" />
    <com.google.android.material.tabs.TabItem
        style="@style/Widget.MaterialComponents.TabLayout.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:icon="@drawable/ic_attach_money_black_24dp"
        />

</com.google.android.material.tabs.TabLayout>

我已尽我所能寻找解决方案,但无济于事。

【问题讨论】:

    标签: android material-design material-components


    【解决方案1】:

    我找到了解决问题的方法。我打电话给updateTabs()onTabSelected()。方法如下:

    private fun updateTabs(){
            for (i in 0 until tabs.tabCount){
                tabs.getTabAt(i)?.let {
                    it.icon = ActivityCompat.getDrawable(this,tabIcons[i])
                    if( it.isSelected){
                        it.text = tabTitles[i]
                    }else{
                        it.text = null
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      此问题已在内部修复,不久将推出新版本的材料设计库。

      问题的要点是在内部,在调用onTabUnselected() 之前,选定的选项卡没有更新。然后通过在该方法中设置选项卡的文本,它也会错误地更新旧的选定选项卡。

      【讨论】:

        猜你喜欢
        • 2020-11-22
        • 1970-01-01
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-15
        相关资源
        最近更新 更多