java – 将RecyclerView与Fragments一起使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 将RecyclerView与Fragments一起使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3474字,纯文字阅读大概需要5分钟。
内容图文
![java – 将RecyclerView与Fragments一起使用](/upload/InfoBanner/zyjiaocheng/795/72bdb55e7de04285a6757effb0cb5b2a.jpg)
我在本教程中使用CardView构建了RecycleView小部件:
Android L Tutorials (Part 3) – RecyclerView and CardView
现在我试图把它显示为碎片.
这是我的FragmentActivity:
public class FragmentActivity extends Fragment {
private RecyclerView mRecyclerView;
private CountryAdapter mAdapter;
private LinearLayoutManager layoutManager;
public FragmentActivity(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.card_layout, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
我调用片段的MainActivity:
公共类MainActivity扩展Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentActivity fragment = new FragmentActivity();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container_list, fragment).commit();
}
这是我的activity_main布局:
<RelativeLayout 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=".CountryActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CountryActivity"
/>
</RelativeLayout>
我的card_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
card_view:cardCornerRadius="5dp"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/countryImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:tint="@color/photo_tint"
android:layout_centerInParent="true"
/>
<TextView
android:id="@+id/countryName"
android:gravity="center"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="24sp"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
和frame_layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container_list">
</FrameLayout>
应用程序在调用片段时崩溃.
你的帮助将不胜感激.
解决方法:
你在Fragment类中膨胀错误的布局
View rootView = inflater.inflate(R.layout.card_layout, container, false); //Here you are inflating the row layout instad of the fragment
只需更改正确的布局参考
View rootView = inflater.inflate(R.layout. activity_main, container, false);
同样在您的MainActivity中,将布局资源更改为:
setContentView(R.layout.frame_layout);
内容总结
以上是互联网集市为您收集整理的java – 将RecyclerView与Fragments一起使用全部内容,希望文章能够帮你解决java – 将RecyclerView与Fragments一起使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。