在viewpager上滑动时,选项卡select器在以前的选项卡上不起作用

我正在关注这个例子

我有一个问题,当我滑动ViewPager相应的fragment出现,但是当我从左到右或从右向左滑动并select上一个选项卡时 ,选项卡指示器出现在新选定的选项卡上,但相应fragment不出现在ViewPager 。 请帮我,我哪里错了?

它是支持lib 23.0.0的bug,但在23.0.1中解决了。 首先使用额外部分的SDKpipe理器更新您的支持库 。 并在应用程序gradle文件中写入以下行。

compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1'

供你参考

https://developer.android.com/topic/libraries/support-library/revisions.html并阅读Android支持中的devise支持库修订版本23.0.1部分

在你的活动中,查找Id后,你应该“configuration”你的ViewPager和TabLayout。 一些必要的function可能: ViewPager的addOnPageChangeListener ”和你的TabLayout的“ setOnTabSelectedListener ”像这样:

 public class MainActivity extends AppCompatActivity { private final int numOfPages = 4; //viewpager has 4 pages private final String[] pageTitle = {"Food", "Movie", "Shopping", "Travel"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); for (int i = 0; i < numOfPages; i++) { tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i])); } //set gravity for tab bar tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); final PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager(), numOfPages); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager)); } private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager pager) { return new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { pager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }; } 

我有一个关于这个教程文章,它运作良好! 没有像你的错误。 希望它有帮助: http : //www.devexchanges.info/2015/08/android-material-design-viewpager-with.html

这是我使用和工作正常。

适配器:

  public class SCFragmentPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragments = new ArrayList<>(); private final List<String> mFragmentTitles = new ArrayList<>(); private Context mContext; private FragmentManager mFragmentManager; public SCFragmentPagerAdapter(FragmentManager fm, Context context) { super(fm); this.mFragmentManager = fm; this.mContext = context; } @Override public int getCount() { return mFragments.size(); } // Return the correct Fragment based on index @Override public Fragment getItem(int position) { return mFragments.get(position); } public void addFragment(Fragment fragment, String title) { mFragments.add(fragment); mFragmentTitles.add(title); } @Override public CharSequence getPageTitle(int position) { // Return the tab title to SlidingTabLayout return mFragmentTitles.get(position); } public Fragment getActiveFragment(ViewPager container, int position) { String name = makeFragmentName(container.getId(), position); return mFragmentManager.findFragmentByTag(name); } private static String makeFragmentName(int viewId, int index) { return "android:switcher:" + viewId + ":" + index; } } 

活动:

 public class SCWelcomeActivity extends AppCompatActivity implements SCWelcomeFragment.OnFragmentInteractionListener, SCSyncFragment.OnFragmentInteractionListener, SCRegisterFragment.OnFragmentInteractionListener, SCConfirmationFragment.OnFragmentInteractionListener { private static final String TAG = SCWelcomeActivity.class.getSimpleName(); private ViewPager viewPager = null; private TabLayout tabLayout = null; private Toolbar toolbar = null; private SCFragmentPagerAdapter adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scwelcome); // Layout manager that allows the user to flip through the pages viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); // Initialize the Sliding Tab Layout tabLayout = (TabLayout) findViewById(R.id.tablayout); // Connect the viewPager with the sliding tab layout tabLayout.setupWithViewPager(viewPager); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override public void onFragmentInteraction(Uri uri) { } private void setupViewPager(ViewPager viewPager) { adapter = new SCFragmentPagerAdapter(getSupportFragmentManager(), SCWelcomeActivity.this); adapter.addFragment(SCWelcomeFragment.newInstance(), getString(R.string.title_tab1)); adapter.addFragment(SCSyncFragment.newInstance(), getString(R.string.title_tab2)); adapter.addFragment(SCRegisterFragment.newInstance(), getString(R.string.title_tab3)); adapter.addFragment(SCConfirmationFragment.newInstance(), getString(R.string.title_tab4)); viewPager.setAdapter(adapter); viewPager.setOffscreenPageLimit(4); } public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(Uri uri); } } 

片段例子:

  public class SCWelcomeFragment extends Fragment { private OnFragmentInteractionListener mListener; public static SCWelcomeFragment newInstance() { SCWelcomeFragment fragment = new SCWelcomeFragment(); return fragment; } public SCWelcomeFragment() { super(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_scwelcome, container, false); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(Uri uri); } } 

布局:

  <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/coordinator" android:layout_width="match_parent" android:layout_height="match_parent"> <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.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="130dp" android:fitsSystemWindows="true" android:gravity="bottom" 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" app:layout_collapseMode="pin" android:layout_gravity="center" android:theme="@style/ThemeOverlay.AppCompat.Dark" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 

请尝试像我一样实现适配器。 密切关注在我的Adapter中所有的实例都保存在

 private final List<Fragment> mFragments = new ArrayList<>(); 

在你的情况下,你总是返回一个新的实例。 所以这就是为什么setOffscreenPageLimit(4)。 到4,所以他们也留在记忆中。

上面的答案都没有为我工作截至2016年底, 错误仍然存​​在于devise支持库24 +我能够解决这个问题,通过包装在一个标签布局
协调员布局

我有更短的变种来解决这个错误(Android支持devise库v.23.0.0):

 ... //initialize views mViewPager.setAdapter(pagerAdapter); mTabLayout.setupWithViewPager(mViewPager); mViewPager.clearOnPageChangeListeners(); mViewPager.addOnPageChangeListener(new WorkaroundTabLayoutOnPageChangeListener(mTabLayout)); ... 

和类WorkaroundTabLayoutOnPageChangeListener:

 public class WorkaroundTabLayoutOnPageChangeListener extends TabLayout.TabLayoutOnPageChangeListener { private final WeakReference<TabLayout> mTabLayoutRef; public WorkaroundTabLayoutOnPageChangeListener(TabLayout tabLayout) { super(tabLayout); this.mTabLayoutRef = new WeakReference<>(tabLayout); } @Override public void onPageSelected(int position) { super.onPageSelected(position); final TabLayout tabLayout = mTabLayoutRef.get(); if (tabLayout != null) { final TabLayout.Tab tab = tabLayout.getTabAt(position); if (tab != null) { tab.select(); } } } } 

您正在使用支持库版本23.0.0。 https://code.google.com/p/android/issues/detail?id=183123存在与您的相似的问题&#x3002; 如果确实如此,请将支持库版本更新为23.0.1。 这个问题已被解决。