ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Icon tabs not show #203

Open porpuera opened 7 years ago

porpuera commented 7 years ago

hi thanks for nice library.

I'll try to custom tab view with icon and text. But the icon not show. this my code ,,,

ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

    SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);

    adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), FragmentPagerItems.with(this).
            add("Request", Fragment1.class).
            add("History", Fragment2.class).
            add("Message", Fragment3.class).
            create());
    viewPager.setAdapter(adapter);

    viewPagerTab.setViewPager(viewPager);
    final LayoutInflater inflater = LayoutInflater.from(this);
    final Resources res = getResources();

    viewPagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
        @Override
        public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
            View itemView = inflater.inflate(R.layout.custom_tab_icon_and_text, container, false);
            TextView text = (TextView) itemView.findViewById(R.id.custom_tab_text);
            text.setText(adapter.getPageTitle(position));
            TintableImageView icon = (TintableImageView) itemView.findViewById(R.id.custom_tab_icon);
            switch (position) {
                case 0:
                    icon.setImageDrawable(res.getDrawable(R.drawable.ic_flash_on_white_24dp));
                    break;
                case 1:
                    icon.setImageDrawable(res.getDrawable(R.drawable.ic_flash_on_white_24dp));
                    break;
                case 2:
                    icon.setImageDrawable(res.getDrawable(R.drawable.ic_flash_on_white_24dp));
                    break;
                default:
                    throw new IllegalStateException("Invalid position: " + position);
            }

            return itemView;
        }
    });

this my xml custom tab <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/selectableItemBackground"

<android.support.v4.widget.Space android:id="@+id/center_anchor" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true" />

<com.package.view.TintableImageView android:id="@+id/custom_tab_icon" android:layout_width="24dp" android:layout_height="24dp" android:layout_above="@id/center_anchor" android:layout_centerHorizontal="true" android:scaleType="center" android:src="@drawable/custom_icon" app:tint="@color/grey_700" /> <TextView android:id="@+id/custom_tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/center_anchor" android:layout_centerHorizontal="true" android:layout_marginTop="2dp" android:maxLines="1" android:textColor="@color/grey_700" android:textSize="14sp" />

how to solve this? thanks before

Kraahkan commented 7 years ago

I also have this issue. Can't figure out how to set icons. Not sure if possible but would be very nice to have a method to quickly change the icons with.

burnix commented 7 years ago

Put setCustomViewTab before setViewPager. It worked with me.

akashrao1991 commented 7 years ago

Thanx burnix!!!!! :D