Recyclerview is a newly added SDK in the Android L version to replace the ListView, which is more flexible and replaceable than the ListView. Next, a series of articles explaining how to use Recyclerview to completely discard the ListView.
Introduced
Recyclerview is similar to the ListView principle: it only maintains a small amount of view and can display a large number of datasets. The Recyclerview simplifies the presentation and processing of data in the following two ways:
- Use LayoutManager to determine how each item is arranged.
- Provides a default animation effect for adding and deleting items.
You can also define your own layoutmanager and add delete animations, the Recyclerview project structure is as follows:
Adapter: Before using Recyclerview, you need an adapter that inherits from Recyclerview.adapter to bind the data to each item's interface.
LayoutManager: Used to determine how each item is arranged, displayed, and hidden. When recovering or reusing a view, LayoutManager will request new data from the adapter to replace the old data, which avoids creating too many view and frequent calls to the Findviewbyid method (similar to the ListView principle).
There are three types of layoutmanager available in the SDK:
- Linearlayoutmanager
- Gridlayoutmanager
- Staggeredgridlayoutmanager
Example
The example shown in this section is one of the simplest ways to use it, and in the next few sections you will learn more about some of the other uses of Recyclerview. The author uses an environment that is Android Studio 0.8.6.
1. Add dependencies
Add dependencies in AS and build.gradle
then synchronize to introduce dependency packages:
1234 |
dependencies {... Compile ' com.android.support:recyclerview-v7:21.0.+ '} |
2. Write code
After you add the dependencies, you begin to write the code, similar to the ListView usage, and first create a Recyclerview layout in the XML layout file:
1234567891011 |
<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/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"/> </relativelayout> |
After creating the layout, get this recyclerview in mainactivity and declare the LayoutManager
Adapter
code as follows:
123456789 |
Mrecyclerview = (Recyclerview) Findviewbyid (R.id.my_recycler_view); //create default linear LayoutManagerMlayoutmanager = new Linearlayoutmanager (this); Mrecyclerview.setlayoutmanager (Mlayoutmanager); //If you can determine the height of each item is fixed, set this option to raise high performance mrecyclerview.sethasfixedsize (true); //Create and set adaptermadapter = new Myadapter (Getdummydatas ()); Mrecyclerview.setadapter (Madapter); |
The next problem is the creation of adapter:
1234567891011121314151617181920212223242526272829303132 |
public class Myadapter extends recyclerview. adapter<myadapter. viewholder> { Public string[] datas = null; public myadapter (string[] datas) { This.datas = datas;}//Create new view, called by LayoutManager@OverridePublic Viewholder oncreateviewholder (viewgroup viewgroup, int i) { View view = Layoutinflater.from (Viewgroup.getcontext ()). Inflate (R.layout.item,viewgroup,false);Viewholder VH =new Viewholder (view);return VH;}//Actions to bind data to the interface@Overridepublic void Onbindviewholder (viewholder viewholder, int i) { ViewHolder.mTextView.setText (Datas[i]);}//Get the number of data@Overridepublic int GetItemCount () { return datas.length;}//Custom Viewholder, holding all interface elements for each itempublic static class Viewholder extends recyclerview. Viewholder { Public TextView Mtextview;Public viewholder (view view) { super (view);Mtextview = (TextView) View.findviewbyid (R.id.text);}}} |
3. Operation
The example of writing this code can run. As can be seen from the example, the use of Recyclerview is not more complex than the ListView, but more flexible to use, it will be data, arrangement, data display methods are separated, so customizable, custom form is also very much, very flexible.
Landscape layout
If you want a horizontal list, just set Linearlayoutmanager as follows, and note that the type of Mlayoutmanager is Linearlayoutmanager instead of the parent LayoutManager:
1 |
Mlayoutmanager.setorientation (linearlayoutmanager.horizontal); |
Grid layout
If you want a list of grid layouts, simply declare LayoutManager as Gridlayoutmanager:
12 |
Mlayoutmanager = new Gridlayoutmanager (context,columnum); Mrecyclerview.setlayoutmanager (Mlayoutmanager); |
Note that you can also set the Orientation property of the list in a grid layout to achieve a horizontal and vertical grid layout.
Waterfall Flow Layout
Waterfall Flow use Staggeredgridlayoutmanager bar, the specific method is similar to the above, do not introduce.
Summarize
This section describes the most simple way to use Recyclerview, and later introduces some more advanced usages.
Introducing Recyclerview (i)