Android uses recyclerview to customize lists, click events and drop-down refresh

Using recyclerview for Android

1. What is recyclerview

Recyclerview is a new widget in android-support-v7-21. The official introduction to it is: recyclerview is an upgraded version of listview, which is more advanced and flexible.

Simply put, recyclerview is a new view group that aims to provide similar rendering for any adapter based view. It is supported in the latest version of support-v7 as the successor of listview and GridView controls.

2. What do you need to master to use recyclerview

Adapter -- wrap the data collection and create a view for each item layoutmanager -- place the view of each item in the appropriate position onitemclicklistener -- set the click event swiperefreshlayout for each item -- add the drop-down refresh event itemanimator to the recyclerview -- draw some decorative views itemdecoration around or on the view of each item N -- add animation effects when items are added, removed, or reordered

3. What needs to be prepared before using recyclerview

Add dependent packages (take Android studio as an example)

4. Custom adapter

Recyclerview.adapter contains a new type of adapter. In fact, it is basically similar to the adapters we used before, but slightly different. For example, viewholder is encapsulated for us, so we don't need to write viewholder by ourselves like the previous listview adapter.

The item view is as follows:

The example code is as follows:

5. What is the function of layoutmanager

Because recyclerview is not just like listview or GridView before, it is used as the successor of listview and GridView controls. When using it, you need to set its layoutmanager to specify which type it is. In addition, you can also set whether it is a horizontal or vertical list and whether it is reversed by setting layoutmanager.

Vertical list: (the third parameter is to set whether to reverse, i.e. sliding direction)

mLayoutManager = new linearlayoutmanager(this,linearlayoutmanager.VERTICAL,false); recyclerview.setLayoutManager(mLayoutManager);

Horizontal list:

mLayoutManager = new linearlayoutmanager(this,linearlayoutmanager.HORIZONTAL,false); recyclerview.setLayoutManager(mLayoutManager);

Vertical table layout:

mLayoutManager = new GridLayoutManager(this,2); recyclerview.setLayoutManager(mLayoutManager);

Horizontal table layout: (the fourth parameter represents whether to reverse)

mLayoutManager = new GridLayoutManager(this,2,false); recyclerview.setLayoutManager(mLayoutManager);

6. Why add onitemclicklistener yourself

Recyclerview has a disadvantage. In recyclerview, there is no onitemclicklistener method. Therefore, it is better to handle such events in the adapter at present. If you want to add or remove entries from the adapter, you need to explicitly notify the adapter. This is slightly different from the previous notifydatasetchanged () method. The specific operation can be reflected in the adapter code.

The specific codes are as follows:

7. Realize pull-down refresh and pull-up loading

In fact, as before, neither the native listview nor the GridView can meet the needs of the actual app. In many cases, the drop-down refresh and pull-up loading of the list are realized through the custom view. The same is true for the recyclerview. However, how to customize the recyclerview will not be introduced here. On the contrary, we will borrow the component swiperefreshlayout, Faster list refresh

The interface layout is as follows:

Set the drop-down refresh listening event:

Setting the pull-up load can be achieved by setting the sliding listening event:

8. Itemanimator and itemdecorator

Because there is no built-in split line in recyclerview, itemdecorator appears. In fact, there is another method, which is to directly add split lines in the item interface layout, which can save a lot of code; In short, itemanimator will modify, add and delete the animated display components according to the relevant notifications received on the adapter. It will automatically add and remove the animation of the item. The built-in default effect is also good, which is already very good. Because these two items are not very commonly used, we won't introduce them here. Interested students can find detailed information on the Internet.

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
分享
二维码
< <上一篇
下一篇>>