Summary
RecyclerView is a scroll control provided in support:recyclerview-v7. To use this control, you need to do the following:
- Introduce dependency Library
- Add RecyclerView label to layout file
- Create a single layout file
- Create adapter class
- Set up the layout manager and adapter for Recyclerview in the activity
Although it's not complicated, it's hard for me who has a bad memory to remember completely, so I wrote this note.
Reference articles
What you know about RecyclerView and what you don't know are all here (Part 1)
Since I used RecyclerView, my waist is no longer painful and my hands are no longer sour
Recycle view use full resolution
Android RecyclerView uses full parsing to experience artistic controls
I. introduce dependency Library
implementation 'com.android.support:recyclerview-v7:27.0.2'
II. Add labels to layout files
<android.support.v7.widget.RecyclerView android:id="@+id/rv_mycreation" android:layout_width="match_parent" android:layout_height="match_parent" />
III. create single layout file
The layout file is the layout of each item in the scroll control, and the value of each item will be set in the ViewHolder subclass of the adapter
item_base_use.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dp" android:background="#ddebd774" android:orientation="vertical" android:layout_marginBottom="10dp"> <TextView android:id="@+id/item_tx" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
IV. create adapter class
public class MyRecyclerViewAdapterextends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> list; public MyAdapter(List<String> list) { this.list = list; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // Bind item layout View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_base_use, parent, false); MyAdapter.ViewHolder viewHolder = new MyAdapter.ViewHolder(view); // You can set event listening for each item here return viewHolder; } @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) { // Set the value of the control String string = list.get(position); holder.mText.setText(string); } @Override public int getItemCount() { // Returns the length of the list return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { // Control object, corresponding to the control in item TextView mText; ViewHolder(View itemView) { super(itemView); // Bound controls mText = itemView.findViewById(R.id.item_tx); } } }
5. Set the layout manager and adapter for Recyclerview in the activity
//Get the RecyclerView instance through findViewById mRecyclerView = findViewById(R.id.recyclerView); //Set up RecyclerView Manager mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //Initialize adapter mAdapter = new MyRecyclerViewAdapter(list); //Animate when adding or deleting item s, the default animation is used here mRecyclerView.setItemAnimator(new DefaultItemAnimator()); //Set adapter mRecyclerView.setAdapter(mAdapter);