首页 > 解决方案 > 如何在单击按钮时在回收站视图中添加元素?

问题描述

我有一个屏幕,我需要单击一个按钮在回收站视图中显示三个元素。

在我有我的 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 的所有三个元素都会在单击按钮时出现。我希望元素仅在单击按钮后才出现在另一个下方。

请。建议适当的逻辑。

提前致谢。

标签: javaandroidandroid-adapterandroid-recyclerview

解决方案


试试这个:代码

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 个元素的列表。


推荐阅读