RecyclerView未显示在片段中 [英] RecyclerView not displaying in Fragment

查看:71
本文介绍了RecyclerView未显示在片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的新手,我使用了 https://github.com/tutsplus/Android-CardViewRecyclerView 效果很好.

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

但是当我用它创建Fragment时,它什么也没显示.

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

CardViewFrag.java

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

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

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

我的MainActivity文件很大,但是每次添加一个片段时,我只提到它实现了OnFragmentInteractionListener.如果我的活动适用于除此片段以外的所有片段,则应该没有任何原因-为什么它不适用于此片段.

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

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

任何想法-为什么在Fragment中不显示?

Any idea - why in Fragment no display?

cardview_activity和recyclerview_activity保持视图不变.

cardview_activity and recyclerview_activity remain the views without any changes.

cardview_activity.xml

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

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.

非常感谢.

推荐答案

您尚未覆盖片段所需的方法.您应该首先找到有关Fragment的一些教程,而不是使用受保护的方法 onCreateView 覆盖其公共方法.

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

这篇关于RecyclerView未显示在片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆