How to use RecyclerView to build an efficient list in Android

In Android applications, a list is a common type of UI control that can be used to display and manage many data items. RecyclerView is a new UI control provided by Android SDK, which can present lists in an efficient and flexible manner. This article will introduce how to use RecyclerView to build efficient lists.

What is RecyclerView?

RecyclerView is a custom view, similar to ListView or GridView, it has the following characteristics:

Based on the ViewHolder mode: Through the ViewHolder mode and combined with ViewType to trigger control recycling, RecyclerView can effectively reuse the loaded list items, thereby reducing memory consumption.

Flexible: With RecyclerView you can present a simple list, grid layout or highly customized very complex UI views.

Animation Pro: When the list is sliding, it allows you to add various animations to improve user experience.

Build lists with RecyclerView

Here are the basic steps to build a list using RecyclerView:

Introduce the dependency package of RecyclerView into the project, usually the AndroidX version.
dependencies { implementation 'androidx.recyclerview:recyclerview:1.2.1' } defines RecyclerView in the layout file. <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> Create an Adapter class to inherit RecyclerView.Adapter. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { // holder class static class ViewHolder extends RecyclerView.ViewHolder { TextView title; TextView subTitle;













    public ViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        subTitle = (TextView) view.findViewById(R.id.sub_title);
    }
}

// 数据集合
private List<Data> dataList;

...

}
Implement the ViewHolder of the Adapter class.
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_layout, parent, false); return new ViewHolder(view); } binding data and views. @Override public void onBindViewHolder(ViewHolder holder, int position) { Data data = dataList.get(position); holder.title.setText(data.title); holder.subTitle.setText(data.subTitle); } in Activity or Fragment Use RecyclerView in. // Get the RecyclerView control RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); // Set the layout manager















recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Set Adapter for RecyclerView
MyAdapter adapter = new MyAdapter(myDataList);
recyclerView.setAdapter(adapter);
Summary

RecyclerView is an efficient, flexible and powerful list control. Its advantages include control recycling, flexibility and animation professionalism triggered by ViewHolder mode and ViewType. Building lists with RecyclerView is a modern alternative. However, the use of RecyclerView may have a higher entry barrier and some

Guess you like

Origin blog.csdn.net/qq_21399461/article/details/129885892