简体   繁体   English

Android ViewPager片段-维护状态

[英]Android ViewPager Fragment - maintaining state

I have a ViewPager backed by a FragmentStatePagerAdapter. 我有一个由FragmentStatePagerAdapter支持的ViewPager。 The app is a single Activity app, with fragments for each "page". 该应用程序是单个Activity应用程序,每个“页面”都有片段。 Currently I'm using the ViewPager to display Categories of Products, and each Category will contain a ListView (or RecyclerView) that lists all the products. 当前,我正在使用ViewPager显示产品类别,每个类别将包含一个列出所有产品的ListView(或RecyclerView)。 Once a user taps on a product, the viewpager fragment is replaced by the ProductDetailsFragment. 用户点击产品后,viewpager片段将被ProductDetailsFragment替换。 Which is all fine and dandy but when the user presses back, it reloads all the Category fragments. 一切都很好,但当用户按下时,它会重新加载所有Category片段。 Is there anyway to avoid that? 无论如何要避免这种情况? The issue is that if a user had to paginate over 3 pages they'll want to maintain their scroll state. 问题是,如果用户必须分页超过3页,他们将希望保持其滚动状态。

You should use this 你应该用这个

  fragmentTransaction.add(android.R.id.content, fragment, Fragment.class.getSimpleName());
  fragmentTransaction.addToBackStack(Fragment.class.getSimpleName()).commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM