Sample code for Android list switching

In e-commerce projects, there is often a need to switch the display form of the list in the commodity list page, which is generally divided into list form and table form.

For example, JD:

The final effect of this paper:

Key words: recyclerview

Main layout file: activity_ main.xml

List layout file: item_ list.xml

Tabular layout file: item_ grid.xml

Implementation principle: the gridlayoutmanager of recyclerview is used. The number of columns specified in list form is 1, and the number of columns specified in table form is the specific column value. The default is list form, and the specified number of columns is 1:

Switching between list form and table form:

Set the number of columns through gridlayoutmanager.setspancount (int column). Finally, don't forget to refresh the data with simpleadapter.notifyitemrangechanged (0, simpleadapter. Getitemcount()).

Adapter processing:

Two view types are defined: view_ TYPE_ List and view_ TYPE_ GRID

Load corresponding layout files according to different view types, as follows:

Get view type: when the number of columns is 1, the view type is view_ TYPE_ List, when the number of columns is 3, the view type is view_ TYPE_ GRID

Full code: https://github.com/wangshouquan/list-to-grid

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