回收视图适配器未加载项目

时间:2017-07-20 08:27:10

标签: java android android-recyclerview recycler-adapter

我是android的新手我用它的适配器创建了Recycler视图,但是它不起作用。检查以下代码。

我尝试调试此活动,但没有错误。并且在OtherOfferingAdapter程序中,getItemCount()之后也不再执行。

档案 OtherOfferingAdapter.java

public class OtherOfferingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    List<OtherOfferingData> data = Collections.emptyList();
    OtherOfferingData current;
    int currentPos=0;



    //Create constructor to innitilize context and data sent from MainActivity

    public OtherOfferingAdapter(Context context,List<OtherOfferingData> data){
        this.context=context;
        inflater = LayoutInflater.from(context);
        this.data=data;
    }

    //Inflate the layout when viewholder created
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = inflater.inflate(R.layout.container_offerings, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    //Bind Data
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
        MyHolder myHolder = (MyHolder) holder;
        current = data.get(position);
        myHolder.inputOfferingName.setText(current.offeringname);
        myHolder.inputOfferingPrice.setText(current.price);
        myHolder.inputOfferingRating.setRating(Float.parseFloat(current.rating));

        Picasso.with(context).load(current.imageUrl).into(myHolder.inputOfferingImage);
        if(current.dietType.equals("1")){
            myHolder.inputDietType.setBackgroundResource(R.drawable.veg);
        }else{
            myHolder.inputDietType.setBackgroundResource(R.drawable.nonveg);
        }
    }

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

    class MyHolder extends RecyclerView.ViewHolder{

        ImageView inputOfferingImage, inputDietType;
        TextView inputOfferingPrice, inputOfferingName;
        RatingBar inputOfferingRating;

        public MyHolder(View itemView){
            super(itemView);

            inputOfferingImage = (ImageView) itemView.findViewById(R.id.otherimage);
            inputDietType = (ImageView) itemView.findViewById(R.id.otherdietType);
            inputOfferingPrice = (TextView) itemView.findViewById(R.id.otherprice);
            inputOfferingName = (TextView) itemView.findViewById(R.id.otherofferingnanme);
            inputOfferingRating = (RatingBar) itemView.findViewById(R.id.otherrating);

        }
    }
}

文件OtherOfferingData

public class OtherOfferingData {
    public String imageUrl;
    public String offeringname;
    public String price;
    public String dietType;
    public String rating;
}

我正在调用这样的适配器

recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
                        offeringAdapter = new OtherOfferingAdapter(ProductDescriptionActivity.this, data);
                        recyclerView.setAdapter(offeringAdapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(ProductDescriptionActivity.this));

container_offerings.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:layout_marginBottom="30dp"
    android:background="@color/lightGray"
    android:paddingLeft="15dp"
    android:paddingRight="15dp">

活动页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:design="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
                        android:id="@+id/recycle_view"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:background="@color/lightGray"
                        android:layout_marginBottom="90dp"/>

THANKYOU

1 个答案:

答案 0 :(得分:1)

相对布局中回收者视图的大小为0dp尝试更改

 android:layout_width="0dp"
 android:layout_height="0dp"

两者都匹配父

 android:layout_width="match_parent"
 android:layout_height="match_parent"

您的回收商视图及其适配器似乎没问题。但它们不会出现在屏幕上,因为它们的维度为0dp。