Home » Java » java – Android Studio – How to create a recyclerview with unique buttons-Exceptionshub

java – Android Studio – How to create a recyclerview with unique buttons-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am creating an app that needs a recyclerview but instead of the usual textviews, imageviews etc, I need the recyclerview to contain buttons. I need to be able to rearrange the order of the recyclerview. Is there any way I could do this?

How to&Answers:

Welcome to Stackoverflow,

You can do it by passing datasource to recyclerview like List<String> buttonIds.
Inside onCreateViewHolder(), create a xml layout which contains Button and bind buttonId in onBindViewHolder.

To rearrange position by code, you can switch position of buttonIds by using Collections.swap() then calls adapter.notifyDataSetChanged().

If you want user changes position by dragging. You can check out https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf