Reputation: 97
I have screen where I need to display three elements in a recyclerview on the click of a button.
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. 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. I want the elements to appear one below the other only after the click of the button.
Pls. do suggest suitable logic.
Thanks in advance.
Upvotes: 0
Views: 1407
Reputation: 533
To make each element appear one after the other when you click on the button (meaning 3 clicks):
// 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:
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
. 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
. It would be better set the adapter constructor to use:
new OustChatAdapter(this, emptyList)
Upvotes: 0
Reputation: 246
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.
Upvotes: 1