类似于餐厅页面的zomato或带有项目重复和标题的Recycler视图

时间:2018-11-28 06:33:46

标签: android android-recyclerview retrofit2 multiple-columns

如何通过以下类型创建回收站视图,如下图所示:

桶餐图像:

enter image description here

鸡图像:

enter image description here

如上面的“桶装餐图像”中所示,有一个类别Chicken有2个项目,而如“鸡图像”中所示,类别中有4个项目名为Burgers

所以我的问题是,如何创建具有重复项而不是标题的列表,直到输入其他类别数据。

文本菜单:

鸡肉

  • 香脆鸡
  • 烟熏鸡肉

汉堡

  • 要更长的汉堡
  • 蔬菜生姜汉堡
  • 鸡肉辛格汉堡
  • 鸡肉辛格餐

数据如下:

"category": [
{
  "name": "Panner Thali",
  "id": "1511",
  "desc": "This thali has price only 100 with 4 chpati, i plate rice, 1 sabji, 1 raiyta,1 daal...",
  "items": [
    {
      "name": "Panner with 2 roti and 1 sabji",
      "id": "13931",
      "real_price": "10",
      "description": "This is the testing panner with 2 roti and 1 sabji thali",
      "price": "20.00"
    },{
      "name": "Panner Veg Thali",
      "id": "13930",
      "real_price": "20",
      "description": "This is the testing description..",
      "price": "30.00"
    },{
      "name": "just for test 21",
      "id": "54502",
      "real_price": "0",
      "description": "This is the testing description..",
      "price": "30.00"
    },{
      "name": "just for testing",
      "id": "81742",
      "real_price": "100",
      "description": "This is the testing description..",
      "price": "110.00"
    }
  ]
},{
  "name": "latest cat",
  "id": "1602",
  "desc": "hi",
  "items": [
    {
      "name": "test5",
      "id": "21067",
      "real_price": "0",
      "description": "just test",
      "price": "60.00"
    },{
      "name": "test4",
      "id": "21066",
      "real_price": "0",
      "description": "just test",
      "price": "60.00"
    },{
      "name": "test3",
      "id": "21065",
      "real_price": "0",
      "description": "just test",
      "price": "13.00"
    },{
      "name": "test2",
      "id": "21064",
      "real_price": "0",
      "description": "just test",
      "price": "14.00"
    },{
      "name": "test1",
      "id": "21063",
      "real_price": "0",
      "description": "just test",
      "price": "15.00"
    },{
      "name": "just test",
      "id": "14854",
      "real_price": "50",
      "description": "",
      "price": "60.00"
    },{
      "name": "test6",
      "id": "21072",
      "real_price": "50",
      "description": "test",
      "price": "60.00"
    },{
      "name": "test7",
      "id": "21073",
      "real_price": "50",
      "description": "testt",
      "price": "60.00"
    },{
      "name": "test",
      "id": "81806",
      "real_price": "0",
      "description": "testing",
      "price": "20.00"
    }
  ]
}

为每个类别添加一个新的cardview,而为类别内的每个项目添加一个包含该项目详细信息的textview。

1 个答案:

答案 0 :(得分:0)

对于每个类别,您都可以使用recylerview。否则,您需要为菜单类型增加一个字段。然后将其短按菜单类型。然后采用两种类型的布局:一种用于物料类型,另一种用于物料。现在,基于“类型”或“索引”,将布局添加到recyclerview。

将multiview Adaper用于recyclerview。 Recylerview具有多视图适配器的最强大的功能。示例代码如下: 包com.diary.kumud.mydiary;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;

/*addapter for dashboard*/
public class MultiViewTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<DashBoardModel> dataSet;
    Context mContext;
    int total_types;

    public static class SingleBrickViewHolder extends RecyclerView.ViewHolder {

        TextView titleTxt, descTxt;
        ImageView iconImgView;

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

            this.titleTxt = (TextView) itemView.findViewById(R.id.textView5);
            this.descTxt = (TextView) itemView.findViewById(R.id.textView6);
            this.iconImgView = (ImageView) itemView.findViewById(R.id.imageView5);
        }
    }

    public static class DoubleBrickViewHolder extends RecyclerView.ViewHolder {

        ImageView iconImgView;
        TextView titleTxt;

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

            this.iconImgView = (ImageView) itemView.findViewById(R.id.imageView5);
            this.titleTxt = (TextView) itemView.findViewById(R.id.textView5);
        }
    }

    public MultiViewTypeAdapter(ArrayList<DashBoardModel> data, Context context) {
        this.dataSet = data;
        this.mContext = context;
        total_types = dataSet.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        switch (viewType) {
            case Model.DOUBLE_BRICK:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.smallbricks, parent, false);
                return new DoubleBrickViewHolder(view);
            case Model.SINGLE_BRICK:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bigbrick, parent, false);
                return new SingleBrickViewHolder(view);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {

        switch (dataSet.get(position).tilesType) {
            case 0:
                return Model.DOUBLE_BRICK;
            case 1:
                return Model.SINGLE_BRICK;
            default:
                return -1;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

        DashBoardModel object = dataSet.get(listPosition);
        if (object != null) {
            switch (object.tilesType) {
                case Model.DOUBLE_BRICK:
                    ((DoubleBrickViewHolder) holder).titleTxt.setText(object.titletext);
                    setImage(mContext,object.imagName,((DoubleBrickViewHolder) holder).iconImgView);
                    break;
                case Model.SINGLE_BRICK:
                    ((SingleBrickViewHolder) holder).titleTxt.setText(object.titletext);
                    ((SingleBrickViewHolder) holder).descTxt.setText(object.descText);
                    setImage(mContext,object.imagName,((SingleBrickViewHolder) holder).iconImgView);
                    break;
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startMyActivity(dataSet.get(listPosition).actionActivity);
                }
            });
        }
    }

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

    public void setImage(Context ctx, String imgName, ImageView myImgView){
        Resources res = ctx.getResources();
        int resID = res.getIdentifier(imgName , "drawable", ctx.getPackageName());
        Drawable drawable = res.getDrawable(resID );
        myImgView.setImageDrawable(drawable );
    }

    private void startMyActivity(String ActivityName){
        String activityToStart = ActivityName;
        try {
            Class<?> c = Class.forName(activityToStart);
            Intent intent = new Intent(mContext, c);
            mContext.startActivity(intent);
        } catch (ClassNotFoundException ignored) {
            Log.e("no activity","Activity not found");
        }
    }

}