android - 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。
解决方案
我认为您正在用另一个片段替换一个片段 using:fragmentmanager.replace() 您还需要将片段添加到后台堆栈。为了更好地理解,请参阅: 如果存在,如何从 BackStack 恢复片段
推荐阅读
- sql - 仅当应用程序用户界面设置为波斯语时出现验证错误
- php - Redmine Rest API 错误“主题不能为空”
- prerender - OG 标签不适用于 react-helmet 和 Netlify
- reactjs - 使用 Apollo React 客户端,为什么您并不总是需要解析器来解析 @client GraphQL 查询?
- java - 将对象列表转换为数组。获取 ArrayStoreException
- java - Elasticsearch 集成测试和 spring-test 不能很好地协同工作 - AccessControlException
- c# - mvc中的view不能第二次渲染html
- css - Angular EPUB 框架的 iOS 移动浏览器中的 domainurl/stylesheet.css 取消错误
- go - Gorutine 从 main 返回后没有完成执行。为什么?
- xamarin.android - 如何强制项目进入屏幕上的固定位置?