当前在片段内实现一个recyclerview。以前使用活动-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;
}
preparemovie()
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();
}
HomeFragment
<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可以复制问题的样子。
答案 0 :(得分:0)
我认为您正在使用fragmentmanager.replace()将一个片段替换为另一个片段,还需要将片段添加到backstack。为了更好地理解,请参见: How to resume Fragment from BackStack if exists