我仍在努力让自己了解recyclerview。我有一个arraylist,可用于初始化回收站视图。
如何设置适配器和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添加新项目?
您可以在Github Happy代码中看到此示例!!!
—
Cabezas