0
我想在gradle中使用compile'com.android.support:design:25.3.1'在tablayout中設置自定義選項卡。Android - 帶自定義視圖選項卡的tablayout - 無法應用可繪製選擇器
我想使用選擇器,以便在單擊某個選項卡時根據其狀態進行更改。
所以我嘗試了失敗以下內容:
這是我的活動XML:
<ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
style="@style/MyCustomTabLayout"
/>
這裏是我使用的tabLayout風格:
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">@android:color/transparent</item>
</style>
它隱藏了下劃線。
這裏是我的家標籤我的選擇之一:
這裏是自定義視圖,我爲每個標籤:
<ImageView
android:id="@+id/iv_imgview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:src="@drawable/home_tab_selector"
/>
<TextView
android:id="@+id/tv_tab_name"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="HOME"
/>
和這裏是創建tablayout的代碼的一部分:
for (int i = 0; i < NUM_OF_TABS; i++)
tabLayout.addTab(tabLayout.newTab());
adapter = new LandingPagerAdapter(getApplicationContext(),getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(this);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(adapter.getTabView(i));
}
tabLayout.getTabAt(0).select();
最後這裏負責應用選擇器和自定義視圖中getTabView方法:
public class LandingPagerAdapter extends FragmentStatePagerAdapter {
private final Context context;
//integer to count number of tabs
int tabCount;
private Fragment mCurrentFragment;
private String[] tabTitles = new String[]{"Home", "tab2"};
// configure tab icons
int[] imageTabResId = {
R.drawable.home_tab_selector,
R.drawable.tab_two_selector,;
//Constructor to the class
public LandingPagerAdapter(Context context, FragmentManager fm, int tabCount) {
super(fm);
this.context = context;
this.tabCount = tabCount;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabHomeContainerFragment tab1 = new TabHomeContainerFragment();
return tab1;
case 1:
TabTwoContainerFragment tab2 = new TabTwoContainerFragment();; //Todo: these should all be containers
return tab2;
default:
return null;
}
}
//Overriden method getCount to get the number of tabs
@Override
public int getCount() {
return tabCount;
}
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
if (getCurrentFragment() != object) {
mCurrentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
public View getTabView(int position) {
// Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
View v = LayoutInflater.from(context).inflate(R.layout.custom_landingpagetab, null);
TextView tv = (TextView) v.findViewById(R.id.tv_tab_name);
tv.setText(tabTitles[position]);
ImageView img = (ImageView) v.findViewById(R.id.iv_imgview);
img.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),imageTabResId[position]));
return v;
}
}
通知在getTabView我將圖像資源設置爲類字段中數組的選擇器。 但是我一直在測試的api上的設備上沒有任何反應。