2017-04-26 100 views
0

我嘗試使用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> 

感謝你的幫助: )

回答

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"); 

     } 
    } 

注意:使用選擇器繪製已選中和未選中。

例: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> 
+0

謝謝你,它更容易使用的選擇,hahahaaa – MNFS

0

您可以嘗試創建一個函數,將所有圖標更改回默認值,然後調用它在分配onTabSelected中的選定圖標之前: