Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

android - RecyclerView not displaying in Fragment

New to Android, I used the code from https://github.com/tutsplus/Android-CardViewRecyclerView which is working perfectly.

But when I created Fragment out of it, it is displaying nothing.

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

I am calling this from Navigation drawer.

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

My MainActivity file is quite big, but every time I add a fragment, I just mention that it implements OnFragmentInteractionListener. If my Activity is working for all fragments except this one, there should not be any reason - why it is not working for this 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

Any idea - why in Fragment no display?

cardview_activity and recyclerview_activity remain the views without any changes.

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>

This question may be asked a couple of times but none of the solution helped me.

Many thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have not Override the Methods which are necessary for fragment. You should find some tutorials on Fragment first,rather using protected method onCreateView you should override its public method.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...