RecyclerView添加页脚

时间:2017-08-02 17:32:43

标签: android android-recyclerview

我试图添加一个按钮作为我的RecyclerView的最后一项。我已经看过一些教程,但我还没弄清楚。我使用数组列表从我的主类中添加项目。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

public Context context;
private List<Archivos> mDataset;

public MyAdapter(Context context, List<Archivos> myDataset) {
    this.context = context;
    this.mDataset = myDataset;
}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_item, parent, false);
    Log.d("VIVZ", "onCreateHolder called");
    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.mTextTitle.setText(mDataset.get(position).titulo);
    holder.mTextDescription.setText(mDataset.get(position).descripcion);
    holder.mTextType.setText(mDataset.get(position).type);
    holder.mImageView.setImageDrawable(mDataset.get(position).icon);
}

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

@Override
public int getItemViewType(int position) {
    return (position == mDataset.size()) ? R.layout.fr_signup : R.layout.card_item;
}

class MyViewHolder extends RecyclerView.ViewHolder implements RecyclerView.OnClickListener {
    private CardView mCardView;
    private TextView mTextTitle;
    private TextView mTextDescription;
    private TextView mTextType;
    private ImageView mImageView;

    private MyViewHolder(View v) {
        super(v);
        mCardView = v.findViewById(R.id.card_view);
        mTextTitle = v.findViewById(R.id.card_doc_tittle);
        mTextDescription = v.findViewById(R.id.card_doc_description);
        mTextType = v.findViewById(R.id.card_doc_type);
        mImageView = v.findViewById(R.id.iv_image);
        mCardView.setOnClickListener(this);
    }

}

2 个答案:

答案 0 :(得分:0)

在下面的代码中添加额外的行以显示Recyclerview最后一行中的按钮,以查看方式初始化viewholder中的按钮:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
  holder.mTextTitle.setText(mDataset.get(position).titulo);
  holder.mTextDescription.setText(mDataset.get(position).descripcion);
  holder.mTextType.setText(mDataset.get(position).type);
  holder.mImageView.setImageDrawable(mDataset.get(position).icon);
  //To show button only in last row
   if(position = mDataset.size()-1)
     {
       holder.mButton.setText("OK");
     }
}

答案 1 :(得分:0)

我已经编辑了您的适配器以包含页脚,但它不是完整的复制+粘贴解决方案,但是有一些TODO和带有提示的评论。

public class MyAdapter extends RecyclerView.Adapter {

public Context context;
private List<Archivos> mDataset;

public MyAdapter(Context context, List<Archivos> myDataset) {
    this.context = context;
    this.mDataset = myDataset;
}

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

    if(viewType == R.layout.card_item){
      View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.card_item, parent, false);
      Log.d("VIVZ", "onCreateHolder called");
      return new MyViewHolder(v);
    } else{
       // TODO: create here a holder for the Footer, inflate fr_signup and return it
       Log.d("VIVZ", "onCreateFooterHolder called");
    }
}

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

    if(getItemViewType(position) == R.layout.card_item){
      MyViewHolder h = (MyViewHolder) holder;
      h.mTextTitle.setText(mDataset.get(position).titulo);
      h.mTextDescription.setText(mDataset.get(position).descripcion);
      h.mTextType.setText(mDataset.get(position).type);
      h.mImageView.setImageDrawable(mDataset.get(position).icon);
    } else {
      // TODO: cast holder to the ViewHolder that of the footer and set any values needed
    }
}

@Override
public int getItemCount() {
    return mDataset.size() + 1; // added here +1 for the footer
}

@Override
public int getItemViewType(int position) {
    return (position == mDataset.size()) ? R.layout.fr_signup : R.layout.card_item;
}

class MyViewHolder extends RecyclerView.ViewHolder implements RecyclerView.OnClickListener {
    private CardView mCardView;
    private TextView mTextTitle;
    private TextView mTextDescription;
    private TextView mTextType;
    private ImageView mImageView;

    private MyViewHolder(View v) {
        super(v);
        mCardView = v.findViewById(R.id.card_view);
        mTextTitle = v.findViewById(R.id.card_doc_tittle);
        mTextDescription = v.findViewById(R.id.card_doc_description);
        mTextType = v.findViewById(R.id.card_doc_type);
        mImageView = v.findViewById(R.id.iv_image);
        mCardView.setOnClickListener(this);
    }

}