选中后更改Tab的customView

时间:2018-11-15 03:13:16

标签: android android-custom-view android-tablayout

enter image description here我想更改“价格”标签下的图片。 第一次按下时,将按价格升序显示产品,再次按下同一选项卡时,将按价格降序显示产品。 我无法更改标签的图像以显示订单。 我将customView用于我的第三个标签,因为它包含与文本对齐的图像,

下面是我的代码:

price_tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/price"
        android:textAllCaps="true"
        android:textColor="@android:color/black" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/lth" />

</LinearLayout>

ProductActivity.java

tablayout.addTab(tablayout.newTab().setText(getText(R.string.popular)), 0);
        tablayout.addTab(tablayout.newTab().setText(getText(R.string.new_tag)), 1);
        tablayout.addTab(tablayout.newTab().setText(getText(R.string.price)), 2);
        View mCustomView = LayoutInflater.from(ProductActivity.this).inflate(R.layout.price_tab, null);
        mImageViewCustom = (ImageView) mCustomView.findViewById(R.id.icon);
        tablayout.getTabAt(2).setCustomView(mCustomView);
        tablayout.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {

                if (tab.getText().equals(getString(R.string.new_tag))) {
                    sort = 0;
                    page = 1;
                    isLastPage = false;
                    productList.clear();
                    productListAdapter.notifyDataSetChanged();
                    getProducts();


                } else if (tab.getText().equals(getString(R.string.price))) {
                    if (sort != 2) {
                        sort = 2;

                        mImageViewCustom.setImageResource(R.drawable.lth);
                    } else {
                        sort = 3;
                        mImageViewCustom.setImageResource(R.drawable.htl);
                    }
                    page = 1;
                    isLastPage = false;
                    productList.clear();
                    productListAdapter.notifyDataSetChanged();
                    getProducts();

                } else if (tab.getText().equals(getString(R.string.popular))) {
                    sort = 1;


                    page = 1;
                    isLastPage = false;
                    productList.clear();
                    productListAdapter.notifyDataSetChanged();
                    getProducts();
                }

            }

            @Override
            public void onTabUnselected(Tab tab) {

            }

            @Override
            public void onTabReselected(Tab tab) {

                if (tab.getText().equals("Price")) {
                    if (sort == 2) {
                        sort = 3;
                    } else
                        sort = 2;
                    page = 1;
                    isLastPage = false;
                    productList.clear();
                    productListAdapter.notifyDataSetChanged();

                    getProducts();
                }
            }


        });

        getProducts();

enter image description here

3 个答案:

答案 0 :(得分:2)

在您的onTabReselcted()中,尝试以下代码:

@Override
        public void onTabReselected(TabLayout.Tab tab) {
            if (tab.getText().toString().equals("Price")) {
                if (sort == 2) {
                    sort = 3;
                } else
                    sort = 2;
                page = 1;
                isLastPage = false;
                productList.clear();
                productListAdapter.notifyDataSetChanged();

                //change tab icon
                View view = tab.getCustomView();
                if (view != null) {
                   ImageView icon = view.findViewById(R.id.icon);
                   icon.setImageResource(R.drawable.your_image_resource);
                }      

                getProducts();
            }
        }

根据您的sort逻辑,您可以将图标从增加更改为减少,反之亦然

答案 1 :(得分:1)

使用此功能,您可以根据标签的当前位置自定义所需的任何标签。

private TabLayout tabs;
tabs = view.findViewById(R.id.tabs);

 tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
            {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {


                    if (tab.getPosition()==your tab position){
                        tabs.setTabTextColors(Color.parseColor("#fdb827"), Color.parseColor("#ffffff"));
                        tabs.setSelectedTabIndicatorColor(Color.parseColor("#ffffff"));

                    }else {
                        tabs.setTabTextColors(Color.parseColor("#fdb827"), Color.parseColor("#F57F17"));
                        tabs.setSelectedTabIndicatorColor(Color.parseColor("#fdb827"));
                    }



                }

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

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

                }
            });

希望这会有所帮助。

答案 2 :(得分:0)

将tab.getText()更改为tab.getText()。toString()