I know that this thread if fairly old, but i wanted to share my solution to this problem nevertheless, because it might be helpful for some.
It works quite nice, even for different drawables depending on the selected state.
First of i did define two array inside the SlidingTabLayout
class (this could easily be outsourced to another class):
private int[] imageResId = {
R.drawable.multi_random,
R.drawable.single_random,
R.drawable.known_person,
};
private int[] imageResSelected = {
R.drawable.multi_random_selected,
R.drawable.single_random_selected,
R.drawable.known_person_selected
};
private int mOldPosition = 0;
Now we alter the populateTabStrip()
method inside the SlidingTabLayout
:
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
ImageView tabImageView = null;
if (mTabViewLayoutId != 0) { // HAS TO BE SET FOR THE MOMENT!
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabImageView = (ImageView) tabView.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabImageView == null && ImageView.class.isInstance(tabView)) {
tabImageView = (ImageView) tabView;
}
int resourceId;
if (i == mViewPager.getCurrentItem()) {
resourceId = imageResSelected[i];
mOldPosition = i;
} else {
resourceId = imageResId[i];
}
tabImageView.setImageResource(resourceId);
tabView.setOnClickListener(tabClickListener);
mTabStrip.addView(tabView);
}
To update the image according to which tab is selected we add some lines to the onPageSelected
method
// This Method is called once the transition is finished
// Change the drawable of the old one..
ImageView view = (ImageView) mTabStrip.getChildAt(mOldPosition);
view.setImageResource(imageResId[mOldPosition]);
// And now change it of the current one
view = (ImageView) mTabStrip.getChildAt(position);
view.setImageResource(imageResSelected[position]);
mOldPosition = position;
Finally we need to add a custom Tab View:
mSlidingTabLayout.setCustomTabView(R.layout.custom_tab, 0);
Like this one
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_weight="1"
android:src="@drawable/multi_random"
/>
This solution is far from perfect, but works for my needs. Maybe i could help someone with this.