2016-08-17 88 views
1

我實現了一個TabLayout有三個選項卡。當我滑動頁面一切正常時,它也適用於我只使用標籤點擊進行導航。但是,當我滑動頁面並單擊任何先前選擇的選項卡時,只有選項卡指示符更改爲新選項卡,但文本高亮顯示和頁面不會更改時出現問題。Android TabLayout選項卡面臨的問題點擊

我檢查了TabLayout的點擊偵聽器,它沒有在上述情況下執行。

更多信息:

minSdk:16 
targetSdk:24 

我試圖支持庫24.0.0和24.1.1

TabSelectedListener。

 tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      tabPages.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

PagerAdapter

public class TabPageAdapter extends FragmentStatePagerAdapter { 
public TabPageAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int position) { 
    Fragment f1 = new Fragment1(); 
    Fragment f2 = new Fragment2(); 
    Fragment f3 = new Fragment3(); 

    switch (position) { 
     case 0: 
      return f1; 
     case 1: 
      return f2; 
     case 2: 
      return f3; 
    } 
    return null; 
} 


@Override 
public int getCount() { 
    return 3; 
} 

}

+0

@SangramHaladkar添加tabSelectedListener。 –

+0

發佈你的'PagerAdapter'代碼? – Bryan

+0

@Bryan有代碼 –

回答

1

可以避開OnTabSelectedListener完全,TabLayout有這樣的convenience method

tabs.setupWithViewPager(tabPages); 
0
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setText("Comparison")); 
     tabLayout.addTab(tabLayout.newTab().setText("Details")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
     final PagerAdapter adapter = new PagerAdapter 
       (getSupportFragmentManager(), tabLayout.getTabCount()); 
     viewPager.setAdapter(adapter); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 

插入該代碼中的活性。 添加下面的代碼在活動XML文件

<android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorM3signin" 
     app:tabSelectedTextColor="@color/com_facebook_button_background_color_pressed" 
     app:tabTextColor="@color/com_facebook_button_background_color_pressed" 
     android:elevation="6dp" 
     android:inputType="textCapCharacters" 
     app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" 
     app:tabIndicatorColor="@color/com_facebook_button_background_color_pressed" 
     app:tabIndicatorHeight="5dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     android:layout_below="@+id/toolbar" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/tab_layout"/> 

檢查這個PagerAdapeter類。

public class PagerAdapter extends FragmentStatePagerAdapter { 
    int mNumOfTabs; 

    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this.mNumOfTabs = NumOfTabs; 
    } 

    @Override 
    public Fragment getItem(int position) { 

     switch (position) { 
      case 0: 
       TabFragmentOne tab1 = new TabFragmentOne(); 
       return tab1; 
      case 1: 
       TabFragmentTwo tab2 = new TabFragmentTwo(); 
       return tab2; 

      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     return mNumOfTabs; 
    } 
} 
+0

我已經這樣做了。 –

+0

http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/請檢查此鏈接 –