简体   繁体   English

将 RecyclerView 与 Fragment 一起使用

[英]Using RecyclerView with Fragments

I built the RecycleView widget with CardView from this tutorial:我从本教程中使用 CardView 构建了 RecycleView 小部件:

Android L Tutorials (Part 3) - RecyclerView and CardView Android L 教程(第 3 部分)- RecyclerView 和 CardView

Now Im trying to show it as a Fragment.现在我试图将它显示为一个片段。

Here is my FragmentActivity:这是我的 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 where I call my fragment: public class MainActivity extends Activity {我在其中调用片段的 MainActivity:public class MainActivity extends 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();
}

Here is my activity_main layout:这是我的 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>

My card_layout我的 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>

and frame_layout :和 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>

The app crashes when it calls the fragment.应用程序在调用片段时崩溃。 Your help will be appreciated guys.伙计们,您的帮助将不胜感激。

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);
    }

Please use this code hope so it will be help you.请使用此代码,希望对您有所帮助。

You are inflating the wrong layout in your Fragment class您在Fragment类中夸大了错误的布局

View rootView = inflater.inflate(R.layout.card_layout, container, false);  //Here you are inflating the row layout instad of the fragment

Just change the layout reference for the proper one只需更改适当的布局参考

View rootView = inflater.inflate(R.layout. activity_main, container, false);

Also in your MainActivity change the layout resource to:同样在您的 MainActivity 中,将布局资源更改为:

setContentView(R.layout.frame_layout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM