What do you mean by unselected.
Can you share an image of what you are trying to achieve and where you are at now..
I wouldn't recommend your approach. It does a lot of things which are not needed to solve your problem. I suggest making use of the icon and text from the TabLayout class and just set your icons(drawables with states) and text.
Or, even a custom layout if required, but making use of the text1 and icon from the TabLayout. Do you have any issues doing this?
Update..
Try the below:
IconTextTabLayout.java:
package example.customtabs;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import sample.customtabs.example.com.customtabs.R;
public class IconTextTabLayout extends TabLayout {
public IconTextTabLayout(Context context) {
super(context);
}
public IconTextTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IconTextTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
this.removeAllTabs();
int i = 0;
for (int count = adapter.getCount(); i < count; ++i) {
this.addTab(this.newTab().setCustomView(R.layout.custom_tab)
.setIcon(YourIcons[i])
.setText(adapter.getPageTitle(i)));
}
}
}
custom_tab.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">
<ImageView
android:id="@android:id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
In Your activity xml use IconTextTabLayout. Like :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<example.customtabs.IconTextTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white"/>
</LinearLayout>
also, set view pager to it in your activity like :
IconTextTabLayout tabLayout = (IconTextTabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
Override getPageTitle in your viewpager adapter to provide title.
@Override
public CharSequence getPageTitle(int position) {
return TITLES[i];
}
Additionally, if you want you can get icons too from the adapter by writing a method that returns drawables, just like the getPageTitle
method. That's optional. Just a matter of design.
--
YourIcons[]
= This is an array of drawables.
In your case - xml files, with selected and non-selected images...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…