Android self-learning course-Use of RecyclerView (2), androidrecycler
Introduction to RecyclerView
RecyclerView is introduced in Android 5 in the Support-V7 package. She allows the display of items in whatever way (may be associated with ListView), as the package name says, can be used in API7 or above (Android 22 ).
Her name comes from the way she works. When an Item is hidden, she does not destroyed her and then creates a new item for each new object, hidden items are recycled: they are reused and new data is bound to them.
A RccyclerView is divided into six main components:
An Adpter that provides data (similar to Listview)
An ItemAnimator is responsible for modifying, adding, deleting, and moving the animation effects of items.
An ItemDecoration, Which can add drawings or change the layout of an item
An Layoutmanage that specifies the Items Layout
An ViewHolder, the base class of each Items View
The RecyclerView itself binds all
In the support-V7 package, some components have been bound to the default implementation method. You haveItemAnimator and threeLayoutmanage can be used for fun.RecyclerView does not need to be modified, andItemDecoration is optional.Adpter andViewHolder.
Display a RecyclerView1.Prepare your project add to your dependenceies:
RecyclerView: compile 'com. android. support: recyclerview-v7: 22.2.1'
CardView: compile 'com. android. support: cardview-v7: 22.2.1'
2. The bass item is a simple list. Each items contains a title and a subtitle.
1 public class Item{ 2 private String title; 3 private String subtitle; 4 5 public Item(String title,String subtitle){ 6 this.title = title; 7 this.subtitle = subtitle; 8 } 9 10 public String getTitle(){11 return title;12 }13 14 public String getSubtitle(){15 return subtitle;16 }17 }
3. Item layout our items is presented using CardView. A CardView is a modified FrameLayout, so it is very simple to display two textviews.
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v7.widget.CardView 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 app:contentPadding="8dp" 8 app:cardUseCompatPadding="true"> 9 10 <LinearLayout11 android:layout_width="match_parent"12 android:layout_height="match_parent"13 android:orientation="vertical">14 15 <TextView16 android:id="@+id/title"17 android:layout_width="match_parent"18 android:layout_height="wrap_content"19 android:singleLine="true"20 style="@style/Base.TextAppearance.AppCompat.Headline"/>21 22 <TextView23 android:id="@+id/subtitle"24 android:layout_width="match_parent"25 android:layout_height="0dp"26 android:layout_weight="1"27 style="@style/Base.TextAppearance.AppCompat.Subhead"/>28 29 </LinearLayout>30 31 </android.support.v7.widget.CardView>
4. The first step of The adapter is to define our own ViewHolder class. She must inherit RecycleView. ViewHolder and store it. When binding your data to holder, you need to use the View. (The translation is too bad) (original sentence: and shocould store references to the Views you will need when binding your data on the holder.) Here we have 2 textviews.
1 public class MyAdapter extents RecyclerView.Adaper<>{ 2 private static final String TAG = MyAdapter.class.getSImpleName(); 3 4 public static class MyViewHolder extents RecyclerView.Viewholder{ 5 TextView title; 6 TextView subtitle; 7 8 public MyViewHolder(View itemView){ 9 super(itemView);10 11 title = (TextView)itemView.findViewById(R.id.title);12 subtitle = (TextView) itemView.findViewById(R.id.subtitle);13 }14 }15 }
Now, what is the simplest way to store object sets? Yes, it is Collect. In this example, a simple method is written. we store our items (object set) in ArrayList and in the MyAdaper. java class.
private List<Item> items; private static final int ITEM_COUNT = 50; public Myadapet() { Random random = new Random(); items = new ArrayList<>(); for (int i = 0; i < ITEM_COUNT; i++) { items.add(new Item("Item:" + i, "this is the item number " + i, random.nextBoolean())); } }
Then we will implement the true RecyclerView. Adaper method:
- OnCreateViewHolder (ViewGroup viewGroup, int viewType) should create a View and return a matched ViewHolder,
- OnBindViewHolder (ViewHolder holder, int position) should use the position to get the data in the item to fill ViewHolder,
- GetItemCount () should be the number of items
In our example, the implementation method is relatively simple.
1 @Override 2 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 3 4 View v = LayoutInflater.from(viewGroup.getContext()).inflate(layout, viewGroup, false); 5 return new ViewHolder(v); 6 } 7 8 @Override 9 public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {10 final Item item = items.get(i);11 ViewHolder myViewHolder = (ViewHolder) viewHolder;12 myViewHolder.title.setText(item.getTitle());13 myViewHolder.subtitle.setText(item.getSubtitle())14 }15 16 @Override17 public int getItemCount() {18 return items.size();19 }
5. Bind everything together
We have defined what we need. Witness a miraculous moment.Step 1: Add a RecyclerView to Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>
We use simple LinearlayoutManger. We also use simple DefaultItemAnimator.
1 package com.ryan.recycleviewdemo02; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.support.v7.widget.DefaultItemAnimator; 6 import android.support.v7.widget.GridLayoutManager; 7 import android.support.v7.widget.LinearLayoutManager; 8 import android.support.v7.widget.RecyclerView; 9 import android.view.Menu;10 import android.view.MenuItem;11 12 public class MainActivity extends AppCompatActivity {13 14 private static String TAG = MainActivity.class.getSimpleName();15 16 private RecyclerView recyclerView;17 // private RecyclerView.LayoutManager layoutManager;18 // private RecyclerView.Adapter adapter;19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.activity_main);24 25 recyclerView = (RecyclerView) findViewById(R.id.recyclerView);26 recyclerView.setAdapter(new Myadapet());27 recyclerView.setItemAnimator(new DefaultItemAnimator());28 recyclerView.setLayoutManager(new LinearlayoutManger(this));29 30 }31 32 }
:
The above color effect will be discussed later.
Here is the display of the basic RecyclerView. Follow-up ............
Translation and personal understanding: www. enoent. fr/blog/2015/01/18/recyclerview-basics /,
Thank you.