在viewpager上滑动时,选项卡选择器无法在上一个选项卡上单击

我正在关注这个例子..

我有一个问题,当我在ViewPagerViewPager相应的fragment但是当我从左向右或从右向左滑动并选择上一个Tab时 ,Tab指示符出现在新选择的Tab上,但相应的fragment不会出现在ViewPager 。 请帮助我,我哪里出错了?

这是支持lib 23.0.0中的错误,但它在23.0.1中得到解决。 首先使用extras部分中的SDK manager更新您的suppory库 。 并在app 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支持中的设计支持库更改 ,版本23.0.1部分

在您的活动中,在按ID查找视图后,您应该“配置”您的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) { } }; } 

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

这是我使用和工作正常。

适配器:

  public class SCFragmentPagerAdapter extends FragmentPagerAdapter { private final List mFragments = new ArrayList<>(); private final List 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); } } 

布局:

         

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

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

在您的情况下,您总是返回一个新实例。 这就是设置setOffscreenPageLimit(4)的原因。 因此它们也被保存在记忆中。

上述答案都不适合我。截至2016年底,该错误仍然存​​在于设计支持库24+中我能够通过将标签布局包装在一个内部来解决这个问题
协调员布局

我有更短的变种来修复这个bug(android support design lib 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 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 ,看起来与您的相似。 如果确实如此,请将支持库版本更新为23.0.1。 这个问题已被解决。