简体   繁体   English

使用FragmentStatePagerAdapter在ViewPager的两侧动态添加片段

[英]Adding Fragments Dynamically on both side of the ViewPager using FragmentStatePagerAdapter

In one of my apps, I need to add Fragments on both sides of the ViewPager. 在我的一个应用程序中,我需要在ViewPager的两侧添加Fragments。 First of all, I will get a constant of 5 feeds, and my ViewPager will show feed at index 2 ie my current displayed Fragment will contain data present at index 2. So overall my ViewPager will show center of 5 feeds at start and that i have achieved by just setting the ViewPager current Item as 2 like this 首先,我将获得5个Feed的常量,我的ViewPager将在索引2处显示feed,即我当前显示的Fragment将包含索引2处的数据。因此,总体上我的ViewPager将在开始时显示5个Feed的中心,并且我只需将ViewPager当前Item设置为2即可实现

viewPager.setCurrentItem(2);

Now user can swipe both sides, when he will swipe left from center position, I will look for next feed ie fetch from server and add feed at zero index of my ViewPager like this 现在用户可以滑动两边,当他从中心位置向左滑动时,我会查找下一个Feed,即从服务器获取并在我的ViewPager的零索引处添加Feed,就像这样

feedsList.add(0, modelClassObject); // feedsList will contain all feeds and has been used by adapter to show data in fragments.
adapter.notifyDataSetChanged();

and when i swipe right from center position, i will add feed at the last simply like this 当我从中心位置向右滑动时,我会在最后添加Feed,就像这样

feedsList.add(modelClassObject);
adapter.notifyDataSetChanged();

Now the problem is if i only add feeds at the right ie at the end of the feedsList, everything works fine, but problem comes when i add feeds at zero index. 现在的问题是,如果我只在右侧添加Feed,即在feedsList的末尾,一切正常,但是当我在零索引处添加Feed时出现问题。 My adapter is not showing that new feed that has been added to zero position instead it is repeating one of the existing feed and that too on the right side but not on the left. 我的适配器没有显示已添加到零位置的新Feed,而是重复现有Feed中的一个,而右侧但不是左侧。 I Have tried everything, but nothing is going right way. 我已经尝试了一切,但没有什么是正确的。 Here is my adapter code. 这是我的适配器代码。

private class HorizontalPagerAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int arg0) {
        return FeedUserProfileFragment.newInstance(feedsList.get(arg0),
                arg0);
    }

    @Override
    public int getCount() {
        return feedsList.size();
    }

}

I have also used this 我也用过这个

@Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

but no results.. :( 但没有结果...... :(

So in severe need, If anyone had done that earlier and faced the same issue, please let me know what i am doing wrong. 所以在严重的需要中,如果有人早点这样做并遇到同样的问题,请让我知道我做错了什么。 I only need to add fragment at zero index of my ViewPager. 我只需要在我的ViewPager的零索引处添加片段。

I faced a similar problem before, and my solution was : 我以前遇到过类似的问题,我的解决方案是:

  1. at first the list is declared in the adapter itself, so that when creating an instance of that adapter I can have it's own list then. 首先,列表在适配器本身中声明,因此在创建该适配器的实例时,我可以拥有它自己的列表。
  2. modified the method getItem(int arg0) in the adapter class so that it returns a specific item from the list depending on that item position. 修改了适配器类中的方法getItem(int arg0) ,以便它根据该项位置从列表中返回特定项。
  3. when creating a new fragment, use instantiate method to create it, and after that add it to your fragments. 在创建新片段时,使用instantiate方法创建它,然后将其添加到片段中。

So, the complete solution would be : 所以,完整的解决方案是:

adapter class: 适配器类:

private class HorizontalPagerAdapter extends FragmentStatePagerAdapter {

public List<Fragment> feedsList;

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

    @Override
    public Fragment getItem(int position) {
        return feedsList.get(position);
    }

    @Override
    public int getCount() {
        return feedsList.size();
    }

}

and when creating the adapter: 并在创建适配器时:

public static YourPageAdapter adapter_obj; // make sure it's static object
adapter_obj =  new YourPageAdapter(getSupportFragmentManager());
adapter_obj.feedsList = new ArrayList<Fragment>();
// then add the list of fixed fragments to it(the 5 in the beginning)
adapter_obj.feedsList = fragments_list; // an ArrayList contains the 5 fragments

and when want to create a new fragment: 当想要创建一个新片段时:

adapter_obj.feedsList.add(0, Fragment.instantiate(mContext, ViewPager_Container_Class.class.getName(), page));
adapter_obj.notifyDataSetChanged();

FragmentStatePagerAdapter can't handle it right when you add a new fragment in front. 在前面添加新片段时,FragmentStatePagerAdapter无法正确处理它。

The workaround is this: 解决方法是这样的:

  1. Add a new fragment at the end. 最后添加一个新片段。
  2. Call notifyDataSetChanged(); 调用notifyDataSetChanged();
  3. Bring the fragment to front. 将片段放在前面。
  4. Call notifyDataSetChanged(); 调用notifyDataSetChanged();

BTW, getItemPosition() should return correct positions all along: BTW,getItemPosition()应该一直返回正确的位置:

public int getItemPosition(Object object)
{
    return feedsList.indexOf( object );
}

So, with your code, it should be: 所以,使用您的代码,它应该是:

newFrag = Fragment.instantiate(...);
feedsList.add( newFrag );
adapter_obj.notifyDataSetChanged();

feedsList.remove( feedsList.size() - 1 );
feedsList.add( 0, newFrag );
adapter_obj.notifyDataSetChanged();

I guess the implementation of FragmentStatePagerAdapter doesn't expect both adding a new fragment and changing position happen at the same time. 我想FragmentStatePagerAdapter的实现并不期望同时添加新片段和更改位置。

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

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