Study on the use of recyclerview developed by Android

During the development of some material design specifications, such as the upward folding effect realized by the coordinatelayout mentioned earlier, if the listview is still used, this effect cannot be achieved, because the listview is incompatible with this control, and the recyclerview replaces it. Differences between listview and listview:

① Recyclerview only cares about the reuse and caching of items ② recyclerview doesn't care about the separation style of items (give it to itemdecoration) ③ recyclerview doesn't care about the animation of items (give it to itemanimator) ④ recyclerview doesn't care about the layout of items (give it to layoutmanager). This paper is a preliminary discussion, so we don't explain ② ③ ④ in detail first, but first record the usage and problems encountered, Update later! Let's start with the renderings. The animation effects are also mentioned in previous blogs. You can refer to the recyclerview in the middle. Here we mainly talk about how to use it and some problems encountered

Add dependencies first, and the version doesn't matter: the compile 'com. Android. Support: recyclerview-v7:23.1.1' layout file is as follows. Note that the last attribute can be ignored, which is used to coordinate the animation effect (that is, the effect of pushing the above image)

Customize an adapter that inherits from recyclerview.adapter. The code is as follows:

Analysis: the data source and root view are obtained from the construction method. The root view is used to display the snackBar. If you don't know the snackBar, please read the previous article. Then create an internal class, which inherits from recyclerview.viewholder and uses it to save all the controls of each item. Next, rewrite several methods: getitemcount, onbindviewholder and oncreateviewholder. They return the number of items, bind the viewholder and create the viewholder respectively. Creating the viewholder is to obtain the instance of each control through the layout file of the item. Binding the viewholder is to map the data in the data source to the control of the item, Because there are delete and modify buttons in our item, we add click events for them in onbindviewholder. Next, we define an interface onitembuttonclicklistener, and define two methods to callback click, delete and modify respectively, and create the object of the interface. When the button is clicked, we call the two methods of the interface respectively, and pass in the position of the clicked item. The code of activity part is as follows:

Resolution: after obtaining the instance of recyclerview, set the corresponding layout manager, then set and implement the callback function through setonitembuttonclicklistener, and finally set the adapter for recyclerview. Problems encountered: (1) just forgot to set up the layout, and RecyclerView didn't show it. Because after we deleted the item, we called the notifyItemRemoved method to display the animation and refresh it. In onBindViewHolder, the position was sent to the interface method when the delete button was monitored. The position was wrong because it was fixed at the time of binding. If the data of the data source is deleted, the size of the data source will become smaller, and this position will no longer correspond to each item of the data source, which will cause confusion. Therefore, the value obtained by viewholder. Getlayoutposition() should be passed in here, which is generated by clicking. ③ If you insert an item before the first item of the recyclerview and call notifyitmeinserted (0), the animation effect will not be played.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>