![]() The RecyclerView class does not implement any ready-made methods for working with checkboxes, so you need to write everything yourself. Please feel free to comment if you have problem implementing this solution. Answer: The fact is that the items are reused and for correct operation it is necessary to forcibly save the changes in the checkboxes to a separate storage, and restore the values from it when displaying them. It has 3 branches each demonstrating the 3 methods listed in this article,įeel free to submit contributions in the repo if you know other methods for solving this problem. Oziomajnr/RecyclerViewCheckBo圎xample2 RecyclerViewCheckBo圎xample2 ![]() I have created a github repo with the source code for this tutorial I prefer using this method since it does everything in the Adapter and does not interfere with the model.īehavior of the views after method 2 and 3įrom the animation above, notice how the state of the check boxes are retained even after the views are recycled. In the Adapter create a SparseBooleanArrayĭeclare a spare boolean array to hold the state of the itemsthen in the item click handler onClick() use the state of the items in the itemStateArray to check before toggling, here is an exampleĪlso use the sparse boolean array to set the checked state when the view is bound ![]() To do this, in the adapter we use a Map or a SparseBooleanArray (which is similar to a map but is a key value pair of int and boolean)to store the state of all the items in our list of items and then use the keys and values to compare when toggling the checked state. Use an array to hold the state of the items. The disadvantage of this method is that the model is now aware of the view and this might not be a very good software design, so the last method would make sure the state of the views are stored in the adapter.ģ. …this correctly displays the correct state for the correct view. Public void onBindViewHolder ( ViewHolder holder, int position )
0 Comments
Leave a Reply. |