RecyclerView没有出现在我的片段中

时间:2015-12-02 16:01:48

标签: android android-fragments

所以我在片段中有一个回收者视图,但由于某种原因它不会显示数据:我似乎无法找到它为什么不在里面显示卡片的原因回收视图。所以任何有关这方面的见解都会对我附上我的代码有所帮助,所以你们可以看看并给我一些反馈, 干杯!!

这是我的片段:我在哪里制作循环视图。     包lucas.app_2001;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import java.util.ArrayList;


public class OneFragment extends Fragment {
    static ArrayList<Person> persons = new ArrayList<>();
    private  RecyclerView.LayoutManager rv;
    private RecyclerView.Adapter adapter;
    ImageButton rate;


    public OneFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View view = inflater.inflate(R.layout.recyclerview_activity, container, false);
        final FragmentActivity c = getActivity();
        RecyclerView  recyclev = (RecyclerView) view.findViewById(R.id.Recycle);
        rv = new LinearLayoutManager(getActivity());
        Person.initializeData();
        adapter = new RVAdapter(persons);
        recyclev.setLayoutManager(rv);
        recyclev.setHasFixedSize(true);
        recyclev.setAdapter(adapter);
        rate = (ImageButton) view.findViewById(R.id.rate);
        initializeData(c);

        return view;
    }
    public static void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma, R.drawable.icorating0, "75pts."));
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma, R.drawable.icorating0, "20pts."));
        persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.emma, R.drawable.icorating0, "30pts."));
        persons.add(new Person("Lillie Watts", "35 years old", R.drawable.emma, R.drawable.icorating0, "40pts."));
    }


}

这是卡片视图java类:

package lucas.app_2001;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.lang.Override;

import lucas.app_2001.R;

public class CardViewActivity extends Activity {

    TextView personName;
    TextView personAge;
    ImageView personPhoto;
    ImageButton rate;
    TextView Points;
    LinearLayout Card;
    CardView  cardview_activity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.rate);
        setContentView(R.layout.cardview_activity);
        personName = (TextView)findViewById(R.id.person_name);
        Card = (LinearLayout)findViewById(R.id.Card);
        personAge = (TextView)findViewById(R.id.person_age);
        personPhoto = (ImageView)findViewById(R.id.person_photo);
        rate.setImageResource(R.drawable.icorating0);
        Points = (TextView)findViewById(R.id.Points);
        Card.setBackgroundResource(R.drawable.iphone_wallpaper_01b1);
        cardview_activity.setBackgroundResource(R.drawable.iphone_wallpaper_01b1);

        personName.setText("Emma Wilson");
        personAge.setText("23 years old");
        personPhoto.setImageResource(R.drawable.emma);
        Points.setText("75pts.");
        rate = (ImageButton) findViewById(R.id.rate);



    }
}

这是我的回收视图适配器:1

package lucas.app_2001;

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.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
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;
        ImageButton StarRate;
        TextView Points;

        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);
            StarRate = (ImageButton)itemView.findViewById(R.id.rate);
            Points = (TextView)itemView.findViewById(R.id.Points);
        }
    }

   private List<Person> persons;

    public 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);
    personViewHolder.StarRate.setImageResource(persons.get(i).rate);
    personViewHolder.Points. setText(persons.get(i).Points);


}

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

持有回收视图数据的人员类:

package lucas.app_2001;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import java.lang.String;
import java.util.ArrayList;
import java.util.List;

public class Person {
    String name;
    String age;
    int photoId;
    int rate;
    String Points;
    static List<Person> persons;


    Person(String name, String age, int photoId, int rate, String Points) {
        this.name = name;
        this.age = age;
        this.photoId = photoId;
        this.rate = rate;
        this.Points = Points;
    }

    public static void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma, R.drawable.icorating0, "75pts."));
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma, R.drawable.icorating0, "20pts."));
        persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.emma, R.drawable.icorating0, "30pts."));
        persons.add(new Person("Lillie Watts", "35 years old", R.drawable.emma, R.drawable.icorating0, "40pts."));


    }
}

我不确定我的错误可能在哪里,也不知道在哪里看我在这里寻找的是关于我可能搞砸的地方的一些指导

1 个答案:

答案 0 :(得分:2)

查看你的onCreateView:

 Person.initializeData();

&#34;人&#34;来自? 为什么initializeData()甚至是静态的?

不应该

OneFragment.initializeData()?