如何在单击按钮时在recyclerview中添加元素?

时间:2019-02-28 10:33:18

标签: java android android-recyclerview android-adapter recycler-adapter

我有一个屏幕,单击一下按钮,需要在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贴图的所有三个元素。我希望仅在单击按钮后,这些元素才能在另一个元素的下方出现。

请。确实建议合适的逻辑。

谢谢。

2 个答案:

答案 0 :(得分:1)

尝试一下:代码

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

答案 1 :(得分:0)

要在单击按钮时使每个元素依次出现(意味着单击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)