java - 如何在单击按钮时在回收站视图中添加元素?
问题描述
我有一个屏幕,我需要单击一个按钮在回收站视图中显示三个元素。
在我有我的 recyclerview 的 Activity 中,我想知道使我能够通过单击按钮将项目添加到我的 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 的所有三个元素都会在单击按钮时出现。我希望元素仅在单击按钮后才出现在另一个下方。
请。建议适当的逻辑。
提前致谢。
解决方案
试试这个:代码
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 个元素的列表。
推荐阅读
- sql - 多个连接表的 SQL 查询
- datetime - 多行/列时间戳,有什么方法可以让它更容易?
- flutter - 通过变量访问对象属性
- python - Python程序查找文档列表中是否存在某个关键字(字符串)
- kotlin - 找不到Android registerForActivityResult
- python - how to check if an attribute
YYYYMMDD in a .xml file is equal to a fixed Date value - python - Wikidata - 获取大量 id 的标签
- mongodb - 日期范围内带有objectId的mongodb指南针查询
- javascript - 带有 Javascript 的网关 IP
- kubernetes - Kubernetes:使用名称引用服务中的容器端口