【问题标题】:Change image on Tab in TabLayout when Selected选择时更改 TabLayout 中选项卡上的图像
【发布时间】:2015-09-16 11:23:13
【问题描述】:

我正在使用 Design TabLayout,

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabBackground="@color/ColorPrimary"
            app:tabIndicatorColor="@color/orange"
            app:tabIndicatorHeight="3dp"
            />

我已将自定义视图添加到选项卡

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null">

    <ImageView
        android:id="@+id/imgTab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tab_image"
        android:layout_centerHorizontal="true"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

    <TextView
        android:id="@+id/tabTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imgTab"
        android:text="Home"
        android:background="@null"
        android:layout_centerHorizontal="true"
        android:textColor="@color/selector_tab_text"/>
</RelativeLayout>

tab.setCustomView(view);

我想在选择选项卡时更改自定义视图中的图像。 尝试在 imageview 上使用 Selector 不起作用。 我无法在运行时将视图分配给 Tab,它只包含 setCustomView 方法。 如何实现?

【问题讨论】:

    标签: android android-tabs android-tablelayout


    【解决方案1】:

    setOnTabSelectedListener 添加到tabLayout 对象

    我叫我的navigation

    navigation.setOnTabSelectedListener(
                new TabLayout.ViewPagerOnTabSelectedListener(mainView) {
    
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        super.onTabSelected(tab);
    
                        // 1. get the custom View you've added
                        View tabView = tab.getCustomView();
    
                        // get inflated children Views the icon and the label by their id
                        TextView tab_label = (TextView) tabView.findViewById(R.id.nav_label);
                        ImageView tab_icon = (ImageView) tabView.findViewById(R.id.nav_icon);
    
                        // change the label color, by getting the color resource value
                        tab_label.setTextColor(getResources().getColor(R.color.active_color));
                        // change the image Resource
                        // i defined all icons in an array ordered in order of tabs appearances
                        // call tab.getPosition() to get active tab index.
                        tab_icon.setImageResource(navIconsActive[tab.getPosition()]);
                    }
    
                    // do as the above the opposite way to reset tab when state is changed 
                    // as it not the active one any more
                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                        super.onTabUnselected(tab);
                        View tabView = tab.getCustomView();
                        TextView tab_label = (TextView) tabView.findViewById(R.id.nav_label);
                        ImageView tab_icon = (ImageView) tabView.findViewById(R.id.nav_icon);
    
                        // back to the black color
                        tab_label.setTextColor(getResources().getColor(R.color.dark_grey));
                        // and the icon resouce to the old black image 
                        // also via array that holds the icon resources in order 
                        // and get the one of this tab's position
                        tab_icon.setImageResource(navIcons[tab.getPosition()]);
                    }
    
                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                        super.onTabReselected(tab);
                    }
                }
        );
    

    这将完美地完成这项工作,你可以像我自己一样把自己垫在后面:D

    【讨论】:

    • @HimanshuRawat .. 是的,它会起作用的,here is the steps from the beginning
    • setOnTabSelectedListener 已被弃用,它不能与 addOnTabSelectedListener 一起使用
    • 另外你应该选择(设置新颜色)第一个标签并在创建TabLayout后取消选择其他标签。
    【解决方案2】:

    由于setOnTabSelectedListener已被弃用,并且没有新答案,我将更新一个更新的答案

    应将 addOnTabSelectedListener 与 OnTabSelectedListener 一起使用

       tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabReselected(p0: TabLayout.Tab?) {
                }
    
                override fun onTabUnselected(p0: TabLayout.Tab?) {
                    (p0?.customView?.findViewById(R.id.tab) as TextView).setTextColor(
                        context!!.resources.getColor(
                            R.color.v3_tint_tab_unselected_color
                        )
                    )
                }
    
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    (tab?.customView?.findViewById(R.id.tab) as TextView).setTextColor(
                        context!!.resources.getColor(
                            R.color.v3_tint_tab_selected_color
                        )
         (tab?.customView?.findViewById(R.id.tab_img) as ImageView).setImageResource(R.drawable.custom_image)
    
        //for getting the posiiton if needed you can use
          tab.position or tab.getPosition()
                    )
                }
            })
    

    自定义选项卡 xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/parent_layout">
    
    <ImageView android:layout_width="@dimen/space_45"
               android:layout_height="@dimen/space_45"
               android:id="@+id/tab_img"
               android:gravity="center"
               android:layout_gravity="center"
               android:src="@drawable/hill_icon_blue"
    />
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/tab"
              android:gravity="center"
              android:layout_gravity="center"
              android:text=""
              android:textColor="@color/white"
              android:textSize="@dimen/text_size_14"
              android:fontFamily="@font/roboto_medium"/>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:
          ImageView imageView= (ImageView) findViewById(R.id.your_image_view);
      
          // On tab selected listener
          tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  if (tab.getPosition() == YOUR_TAB_NUMBER)
                      imageView.setBackground(getDrawable(R.drawable.ic_second_background));
              }
      
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
                  if (tab.getPosition() == YOUR_TAB_NUMBER)
                      imageView.setBackground(getDrawable(R.drawable.ic_first_background));
              }
      
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
      
              }
          });
      

      【讨论】: