在每个部分(如paytm)中,在recyclerview结束时显示不同的视图

时间:2017-02-13 09:10:25

标签: java android android-recyclerview recycler-adapter

我创建了这个设计。其中包括列表清单。

enter image description here

现在我想添加一个视图"查看全部"在每个回收者视图结束时的每个部分。我想这是由paytm完成的。

enter image description here

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

  

BaseRowAdapter.java

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

public ArrayList<MainType> types;
private Context context;
private LayoutInflater layoutInflater;

public BaseRowAdapter(ArrayList<MainType> types, Context context) {
    this.types = types;
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.vertical_scroll_single_entry, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.recyclerView.setAdapter(new BaseRowItemAdapter(context, types.get(position).list));
    holder.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
    holder.recyclerView.setHasFixedSize(true);
    holder.tvHeading.setText(types.get(position).name);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    RecyclerView recyclerView;
    TextView tvHeading;

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

        recyclerView = (RecyclerView) itemView.findViewById(R.id.rvItems);
        tvHeading = (TextView) itemView.findViewById(R.id.tvHeading);
    }
}
}

对于Inner recyclerview,我必须在最后看到更多的布局。我做过这样的事情。这导致崩溃的应用程序。

  

BaseRowItemAdapter.java

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

我的几个logcat详细信息

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)

由于 IndexOutOfBoundsException 而崩溃。我不知道如何解决这个问题,请帮忙。

1 个答案:

答案 0 :(得分:1)

更改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 >>");
}
}