簡體   English   中英

將項目添加到回收者視圖?

[英]Add items to a recycler view?

因此,我嘗試實現一種方法,在該方法中,單擊按鈕時,會將新行或新項目添加到recyclerview。 我已經閱讀了很多關於此的內容,但我始終無法解決。 任何幫助將不勝感激!

我的適配器在這里,我相信這是您編寫大多數代碼以將項目添加到recyclerview的地方:

     public class DazAdapter extends RecyclerView.Adapter<DazAdapter.MyViewHolder> {
List<Information> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;

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

public void addData(Information newModelData, int position) {
    data.add(position, newModelData);
    notifyItemInserted(position);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.custom_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current = data.get(position);
    holder.title.setText(current.title);

}

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

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);

    }
}

}

您是否嘗試在addData方法上調用notifyDataSetChanged()而不是notifyItemInserted

就我而言,我使用它從Loader加載數據,但是我認為當您添加單個項目時它應該可以工作。

 public void onLoadFinished(Loader<List<Data>> loader, List<Data> data) {
    this.mAdapter.setDatas(data);
    this.mAdapter.notifyDataSetChanged();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM