Java 在每个部分的recyclerview末尾显示不同的视图,如paytm

Java 在每个部分的recyclerview末尾显示不同的视图,如paytm,java,android,android-recyclerview,Java,Android,Android Recyclerview,我创造了这个设计。它由列表的列表组成 现在,我想在每个recycler视图的末尾的每个部分中添加一个视图作为“全部查看”。我想这样做是由paytm完成的 这是我的密码。我有两个回收站,一个是垂直的,另一个是水平的。这是我的BaseRowAdapter,其中包含另一个recyclerview BaseRowAdapter.java 它正在崩溃,因为IndexOutOfBoundsException。我不知道如何解决这个问题,请帮助。更改您的onBindViewHolder @Override

我创造了这个设计。它由列表的列表组成

现在,我想在每个recycler视图的末尾的每个部分中添加一个视图作为“全部查看”。我想这样做是由paytm完成的

这是我的密码。我有两个回收站,一个是垂直的,另一个是水平的。这是我的BaseRowAdapter,其中包含另一个recyclerview

BaseRowAdapter.java


它正在崩溃,因为IndexOutOfBoundsException。我不知道如何解决这个问题,请帮助。

更改您的
onBindViewHolder

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {

if (position != subTypes.size()) {
    SubType subType = subTypes.get(position);
    holder.tvProductName.setText(subType.name);
    Picasso.with(context).load(subType.image).into(holder.ivProduct);
    if (!subType.disc.equals("0")) {

        holder.tvDiscountPercentage.setText(subType.disc + "%");
        holder.tvActualAmount.setPaintFlags(holder.tvActualAmount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        int a = Integer.parseInt(subType.amount);
        float b = Float.parseFloat(subType.disc) / 100;
        holder.tvActualAmount.setText("₹ " + subType.amount);
        int c = (int) (a * b);
        holder.tvDiscountAmount.setText("₹ " + c);
    } else {
            holder.tvDiscountPercentage.setBackgroundColor(Color.parseColor("#ffffff"));
        holder.tvActualAmount.setText("₹ " + subType.amount);
    }
} else {
    holder.seeMore.setText("See More >>");
}
}

共享日志因为它是列表的列表,所以我无法执行此操作,因为子类型列表的大小正在更改。我也在添加logcat。尝试添加这一行
SubType SubType=subTypes.get(position)在您的
onBindViewHolder()中的
if(position!=subTypes.size())
之后
public class BaseRowItemAdapter extends RecyclerView.Adapter<BaseRowItemAdapter.CustomViewHolder> {

private Context context;
private ArrayList<SubType> subTypes;
private LayoutInflater inflater;

public BaseRowItemAdapter(Context context, ArrayList<SubType> subTypes) {
    this.context = context;
    this.subTypes = subTypes;
    this.inflater = LayoutInflater.from(context);
}

@Override
public int getItemViewType(int position) {
    return (position == subTypes.size()) ? R.layout.see_more : R.layout.single_item_card;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == R.layout.single_item_card) {
        view = inflater.inflate(R.layout.single_item_card, parent, false);
    } else {
        view = inflater.inflate(R.layout.see_more, parent, false);
    }

    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    SubType subType = subTypes.get(position);
    if (position != subTypes.size()) {
        holder.tvProductName.setText(subType.name);
        Picasso.with(context).load(subType.image).into(holder.ivProduct);
        if (!subType.disc.equals("0")) {

            holder.tvDiscountPercentage.setText(subType.disc + "%");
            holder.tvActualAmount.setPaintFlags(holder.tvActualAmount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            int a = Integer.parseInt(subType.amount);
            float b = Float.parseFloat(subType.disc) / 100;
            holder.tvActualAmount.setText("₹ " + subType.amount);
            int c = (int) (a * b);
            holder.tvDiscountAmount.setText("₹ " + c);
        } else {
            holder.tvDiscountPercentage.setBackgroundColor(Color.parseColor("#ffffff"));
            holder.tvActualAmount.setText("₹ " + subType.amount);
        }
    } else {
        holder.seeMore.setText("See More >>");
    }
}

@Override
public int getItemCount() {
    return subTypes.size()+1;
}


public class CustomViewHolder extends RecyclerView.ViewHolder {

    public ImageView ivProduct;
    public TextView tvProductName, tvActualAmount, tvDiscountAmount, tvDiscountPercentage, seeMore;

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

        tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
        tvActualAmount = (TextView) itemView.findViewById(R.id.tvActualAmount);
        tvDiscountAmount = (TextView) itemView.findViewById(R.id.tvDiscountAmount);
        tvDiscountPercentage = (TextView) itemView.findViewById(R.id.tvDiscountPercentage);
        ivProduct = (ImageView) itemView.findViewById(R.id.ivProduct);
        seeMore = (TextView) itemView.findViewById(R.id.seeMore);
    }
}
}
public class SubType {

    public int id;
    public String typeId;
    public String name;
    public String image;
    public String unit;
    public String amount;
    public String disc;
}

public class MainType {

    public int id;
    public String name;
    public ArrayList<SubType> list;
}
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
at java.util.ArrayList.get(ArrayList.java:308)
at com.ashishkudale.linksolution.adapters.BaseRowItemAdapter.onBindViewHolder(BaseRowItemAdapter.java:54)
at com.ashishkudale.linksolution.adapters.BaseRowItemAdapter.onBindViewHolder(BaseRowItemAdapter.java:23)
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {

if (position != subTypes.size()) {
    SubType subType = subTypes.get(position);
    holder.tvProductName.setText(subType.name);
    Picasso.with(context).load(subType.image).into(holder.ivProduct);
    if (!subType.disc.equals("0")) {

        holder.tvDiscountPercentage.setText(subType.disc + "%");
        holder.tvActualAmount.setPaintFlags(holder.tvActualAmount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        int a = Integer.parseInt(subType.amount);
        float b = Float.parseFloat(subType.disc) / 100;
        holder.tvActualAmount.setText("₹ " + subType.amount);
        int c = (int) (a * b);
        holder.tvDiscountAmount.setText("₹ " + c);
    } else {
            holder.tvDiscountPercentage.setBackgroundColor(Color.parseColor("#ffffff"));
        holder.tvActualAmount.setText("₹ " + subType.amount);
    }
} else {
    holder.seeMore.setText("See More >>");
}
}