本文介绍了以编程方式将新项目添加到recyclerview吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我仍在努力让自己了解recyclerview。
我有一个数组列表,可用于初始化回收站视图。
I'm still trying to get my head around recyclerview.I have an arraylist, that I use to initialize a recycler view with.
如何在设置适配器和layoutmanager之后向回收站视图添加新项目?
How can I add new items to the recycler view post setting an adapter and layoutmanager?
private void initData() {
mItems = new ArrayList<String>();
for (int i = 0; i < ITEMS_COUNT; i++) {
mItems.add("Item " + (i + 1));
}
}
private void initRecyclerView() {
mRecentRecyclerView = (RecyclerView) findViewById(R.id.recentrecyclerView);
mRecentRecyclerView.setHasFixedSize(true);
mRecentLayoutManager = new LinearLayoutManager(this);
mRecentRecyclerView.setLayoutManager(mRecentLayoutManager);
mAdapter = new RecyclerView.Adapter<CustomViewHolder>() {
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notice_snippet
, viewGroup, false);
return new CustomViewHolder(view);
}
@Override
public void onBindViewHolder(CustomViewHolder viewHolder, int i) {
viewHolder.noticeSubject.setText(mItems.get(i));
}
@Override
public int getItemCount() {
return mItems.size();
}
};
mRecentRecyclerView.setAdapter(mAdapter);
private class CustomViewHolder extends RecyclerView.ViewHolder {
private TextView noticeSubject;
public CustomViewHolder(View itemView) {
super(itemView);
noticeSubject = (TextView) itemView.findViewById(R.id.notice_subject);
}
}
所以基本上在我 initdata之后()
和 initRecyclerView()
,如何将新项目添加到RecyclerView?
So basically after I initdata()
and initRecyclerView()
, how do i add a new item to the RecyclerView??
推荐答案
首先将您的商品添加到 mItems
,然后使用:
First add your item to mItems
and then use:
mAdapter.notifyItemInserted(mItems.size() - 1);
此方法比使用以下方法更好:
this method is better than using:
mAdapter.notifyDataSetChanged();
性能。
这篇关于以编程方式将新项目添加到recyclerview吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!