简体   繁体   English

重新启动ViewPager + FragmentStatePagerAdapter到项目0

[英]Restarting ViewPager + FragmentStatePagerAdapter to item 0

Using SDK 19, min 13, and support.v4.app Fragments. 使用SDK 19(至少13)和support.v4.app片段。

I have searched SO and found similiar threads which should help, but everything I have tried has not addressed my issue yet. 我搜索了SO,发现类似的线程应该有所帮助,但是我尝试过的所有操作都尚未解决我的问题。 Furthermore, many people seem to have the issue of the ViewPager restarting when they don't want it to, whereas my problem seems to be the opposite. 此外,许多人似乎在不希望ViewPager重新启动时遇到问题,而我的问题似乎恰恰相反。

These posts do not seem to have helped me yet: 这些帖子似乎还没有帮助我:
PagerAdapter start position PagerAdapter的起始位置
ViewPager PagerAdapter not updating the View ViewPager PagerAdapter不更新视图
How to force ViewPager to re-instantiate its items 如何强制ViewPager重新实例化其项目

Here is my setup: 这是我的设置:
A (support.v4) Fragment in memory, which contains a ViewPager, inflated from a layout file, thus not added programmatically. 内存中的一个(support.v4)片段,其中包含一个ViewPager,它是从布局文件中填充的,因此未通过编程方式添加。 The Fragment itself is not recreated, but in memory for the life of the app; Fragment本身不会重新创建,而是在应用程序的生命周期内保留在内存中。 it is being attached/detached to the root FragmentActivity's FragmentManager. 它被附加/分离到根FragmentActivity的FragmentManager。

When the user visits this Fragment sometime during the app's lifetime, it is attached and the onCreateView is called, where I findViewById the ViewPager and assign a new FragmentStatePagerAdapter to it. 当用户在应用程序的生存期内访问此Fragment时,它会被附加并调用onCreateView,在其中我找到ViewPageryViewById并为其分配一个新的FragmentStatePagerAdapter。

Here is my problem: 这是我的问题:
The first time the user visits this pager, everything works fine. 用户第一次访问此寻呼机时,一切正常。 The next time they visit it, I expect it to "start over" from page 0, meaning that I expect to not be able to scroll left immediately, but must start scrolling right. 下次他们访问它时,我希望它从页面0开始“重新开始”,这意味着我希望不能立即向左滚动,但必须开始向右滚动。 I also expect the getItem() of the FragmentStatePagerAdapter to start back at position 0. I basically want it to function the same as the first time the user visited it. 我还希望FragmentStatePagerAdapter的getItem()从位置0开始。我基本上希望它的功能与用户第一次访问它时相同。 However, this is not the case. 然而,这种情况并非如此。

I have tried several things, but it always seems to start at the previous index of page I left it at. 我已经尝试了几件事,但是它似乎总是从我留在它上面的页面的上一个索引开始。 So if the first time I scrolled 5 pages over, and then left the Fragment and returned later, the ViewPager starts at that same page index, meaning I can scroll 5 pages left. 因此,如果我第一次滚动5页,然后离开Fragment并稍后返回,则ViewPager从该页面索引开始,这意味着我可以向左滚动5页。 I don't want this. 我不要这个

It may have something to do with the ViewPager or FragmentStatePagerAdapter storing pages internally, which are not being released. 它可能与ViewPager或FragmentStatePagerAdapter在内部存储未发布的页面有关。 But I thought the commented out code I tried would have done that. 但是我认为我尝试过的注释掉的代码会做到这一点。 There might be another way that I have not tried. 我可能没有尝试过另一种方法。 Perhaps it is related to not recreating my Fragment, but doing so at this point in my architecture will not be great. 也许这与不重新创建我的Fragment有关,但是在我的体系结构中,此时这样做并不是很好。 I was hoping I could restart the ViewPager without doing this. 我希望我可以不这样做就重新启动ViewPager。

Here is my code: 这是我的代码:

My Fragment's onCreateView code. 我的片段的onCreateView代码。 Everything commented out I have tried without success. 一切都注释掉了,我尝试了没有成功。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_playscreen, container, false);

    //while (getChildFragmentManager().popBackStackImmediate()) {}

    mAdapter = new AdapterPlayPages(this, getChildFragmentManager());
    mPager = (ViewPlayPager) view.findViewById(R.id.pagerPlayScreen);
    //mPager.setAdapter(null);
    mPager.storeAdapter(mAdapter);
    //mPager.setCurrentItem(0);
    //mPager.removeAllViews();

    return view;
}

My ViewPager, which has been overridden like so to get around a completely unrelated bug, as suggested here: https://stackoverflow.com/a/19900206/1002098 . 我的ViewPager已被覆盖,以解决一个完全不相关的错误,如此处建议: https ://stackoverflow.com/a/19900206/1002098。 This does not effect the problem; 这不会影响该问题。 I removed these changes so that I could call setAdapter(null) as suggested, but it did not address my question. 我删除了这些更改,以便可以按照建议调用setAdapter(null),但是它没有解决我的问题。

public class ViewPlayPager extends ViewPager
{
    PagerAdapter mPagerAdapter;

    public ViewPlayPager(Context context)
    {
        super(context);
    }

    public ViewPlayPager(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow()
    {
        super.onAttachedToWindow();

        if (mPagerAdapter != null)
        {
            //super.setAdapter(null);  // did not help
            super.setAdapter(mPagerAdapter);
        }
    }

    @Override
    public void setAdapter(PagerAdapter adapter)
    {
        // do nothing
    }

    public void storeAdapter(PagerAdapter adapter)
    {
        mPagerAdapter = adapter;
    }
}

My FragmentStatePagerAdapter. 我的FragmentStatePagerAdapter。 I've removed some unrelated logic and members. 我删除了一些不相关的逻辑和成员。

public class AdapterPlayPages extends FragmentStatePagerAdapter
{
    private FragmentPlayScreen mParent;

    public AdapterPlayPages(FragmentPlayScreen parent, FragmentManager fragmentManager)
    {
        super(fragmentManager);

        mParent = parent;
    }

    @Override
    public int getCount()
    {
        // some logic from parent to determine true count
        // the count shouldn't effect the position to start at

        return Integer.MAX_VALUE;
    }

    @Override
    public Fragment getItem(int position)
    {
        // logic to determine type of page to display,
        // which is not changing based on position at the moment

        return new FragmentPlayPage();
    }

    // this did not seem to help me
    //@Override
    //public int getItemPosition(Object object)
    //{
    //     //return super.getItemPosition(object);
    //    return POSITION_NONE;
    //}
}

I had to change my architecture to recreate the Fragments - add/remove them from FragmentManager, as opposed to attach/detach them while kept in memory. 我不得不更改架构以重新创建片段-从FragmentManager中添加/删除它们,而不是在内存中附加/分离它们。

It is not clear to me why the Fragment would have to be recreated for the ViewPager to reset. 我不清楚为什么必须重新创建Fragment才能重置ViewPager。 Android FragmentManager and FragmentTransaction supports keeping Fragments in memory and simply attaching/detaching them, or showing/hiding them, so it should be possible to simply reset the ViewPager from the in-memory Fragment's onCreateView event, but again, none of the commented out calls above seemed to work. Android FragmentManager和FragmentTransaction支持将Fragments保留在内存中并简单地附加/分离或显示/隐藏它们,因此应该可以简单地从内存中Fragment的onCreateView事件中重置ViewPager,但同样,没有任何注释掉的调用以上似乎工作。

If anyone else has a solution, I'd still consider it. 如果其他人有解决方案,我仍然会考虑。 It will help me understand this issue. 这将帮助我理解此问题。

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

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