[英]How to add elements in a recyclerview on click of a button?
I have screen where I need to display three elements in a recyclerview on the click of a button. 我有一个屏幕,单击一下按钮,需要在recyclerview中显示三个元素。
In my Activity where I have my recyclerview, I want to know the logic which will enable me to add items to my recyclerview adapter on the click of my button. 在我拥有“ recyclerview”的“活动”中,我想知道使单击按钮即可将项目添加到我的recyclerview适配器的逻辑。 Here is the code segment of my activity : 这是我的活动的代码段:
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);
}
});
When I execute this code, all three elements of my arraylist apper on the click of a button. 当我执行此代码时,单击一个按钮即可显示我的arraylist贴图的所有三个元素。 I want the elements to appear one below the other only after the click of the button. 我希望元素仅在单击按钮后才在另一个元素的下方出现。
Pls. PLS。 do suggest suitable logic. 确实建议合适的逻辑。
Thanks in advance. 提前致谢。
Try this: code 试试这个:代码
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 ++;
}
});
Every time you click add another element to the arraylist, and then display that list instead of the one one with all 3 elements with it. 每次单击时,将另一个元素添加到arraylist,然后显示该列表,而不是显示具有所有3个元素的那个列表。
To make each element appear one after the other when you click on the button (meaning 3 clicks): 要在单击按钮时使每个元素一个接一个地显示(意味着单击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++;
}
}
});
In your OustChatAdapter
class you'll have this method: 在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.
}
Sidenote: My guess is the activity you're in is CourseChatActivity
. 旁注:我猜您正在参加的活动是CourseChatActivity
。 Correct me if I'm wrong but if that's the case, there is no need to pass an instance of the class using this
, then pass the CourseChatActivity.this
after into the OustChatAdapter
. 如果我错了,请纠正我,但是如果是这样,则无需使用this
传递类的实例,然后将CourseChatActivity.this
传递给OustChatAdapter
。 It would be better set the adapter constructor to use: 最好将适配器构造函数设置为使用:
new OustChatAdapter(this, emptyList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.