[英]How to add elements in a recyclerview on click of a button?
我有一个屏幕,单击一下按钮,需要在recyclerview中显示三个元素。
在我拥有“ recyclerview”的“活动”中,我想知道使单击按钮即可将项目添加到我的recyclerview适配器的逻辑。 这是我的活动的代码段:
List<OustChatModel> chatList;
chatList=new ArrayList<>();
chatList.add(new OustChatModel(1,
"Sample Image Card",
R.drawable.app_icon,
"sample description"));
chatList.add(new OustChatModel(2,
"Sample Video Card",
"Sample Video description",
R.drawable.app_icon
));
chatList.add(new OustChatModel(3,
"Textcard title",
"Textcard description"
));
final OustChatAdapter adapter = new OustChatAdapter(this, chatList,CourseChatActivity.this);
proceedChat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.setAdapter(adapter);
}
});
当我执行此代码时,单击一个按钮即可显示我的arraylist贴图的所有三个元素。 我希望元素仅在单击按钮后才在另一个元素的下方出现。
PLS。 确实建议合适的逻辑。
提前致谢。
试试这个:代码
chatList=new ArrayList<>();
int clickAmount = 0;
chatList.add(new OustChatModel(1,
"Sample Image Card",
R.drawable.app_icon,
"sample description"));
chatList.add(new OustChatModel(2,
"Sample Video Card",
"Sample Video description",
R.drawable.app_icon
));
chatList.add(new OustChatModel(3,
"Textcard title",
"Textcard description"
));
final OustChatAdapter adapter = new OustChatAdapter(this, chatList,CourseChatActivity.this);
proceedChat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newList =new ArrayList<>();
if(clickAmount < 3){
newList.add(clickAmount)
}
recyclerView.setAdapter(adapter, newList);
clickAmount ++;
}
});
每次单击时,将另一个元素添加到arraylist,然后显示该列表,而不是显示具有所有3个元素的那个列表。
要在单击按钮时使每个元素一个接一个地显示(意味着单击3次):
// Create the empty array list object
List<OustChatModel> emptyList = new ArrayList<>();
// Create a second array list containing objects
List<OustChatModel> chatList = new ArrayList<>();
chatList.add(new OustChatModel(1,
"Sample Image Card",
R.drawable.app_icon,
"sample description"));
chatList.add(new OustChatModel(2,
"Sample Video Card",
"Sample Video description",
R.drawable.app_icon));
chatList.add(new OustChatModel(3,
"Textcard title",
"Textcard description"));
// Pass empty list into the adapter
final OustChatAdapter adapter = new OustChatAdapter(this, emptyList, CourseChatActivity.this);
// set the adapter for the recycler view
recyclerView.setAdapter(adapter);
// counter to track number of clicks
int numberOfClicks = 0;
proceedChat.setOnClickListener(new View.OnClickListner() {
@Override
public void onClick(View view){
if (numberOfClicks < chatList.length()) {
adapter.addItem(chatList.get(numberOfClicks));
numberOfClicks++;
}
}
});
在OustChatAdapter
类中,您将具有以下方法:
public void addItem(OustChatModel model){
chatList.add(model);
notifyDataSetChanged(); // This is to notify the adapter that the data in the recyclerview has been modified.
}
旁注:我猜您正在参加的活动是CourseChatActivity
。 如果我错了,请纠正我,但是如果是这样,则无需使用this
传递类的实例,然后将CourseChatActivity.this
传递给OustChatAdapter
。 最好将适配器构造函数设置为使用:
new OustChatAdapter(this, emptyList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.