简体   繁体   English

如何在ViewPager中重用布局/片段

[英]How to reuse layouts/fragments in ViewPager

Hi there I am wondering what is the correct way if I would like to add different instance of a Fragment which uses the same Layout with different content in a ViewPager . 嗨,我想知道如果我想添加一个Fragment不同实例的正确方法,该实例在ViewPager使用具有相同内容的相同Layout

So for a better understanding I create my Fragments with an createInstance() method and pass an id with which it gets content for a list from a database . 因此,为了更好地理解,我使用createInstance()方法创建了Fragments并传递了一个ID ,通过该ID它可以从数据库中获取列表的内容。 I add them to my FragmentPagerAdapter . 我将它们添加到FragmentPagerAdapter

So what I get is the first Fragment added multiple times without getting its content for the actual id . 所以我得到的是第一个Fragment多次添加而没有获取其实际ID的内容。

How could I force the ViewPager to treat the Fragments individually ? 如何强制ViewPager分别处理Fragments

*Edit: *编辑:

Okay I totally failed within my createInstance() method which was more a getInstance() method and returned the same instance every time... 好的,我在我的createInstance()方法中完全失败了,它更多是一个getInstance()方法,并且每次都返回相同的实例...

You need to set adapter.notifyDataSetChanged . 您需要设置adapter.notifyDataSetChanged

Refer below 参考下面

class MyFragmentAdapter extends FragmentPagerAdapter{

       public MyFragmentAdapter(FragmentManager fm) {
           super(fm);
       }

       @Override
       public Fragment getItem(int position) {
           return new PagerFragment(arrayListUri.get(position),position);
       }

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

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

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