简体   繁体   English

从RecyclerView中消失的数据

[英]Disappearing data from RecyclerView

I made an app with Fragment containing ViewPager. 我用包含ViewPager的Fragment制作了一个应用程序。 Inside that ViewPager I am displaying list using RecyclerView. 在该ViewPager中,我正在使用RecyclerView显示列表。 When I launch app everything is great. 当我启动应用程序时,一切都很好。 But when I swipe few tabs and coming back list isn't shown. 但是,当我轻扫几个标签时,不会显示返回列表。 I don't know what is wrong... 我不知道怎么了...

Fragment displayed inside ViewPager 在ViewPager内部显示的片段

public class GradesFragmentPage extends Fragment {
    View view;
    List<Grade> gradeList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.grades_page_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        BetterRecyclerView betterRecyclerView = (BetterRecyclerView) getActivity().findViewById(R.id.gradesRecycler);
        betterRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        this.view = view;

        if(getArguments() != null) {
            gradeList = getArguments().getParcelableArrayList("grades");

            BetterRecyclerView betterRecyclerView = (BetterRecyclerView) view.findViewById(R.id.gradesRecycler);
            betterRecyclerView.setAdapter(new GradesAdapter(getActivity(), gradeList));
        }
    }
}

BetterRecyclerView BetterRecyclerView

public class BetterRecyclerView extends RecyclerView {
    public BetterRecyclerView(Context context) {
        super(context);
    }

    public BetterRecyclerView(Context context, AttributeSet attrs) {
        super(context,attrs);

        setLayoutManager(new LayoutManager() {
            @Override
            public LayoutParams generateDefaultLayoutParams() {
                return null;
            }
        });
    }

    public BetterRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //we need this protected method for scroll detection
    public int getVerticalScrollOffset() {
        return computeVerticalScrollOffset();
    }
}

ViewPager itself has a method setOffscreenPageLimit which allows you to specify number of pages kept by the adapter. ViewPager本身具有setOffscreenPageLimit方法,该方法允许您指定适配器保留的页面数。 So your fragments that are far away will be destroyed. 因此,您远处的碎片将被销毁。

First of all looking at your code I don't see you doing any memory releasing measures in your fragments onDestroy(). 首先,查看您的代码,我看不到您在片段onDestroy()中执行任何内存释放措施。 The fact that fragment itself is destroyed and gc'ed does not mean all resources you allocated were removed too. 片段本身被破坏并进行gc'ed操作并不意味着您分配的所有资源也会被删除。

viewPager.setOffscreenPageLimit(10);

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

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