RecyclerView不在Fragment中显示

时间:2016-02-03 09:46:24

标签: android android-fragments

Android新手,我使用了代码 https://github.com/tutsplus/Android-CardViewRecyclerView 这是完美的。

但是当我创建Fragment时,它什么都没显示。

CardViewFrag.java

package com.androidatc.customviewindrawer;

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class CardViewFrag extends Fragment {

    TextView personName;
    TextView personAge;
    ImageView personPhoto;


    protected void onCreateView(Bundle savedInstanceState) {


        getActivity().setContentView(R.layout.cardview_activity);
        personName = (TextView)getActivity().findViewById(R.id.person_name);
        personAge = (TextView)getActivity().findViewById(R.id.person_age);
        personPhoto = (ImageView)getActivity().findViewById(R.id.person_photo);

        personName.setText("Emma Wilson");
        personAge.setText("23 years old");
        personPhoto.setImageResource(R.drawable.emma);
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}

RecyclerViewFrag.java

package com.androidatc.customviewindrawer;

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewFrag extends Fragment {

    public List<Person> persons;
    public RecyclerView rv;


    public void onCreateView(Bundle savedInstanceState) {


        getActivity().setContentView(R.layout.recyclerview_activity);

        rv=(RecyclerView)getActivity().findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        initializeData();
        initializeAdapter();
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }


    public void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma));
        persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery));
        persons.add(new Person("Lillie Watts", "35 years old", R.drawable.lillie));
    }

    public void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(persons);
        rv.setAdapter(adapter);
    }
}

RVAdapter.java

package com.androidatc.customviewindrawer;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
        }
    }

    List<Person> persons;

    RVAdapter(List<Person> persons){
        this.persons = persons;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
    }

    @Override
    public int getItemCount() {
        return persons.size();
    }
}

我从导航抽屉里调用它。

private void loadFragmentLayout(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new CardViewFrag();
                break;
            case 1:
                fragment = new BarCodeFrag();
                break;
            case 2:
                fragment = new SearchBookFrag();
                break;
            case 3:
                fragment = new LoginFrag();
                break;
            case 4:
                fragment = new MyAccFrag();
                break;
            default:
                break;
        }

我的MainActivity文件非常大,但每次添加片段时,我都会提到它实现了OnFragmentInteractionListener。如果我的Activity适用于除此之外的所有片段,则不应该有任何原因 - 为什么它不适用于此Frag。

RecyclerViewFrag.OnFragmentInteractionListener,         CardViewFrag.OnFragmentInteractionListener

public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks,
        SearchCatFragment.OnFragmentInteractionListener,
       // AndroidBarcodeQrExample.OnFragmentInteractionListener,
        BarCodeFrag.OnFragmentInteractionListener,
        LoginFrag.OnFragmentInteractionListener,
        HomeFrag.OnFragmentInteractionListener,
        MainViewFragment.OnFragmentInteractionListener,
        SearchBookFrag.OnFragmentInteractionListener,
        SearchBookFragment.OnFragmentInteractionListener,
        MyAccFrag.OnFragmentInteractionListener,
        RecyclerViewFrag.OnFragmentInteractionListener,
        CardViewFrag.OnFragmentInteractionListener

任何想法 - 为什么片段中没有显示?

cardview_activity和recyclerview_activity保留视图而不做任何更改。

cardview_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="16dp"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_alignParentTop="true"
                android:textSize="30sp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_age"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_below="@+id/person_name"
                />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

recyclerview_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:padding="16dp"
    >



    <android.support.v7.widget.RecyclerView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/rv"
        >

    </android.support.v7.widget.RecyclerView>


</LinearLayout>

这个问题可能会被问过几次,但解决方案都没有帮助我。

非常感谢。

1 个答案:

答案 0 :(得分:1)

您没有覆盖片段所必需的方法。你应该首先找到一些关于Fragment的教程,而不是使用受保护的方法onCreateView,你应该覆盖它的公共方法。

public class RecyclerViewFrag extends Fragment {

   private List<Person> persons;
   private RecyclerView rv;

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.recyclerview_activity, container, false);
    }
     @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        rv=(RecyclerView)view.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        initializeData();
        initializeAdapter();
    }

    public interface OnFragmentInteractionListener {
      // TODO: Update argument type and name
     public void onFragmentInteraction(Uri uri);
    }

    private void initializeData(){
       persons = new ArrayList<>();
       persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma));
       persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery));
       persons.add(new Person("Lillie Watts", "35 years old", R.drawable.lillie));
    }

    private void initializeAdapter(){
      RVAdapter adapter = new RVAdapter(persons);
      rv.setAdapter(adapter);
}
}