What is Recyclerview
Recyclerview is the newest ListView, GridView component of Google, Recyclerview is a container for displaying large amounts of data and improves scrolling performance with a limited number of child view.
Unlike ListView, Recyclerview is no longer responsible for layout, but focuses on layout reuse. Layouts are managed primarily through LayoutManager, and currently provide 3 common layout management:
Linearlayoutmanager Linear layout Manager (ListView effect)
Gridlayoutmanager Grid Layout Manager (GridView effect)
Staggeredgridlayoutmanager Waterfall Flow Manager
Recyclerview mainly through the adapter to get the child view to display. Adapter to inherit the Recyclerview.adapter class, and you need to make a generic of recyclerview.viewholder subclasses.
Use Recyclerview need guide package, need to add dependency on Build.gradle
dependencies {
Compile filetree (dir: ' Libs ', include: [' *.jar '])
compile ' com.android.support:appcompat-v7 : 21.0.3 '
compile ' com.android.support:recyclerview-v7:21.0.3 '
...
The code is as follows:
//inherits from Recyclerview.adapter class Myadapter extends myadapter.viewholder> {//recyclerview View//This method returns is Viewholder, when there is a reusable view, no longer calls @Override public Viewholder OnC
Reateviewholder (viewgroup viewgroup, int i) {View v = getlayoutinflater (). Inflate (R.layout.recycler_item, NULL);
return new Viewholder (v); //Bind data to a child view, automatically reuse view @Override public void Onbindviewholder (Viewholder viewholder, int i) {VIEWHOLDER.TEXTVIEW.S
Ettext (Data[i]);
//recyclerview display the number of data bars @Override public int getitemcount () {return data.length;
///Custom Viewholder, reduce Findviewbyid call number class Viewholder extends Recyclerview.viewholder {TextView TextView;
ImageView ImageView;
Locate the contained UI component public Viewholder (View Itemview) {super (Itemview) in the layout;
TextView = (TextView) Itemview.findviewbyid (R.id.textview);
ImageView = (ImageView) Itemview.findviewbyid (R.id.imageview); }
}
}
Note: When the data changes, you can refresh the UI through Notifydatasetchanged, Getitemviewtype to get the type of the corresponding position, but it no longer requires you to specify how many types, because the method has been able to determine how many types.
The child view layout is as follows: Res/layout/recycler
<?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=" match_parent ">
< ImageView
android:layout_width= "wrap_content"
android:layout_height= wrap_content "android:id=" @+
Id/imageview "
android:src=" @drawable/ic_launcher "/>
<textview android:layout_width=
" Wrap_ Content "
android:layout_height=" match_parent "
android:text=" Medium text "
Android:layout_ marginleft= "5DP"
android:gravity= "center_vertical"
android:id= "@+id/textview"/>
When adapter is ready, the next step is to prepare recyclerview. Add Recyclerview to the layout of the 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"/>
Then in the activity, add the initialization, set code
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Find Recyclerview
Recyclerview recylcer = (recyclerview) Findviewbyid (R.id.recyclerview);
ListView Effect of Linearlayoutmanager
linearlayoutmanager mgr = new Linearlayoutmanager (this);
Vertical longitudinal, similar to Listview,horizontal<span style= "font-family:arial, Helvetica, Sans-serif;" > transverse, similar to gallery</span>
mgr.setorientation (linearlayoutmanager.vertical);
Recylcer.setlayoutmanager (MGR);
Set Adapter
recylcer.setadapter (New Myadapter ());
The effect after the operation is as follows:
If you want to display the GridView effect, replace the Linearlayoutmanager with the Gridlayoutmanager
GridLayout 3 column
gridlayoutmanager mgr=new gridlayoutmanager (this,3);
The effect is as follows:
If you want to display the effect as a waterfall stream, you need to change to Staggeredgridlayoutmanager.
3 Column Direction vertical
staggeredgridlayoutmanager mgr = new Staggeredgridlayoutmanager (3, staggeredgridlayoutmanager.vertical );
When using waterfall flow, the height of the best is not the same, so you can randomly set the height of the view. Modifying the methods in adapter
Bind data to a child view
@Override public
void Onbindviewholder (viewholder viewholder, int i) {
ViewHolder.textView.setText (Data[i]);
Linearlayout.layoutparams LP = new Linearlayout.layoutparams (
LinearLayout.LayoutParams.MATCH_PARENT,
< Span style= "White-space:pre" > </span>100+ (int) (Math.random ()));
VIEWHOLDER.IMAGEVIEW.SETLAYOUTPARAMS (LP);
The operation effect is as follows:
The above is a small set to introduce the Android Recyclerview detailed understanding of the implementation of ListView GridView Waterfall flow effect, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!