我尝试使用custom_tab_layout来实现badgeView,但是当选中该选项卡时,选项卡中的图标不会再次更改。在使用custom_tablayout之前,选中选项卡时,选项卡中的图标会从灰色变为白色。有没有我错过的东西,这是java文件
public class MainActivity extends BaseActivity implements IMainView, TabLayout.OnTabSelectedListener {
private TabLayout tlMain;
private ViewPager vpMain;
private PagerAdapter pagerAdapter;
private MainPresenter mainPresenter;
private int[] tabIcons = {R.mipmap.ic_feeds_shade, R.mipmap.ic_notifications_shade
,R.mipmap.ic_sms_shade, R.mipmap.ic_event_note_shade
,R.mipmap.ic_account_circle_copy};
private int[] tabIconsWhite = {R.mipmap.ic_feeds_putih, R.mipmap.ic_notifications_putih
,R.mipmap.ic_sms_putih, R.mipmap.ic_event_note_putih
,R.mipmap.ic_account_circle_putih};
private String[] title = {"Laporan","Pemberitahuan","Perpesanan"
,"Agenda","Profile"};
private int[] unread = {0,7,0,0,0};
private Toolbar toolbar;
private ImageView iconTabItem;
private TextView badgeView;
public static void start(Context context){
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("isLogin", true);
context.startActivity(intent);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
mainPresenter = new MainPresenter(this);
mainPresenter.onCreate(context);
}
private void goToLogin(){
LoginActivity.start(context);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
vpMain.setCurrentItem(tab.getPosition());
tab.setIcon(tabIconsWhite[tab.getPosition()]);
switch (tab.getPosition()){
case 0:
vpMain.setCurrentItem(0);
toolbar.setTitle(title[0]);
break;
case 1:
vpMain.setCurrentItem(1);
toolbar.setTitle(title[1]);
break;
case 2:
vpMain.setCurrentItem(2);
toolbar.setTitle(title[2]);
break;
case 3:
vpMain.setCurrentItem(3);
toolbar.setTitle(title[3]);
break;
case 4:
vpMain.setCurrentItem(4);
toolbar.setTitle(title[4]);
break;
default:
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.setIcon(tabIcons[tab.getPosition()]);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public void initView() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(title[0]);
tlMain = (TabLayout) findViewById(R.id.tl_main);
tlMain.setupWithViewPager(vpMain);
tlMain.addTab(tlMain.newTab());
tlMain.addTab(tlMain.newTab());
tlMain.addTab(tlMain.newTab());
tlMain.addTab(tlMain.newTab());
tlMain.addTab(tlMain.newTab());
tlMain.setTabGravity(TabLayout.GRAVITY_FILL);
tlMain.setOnTabSelectedListener(this);
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tlMain.getTabCount());
vpMain = (ViewPager) findViewById(R.id.vp_main);
vpMain.setAdapter(pagerAdapter);
vpMain.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlMain));
if(getIntent().getExtras() != null){
Bundle bundle = getIntent().getExtras();
if(!bundle.getBoolean("isLogin")){
goToLogin();
}
}else{
goToLogin();
}
try {
for (int i = 0; i < 6; i++){
tlMain.getTabAt(i).setCustomView(prepareTabView(i));
}
} catch (Exception e){
e.printStackTrace();
}
}
public View prepareTabView(int pos){
View view = getLayoutInflater().inflate(R.layout.custom_tablayout, null);
iconTabItem = (ImageView)view.findViewById(R.id.icon_tabitem);
badgeView = (TextView)view.findViewById(R.id.tab_count);
iconTabItem.setImageResource(tabIcons[pos]);
if (unread[pos]>0){
badgeView.setVisibility(View.VISIBLE);
badgeView.setText(String.format(Locale.getDefault(), "%d", unread[pos]));
} else
badgeView.setVisibility(View.GONE);
return view;
} }
这是view_main.xml
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/dc_toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tl_main"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="@color/colorPrimaryDark"
app:tabIndicatorColor="@color/dc_white"
app:tabIndicatorHeight="4dp"
app:tabSelectedTextColor="@color/dc_yellow"
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="@color/dc_white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
这适用于custom_tablayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon_tabitem"
android:src="@mipmap/ic_notifications_shade" />
<TextView
android:id="@+id/tab_count"
android:layout_width="12dp"
android:layout_height="12dp"
android:background="@drawable/badge_item_count"
android:gravity="center"
android:text="99"
android:padding="1dp"
android:textColor="@color/dc_white"
android:textSize="6sp"
android:textStyle="bold"
android:layout_alignEnd="@+id/icon_tabitem"/>
</RelativeLayout>
</RelativeLayout>
谢谢你的帮助:)
答案 0 :(得分:2)
您可以使用以下代码段。
private void updateTabIcon() {
mTabLayout.setupWithViewPager("your_pager");
for (int pos = 0; pos < "your_total_tab_count"; pos++) {
TabLayout.Tab tab = mTabLayout.getTabAt(pos);
assert tab != null;
tab.setCustomView(R.layout."your_custom_tab_view");
ImageView tabIcon = (ImageView) tab.getCustomView().findViewById(R.id.icon);
TextView badgeView = (TextView) tab.getCustomView().findViewById(R.id.text1);
//Use switch or if to set particular icon to particular tab
badgeView.setText("your_badge_Text");
tabIcon.setImageResource("your_selector_icon_drawable");
}
}
注意:使用选择器选择和取消选择drawable。
Ex:icon_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/"selected_drawable" android:state_selected="true" />
<item android:drawable="@drawable/"unselected_drawable" android:state_selected="false" />
</selector>
答案 1 :(得分:0)
您可以尝试创建一个功能,将所有图标更改回默认值,然后在onTabSelected中指定所选图标之前调用它:)