我使用包含ViewPager的Fragment创建了一个应用程序。在ViewPager内部,我使用RecyclerView显示列表。当我启动应用程序时,一切都很棒。但是,当我滑动几个标签并且返回列表时,没有显示。我不知道出了什么问题......
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
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();
}
}
答案 0 :(得分:3)
ViewPager本身有一个方法setOffscreenPageLimit,它允许您指定适配器保留的页数。所以远处的碎片将被摧毁。
首先看看你的代码我不会看到你在片段onDestroy()中做任何内存释放措施。片段本身被破坏和gc&ed;这并不意味着你分配的所有资源都被删除了。
viewPager.setOffscreenPageLimit(10);