Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RecyclerView in android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use RecyclerView in android. I hope you will get something after reading this article. Let's discuss it together.

In order to make RecyclerView available in all Android versions, the Android development team defined RecyclerView in the support.v7 package. When using this control, you need to open the build.gradle file of the current Modile, and then add the code that depends on the library in the dependencies node. The specific code is as follows:

Compile 'com.android.support:recyclerview-v7: insert version number'

After the addition is completed, you need to click the Sync Now in the upper right corner to synchronize. After the synchronization is complete, you can add RecyclerView controls to the layout file in the following syntax format:

Some common methods are needed to control RecyclerView, which are as follows:

◆ setLayoutManager: sets the layout manager for list items, with LinearLayoutManager as the linear layout manager, GridLayoutManager as the grid layout manager, and StaggeredGridLayoutManager as the waterfall flow grid layout manager.

◆ setItemAnimator: animates when list items are added or deleted. You can use the keyword new to create a DefaultItemAnimator () object to achieve the system's default animation.

◆ addItemDecoration: add list item splitter lines.

◆ addOnItemTouchListener: a touch listener to add list items.

◆ removeOnItemTouchListener: removes the touch listener for the list item.

◆ setAdapter: sets the adapter for the list item, using RecyclerView.Adapter.

RecyclerView.Adapter is an adapter class designed separately for RecyclerView. The relevant methods of RecyclerView.Adapter are as follows:

◆ getItemCount: gets the number of list items.

◆ onBindViewHolder: binds the data displayed in the list item.

◆ onCreateViewHolder: in this method, you can load the layout file in the list item (child).

The following is a concrete example to demonstrate the specific application of RecyclerView vertical list.

Instance 07 simulates QQ message list instance location: CD\ Code\ SL\ 27\ 07

Video location: CD\ Video

Create a Module in Android Studio with the name "QQMessageList" as follows:

(1) Open the build.gradle (Module: QQMessageList) file and add the code that depends on the library in the dependencies node in the file.

(2) modify the layout file activity_main.xml under the res/layout directory of the new Module, modify the default added layout manager to the relative layout manager and delete the TextView component, and then add a RecyclerView component to display the message list. The specific code is as follows:

01 02 08 09 16

(3) create a layout file named layout_item.xml in the res/layout directory, and modify the layout manager added by default to the relative layout manager, first add an icon used to display the avatar by ImageView component, and then add a vertical linear layout manager. In the layout manager, add two TextView components to display the name and text message respectively. The specific code is as follows:

01 02 08 09 16 22 23 30 31 39 40

(4) add the name and text message to the strings.xml string resource file in the res/values directory for display in the list.

(5) create a Java class named Adapter in the com.mingrisoft package, first create an internal class named MyViewHolder in the class and let the class inherit from RecyclerView.ViewHolder, and get the components used to display the name, avatar icon and text information in the list in the internal class. Then let the Adapter class inherit from RecyclerView.Adapter and implement the corresponding method. The specific code is as follows:

01 public class Adapter extends RecyclerView.Adapter {02 @ Override03 public MyViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {04 return null;05} 06 @ Override07 public void onBindViewHolder (MyViewHolder holder, int position) {08} 09 @ Override10 public int getItemCount () {11 return 0 return 12} 13 class MyViewHolder extends RecyclerView.ViewHolder {14 public TextView name, info / / numbered text 15 public ImageView img; / / Icon 16 17 / / get related controls 18 public MyViewHolder (View itemView) {19 super (itemView); 20 name = (TextView) itemView.findViewById (R.id.name); 21 info = (TextView) itemView.findViewById (R.id.info) 22 img = (ImageView) itemView.findViewById (R.id.img); 23} 24} 25}

(6) create three arrays of int type to save icon resources, names and text information of the avatar, then create three ArrayList collections to save the resource files in the array, and finally create an Adapter construction method. In this method, the specific codes for setting the number of menu lines and inline icons, names and text information are as follows:

01 / / Array of icons 02 private int [] icons = {03 R.mipmap.icon_1, R.mipmap.icon_2, R.mipmap.iconicon 3 R.mipmap.icon_4 04, R.mipmap.icon_5, R.mipmap.iconicon 6 05 R.mipmap.icon_7, R.mipmap.icon_8, R.mipmap.iconicon 9 06 R.mipmap.icon_10, R.mipmap.icon_1107} 08 / / name array, citing the words 09 private int [] names in the resource file = {10 R.string.name1, R.string.name2, R.string.name3 R.string.name4 11 R.string.name4, R.string.name5, R.string.name6 12 R.string.name7, R.string.name8, R.string.name9 13 R.string.name10, R.string.name1114} 15 / / Information array 16 private int [] infos = {17 R.string.info1, R.string.info2, R.string.info3 R.string.info1121 18 R.string.info4, R.string.info5, R.string.info6 19 R.string.info7, R.string.info8, R.string.info9 20 R.string.info10, R.string.info1121}; 22 private Context lContext; / / context 23 private List listIcon = new ArrayList () / / Icon set 24 private List listName = new ArrayList (); / / name set 25 private List listInfo = new ArrayList (); / / Information set 26 public Adapter (Context context) {27 lContext = context;28 / / set menu rows and inline icons, names, messages 29 for (int I = 0; I

< 11; i++) {30 listIcon.add(icons[i]);31 listName.add(names[i]);32 listInfo.add(infos[i]);33 }34 } (7)重写MyViewHolder onCreateViewHolder()方法,在该方法中实现获取列表中,每行item的布局文件。修改后代码如下: 01 @Override02 public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {03 //获取列表中,每行的布局文件04 View view = LayoutInflater.from(lContext).inflate(R.layout.layout_item, arg0, false);05 MyViewHolder holder = new MyViewHolder(view); //06 return holder;07 } (8)重写onBindViewHolder()方法,在该方法中设置列表菜单中item(子项)所显示的内容。修改后代码如下: 01 @Override02 public void onBindViewHolder(final MyViewHolder holder, int position) {03 //设置图标04 holder.img.setBackgroundResource(listIcon.get(position));05 //设置名称06 holder.name.setText(listName.get(position));07 //设置信息08 holder.info.setText(listInfo.get(position));09 }`` (9)重写getItemCount()方法,在该方法中实现返回数据集中的项目总数。修改后代码如下: 01 @Override02 public int getItemCount() {03 return listIcon.size();04 } (10)打开主活动MainActivity.java文件,定义所需要的全局变量,在onCreate()方法中首先获取RecyclerView组件,然后为其设置列表布局管理器,最后为其设置适配器。具体代码如下: 01 public class MainActivity extends AppCompatActivity {02 private RecyclerView lRecyclerView; //列表控件03 private Adapter lAdapter; //适配器04 @Override05 protected void onCreate(Bundle savedInstanceState) {06 super.onCreate(savedInstanceState);07 setContentView(R.layout.activity_main);08 //获取列表控件09 lRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);10 //设置列表布局管理11 lRecyclerView.setLayoutManager(new LinearLayoutManager(this));12 //设置适配器13 lRecyclerView.setAdapter(lAdapter = new Adapter(this));14 }15 } (10)在工具栏中,找到 下拉列表框,然后单击要运行的应用(这里为QQMessageList),再单击右侧的 运行按钮,运行效果如图27.27所示。

Figure 27.27 simulated QQ message list

After reading this article, I believe you have a certain understanding of "how to use RecyclerView in android". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report