tabMode=“scrollable” not working

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

I am using the TabLayout from Android Design support library. I am using material design theme. Below is the code

activity_scrollable_Tab.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">   <android.support.design.widget.AppBarLayout  <android.support.design.widget.TabLayout             android:id="@+id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             app:tabMode="scrollable"/>      </android.support.design.widget.AppBarLayout>     <android.support.v4.view.ViewPager     android:id="@+id/viewpager"     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior" />  </android.support.design.widget.CoordinatorLayout> 

scrollableTabActivity.java

public class ScrollableTabsActivity extends AppCompatActivity {      private Toolbar toolbar;     private TabLayout tabLayout;     private ViewPager viewPager;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_scrollable_tabs);          toolbar = (Toolbar) findViewById(R.id.toolbar);         setSupportActionBar(toolbar);         getSupportActionBar().setDisplayHomeAsUpEnabled(true);          viewPager = (ViewPager) findViewById(R.id.viewpager);         setupViewPager(viewPager);          tabLayout = (TabLayout) findViewById(R.id.tabs);         tabLayout.setupWithViewPager(viewPager);     }      private void setupViewPager(ViewPager viewPager) {         ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());         adapter.addFrag(new OneFragment(), "ONE");         adapter.addFrag(new TwoFragment(), "TWO");         .....         adapter.addFrag(new TenFragment(), "TEN");         viewPager.setAdapter(adapter);     }      class ViewPagerAdapter extends FragmentPagerAdapter {         private final List<Fragment> mFragmentList = new ArrayList<>();         private final List<String> mFragmentTitleList = new ArrayList<>();          public ViewPagerAdapter(FragmentManager manager) {             super(manager);         }          @Override         public Fragment getItem(int position) {             return mFragmentList.get(position);         }          @Override         public int getCount() {             return mFragmentList.size();         }          public void addFrag(Fragment fragment, String title) {             mFragmentList.add(fragment);             mFragmentTitleList.add(title);         }          @Override         public CharSequence getPageTitle(int position) {             return mFragmentTitleList.get(position);         }     } } 

Tab scrolling is working in Lollipop. But the same code is not working on Kitkat and Jelly bean devices. On Kitkat and Jelly bean device I face two problem,

  1. When I scroll the Tab, Tab is scrolling along with page navigation in Jellybean and kitkat devices, but in Lollipop Tab alone gets scroll and it is working file. For eg: if 1,2,3..10 are the Tab and if I scroll the Tab, it scroll and for each scroll the Layout gets navigated. If I scroll, Tab gets navigate to 2nd Tab and then 3rd and so on.

  2. Up navigation is not working in Jellybean and Kitkat devices. If I swipe the Toolbar, Tab scrolling is happening with page navigation (above-mentioned issue).

Can anyone tell me what could be the problem and in which portion I need to check and sort it. I can send APK file if provide me the email for a better understanding of the problem.

I am new to android, need some help to fix this.

回答1:

Maybe you should set that tab mode programmatically? Try to call setTabMode method on TabLayaut instance tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

I don't know why it doesn't work for you, but I tested these modes few weeks ago, and for me scrollable mode worked on all Android versions.

Also check if you use the latest version of Design Support Library, maybe you use some previous bugged version?



回答2:

To make Tab scrollable work in kitkat and Jelly bean devices you can refer this tutorial

http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html

Google docs is also there for this https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

It is well tested and working for me on all devices



回答3:

My case is some thing different which is not solved by most of the answers i tried.

The answer is add xmlns:app="http://schemas.android.com/apk/res-auto" in your android.support.design.widget.CoordinatorLayout.

I am posting the complete code here,

<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.support.design.widget.AppBarLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">          <android.support.v7.widget.Toolbar             android:id="@+id/toolbar"             android:layout_width="match_parent"             android:layout_height="?attr/actionBarSize"             android:background="?attr/colorPrimary"             app:layout_scrollFlags="scroll|enterAlways"             app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />          <android.support.design.widget.TabLayout             android:id="@+id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             app:tabMode="scrollable"/>     </android.support.design.widget.AppBarLayout>      <android.support.v4.view.ViewPager         android:id="@+id/viewpager"         android:layout_width="match_parent"         android:layout_height="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> 

After adding xmlns:app="http://schemas.android.com/apk/res-auto" in my TabLayout xml file, Tab scroll function working fine in Jelly bean, Kitkat and lollipop.



回答4:

Please try below xml for your TabLayout.

<android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" app:tabMode="scrollable" /> 


回答5:

Your FragmentPagerAdapter looks a little weird, why are you adding your fragments to a List? Have you tried adding

adapter.notifyDataSetChanged(); 

before

viewPager.setAdapter(adapter); 

on setupViewPager?



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!