我使用本教程中的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>
应用程序在调用片段时崩溃。 我们将非常感谢您的帮助。
答案 0 :(得分:2)
您在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);
答案 1 :(得分:2)
public class DemoActivity extends Fragment {
private RecyclerView mRecyclerView;
private CountryAdapter mAdapter;
private LinearLayoutManager layoutManager;
public DemoActivity(){}
@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);
mAdapter = new CountryAdapter(CountryManager.getInstance().getCountries(), R.layout.card_layout, getActivity());
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
请使用此代码,希望对您有帮助。