首页 > 解决方案 > Recyclerview没有重新加载预期的片段

问题描述

目前在一个片段中实现一个回收器视图。以前是使用activity -recyclerview 完成的,没有任何问题。但是对于片段,我发现了奇怪的问题。

问题- 当我第一次加载回家时,我的 recyclerview 会正常加载。但是在更改为不同的片段并返回主页后,我的 recyclerview 不会加载但会卡在我之前加载的片段上。

有人可以澄清我做错了什么吗?

public class HomeFragment extends Fragment {
private List<FeedData> feedList = new ArrayList<>();
private RecyclerView recyclerView;
private FeedAdapter fAdapter;

private OnFragmentInteractionListener mListener;

public HomeFragment () {
}

public static HomeFragment newInstance(String param1, String param2) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    fAdapter = new FeedAdapter(feedList);
    RecyclerView.LayoutManager fLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(fLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(fAdapter);

   prepareMovieData();

    return rootView;
}

准备电影()

private void prepareMovieData() {
    FeedData feedData = new FeedData("Mad Max: Fury Road", "Action & Adventure", "2015");
    feedList.add(feedData);

    feedData = new FeedData("Inside Out", "Animation, Kids & Family", "2015");
    feedList.add(feedData);

    feedData = new FeedData("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
    feedList.add(feedData);

    feedData = new FeedData("Shaun the Sheep", "Animation", "2015");
    feedList.add(feedData);

    fAdapter.notifyDataSetChanged();
}

首页片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.HomeFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

抱歉这个菜鸟问题,只是学习。大多数代码都与 stackoverflow 和 androidhive 密切相关。*附件是复制问题外观的 gif。

通过 GIPHY

标签: androidandroid-fragmentsandroid-recyclerview

解决方案


我认为您正在用另一个片段替换一个片段 using:fragmentmanager.replace() 您还需要将片段添加到后台堆栈。为了更好地理解,请参阅: 如果存在,如何从 BackStack 恢复片段


推荐阅读