What is Recyclerview?
I personally think that Recyclerview is just a ListView upgrade, the main purpose of this upgrade is to make this view more efficient, and more convenient to use.
We know that ListView improves performance by using Viewholder. Viewholder makes the ListView slide more smoothly by saving references to controls used in the item to reduce Findviewbyid calls. But this pattern is no harm if it is not used in ListView.
In other words, you may not consider the problem of reuse in ListView, but you are sacrificing memory to facilitate the code. But Recyclerview does not allow you to do so, you use recyclerview means that you must reuse, and the effect is actually similar to Listview+viewholder.
Demo Effect: (source in the end of the article)
Main implementation Features:
1, can be dynamic typesetting, choose Linearlayou and GridLayout
2, can add or subtract item
3, the implementation of the item click event Monitoring
4, to achieve click events, click to make the item in the font turned red
How to use Recyclerview?
Recyclerview is a new component in the SUPPORT-V7 package (meaning to import the V7 package first), an imposed sliding component that also has the function of item recycling, compared to the classic ListView. But Recyclerview has already encapsulated the Viewholder, the user only needs to realize own viewholder to be possible, this component will automatically help you to reuse each item.
Using Recyclerview I think there are two main steps:
1, set LayoutManager
2, set up and define adapter (mainly implement Viewholder)
Because the use of Recyclerview and ListView is similar, here or with the more familiar ListView to explain.
The use of Recyclerview and ListView, mainly differs in two places:
1, need to define LayoutManager (this is relatively simple, not much explanation)
2, listview definition of adapter mainly for view to operate, and Recyclerview mainly for Viewholder to operate.
3, ListView itself to achieve the click event, and Recyclerview if you need to click events, you need to write an interface. (Novice not afraid, not difficult)
Here is a point to introduce in the ListView will not meet a few points, but also may be novice think of the recyclerview difficulties:
First, Recyclerview for the Viewholder to operate
The main point here is to understand the three methods that Recyclerview must implement:
Public Viewholder Oncreateviewholder (viewgroup parent, int viewtype)
When any viewholder is instantiated, the Oncreateviewholder will be triggered.
The content implemented here is similar to the Oncreateview in fragment, except that Oncreateview is the last one to return to view and a viewholder is returned here. (note: When used, the viewholder should be defined, not recyclerview.viewholder)
public void Onbindviewholder (viewholder holder, int position)
This establishes the association of Views and data in Viewholder. Because Viewholder is achieved by itself, the use of viewholder here will appear particularly free and convenient.
public int GetItemCount ()
This is not much to say, and ListView in the same, return the size of the data.
In addition to these three methods, the most important thing is to implement a viewholder, this viewholder also need to inherit Recyclerview.viewholder, (if you need to implement the Click event, also need to apply Onclicklistener)
In this viewholder, you can set properties and bind to the controls in the Viewholder view, which is handy to use.
The author demo code:
public class Viewholder extends Recyclerview.viewholder implements View.onclicklistener {public
TextView Tvviewholder;
Public LinearLayout Llviewholder;
Initializes the Viewholder, where the public
Viewholder (View itemview) {
super (Itemview) can be used directly after binding in Onbindviewholder;
Tvviewholder= (TextView) Itemview.findviewbyid (r.id.tv_view_holder);
Llviewholder= (LinearLayout) Itemview;
Llviewholder.setonclicklistener (this);
}
Implement Recyclerview Click events through an interface callback
@Override public
void OnClick (View v) {
if (monitemclicklistener!=null) {
//Here the Onitemclick method is invoked, and this method is implemented when the Recycleradapter is instantiated
Monitemclicklistener.onitemclick (V, GetItemCount ());}}
Second, click the event to write an interface of their own
If this takes a little time to understand the concept, it is not difficult, mainly the following steps: (Do not know to see the author demo)
The following are the author of the Code in the demo;
1. Create an interface and write in it the methods you need to implement
Defines the interface of the Onitemclicklistener, making it easy to implement its click Effect when instantiated
interface Onitemclicklistener {
void Onitemclick (View view, int position);
2, create an object of this interface to store the listening events
Public Onitemclicklistener Monitemclicklistener;
3, where you need to use the method to make the call
Public Viewholder (View itemview) {
super (Itemview);
Tvviewholder= (TextView) Itemview.findviewbyid (r.id.tv_view_holder);
Llviewholder= (LinearLayout) Itemview;
Llviewholder.setonclicklistener (this);
}
Implement Recyclerview Click events through an interface callback
@Override public
void OnClick (View v) {
if (monitemclicklistener!=null) {
//Here the Onitemclick method is invoked, and this method is implemented when the Recycleradapter is instantiated
Monitemclicklistener.onitemclick (V, GetItemCount ());
}
Source screenshot:
Mainactivity:
Package com.example.double2.recyclerviewtest;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.GridLayoutManager;
Import Android.support.v7.widget.LinearLayoutManager;
Import Android.support.v7.widget.RecyclerView;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ArrayAdapter;
Import Android.widget.Button;
Import Android.widget.Spinner;
Import Android.widget.TextView;
Import java.util.ArrayList;
Import java.util.List;
public class Mainactivity extends Appcompatactivity {private Recyclerview mrecyclerview;
Private Recycleradapter Mrecycleradapter;
Private Recyclerview.layoutmanager Mlayoutmanager;
Private Spinner Mspinner;
Private list<string> Mdata = new arraylist<string> ();
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layoUt.activity_main);
Increase test data Mdata.add ("Recycler");
Mdata.add ("Recycler");
Mdata.add ("Recycler");
Initview ();
private void Initview () {Mrecyclerview = (Recyclerview) Findviewbyid (R.id.rc_main);
Mlayoutmanager = new Linearlayoutmanager (this);
Mrecyclerview.setlayoutmanager (Mlayoutmanager);
Mrecyclerview.sethasfixedsize (TRUE);
Set Spinner Mspinner = (Spinner) Findviewbyid (R.id.sp_main);
list<string> mlist = new arraylist<string> ();
Mlist.add ("LinearLayout");
Mlist.add ("GridLayout"); Mspinner.setadapter New Arrayadapter<string> (this, Android.
R.layout.simple_list_item_1, mlist)); Mspinner.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {@Override public void Onitemse
Lected (adapterview<?> Parent, view view, int position, long ID) {switch (position) {case 0: Set to Linear layout Mrecyclerview.setlayoutmaNager (New Linearlayoutmanager (Mainactivity.this));
Break Case 1://Set to Grid Layout, 3-column Mrecyclerview.setlayoutmanager (New Gridlayoutmanager (Mainactivity.this, 3))
;
Break
@Override public void onnothingselected (adapterview<?> parent) {}});
Mrecycleradapter = new Recycleradapter (mdata);
Mrecyclerview.setadapter (Mrecycleradapter); Mrecycleradapter.setonitemclicklistener (New Recycleradapter.onitemclicklistener () {//Here implements Onitemclick interface @Ov Erride public void Onitemclick (final view view, int position) {TextView Tvrecycleviewitemtext = (TextView
) View.findviewbyid (R.id.tv_view_holder); If the font is black it turns red, and vice versa becomes black if (tvrecycleviewitemtext.getcurrenttextcolor () = Color.Black) Tvrecycleview
Itemtext.settextcolor (color.red);
else Tvrecycleviewitemtext.settextcolor (Color.Black);
}});
Button Btnadd = (button) Findviewbyid (R.id.btn_main_add);
Button Btndel = (button) Findviewbyid (R.id.btn_main_del); Btnadd.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {MDat
A.add ("Recycler");
int position = Mdata.size ();
if (Position > 0) mrecycleradapter.notifydatasetchanged ();
}
}); Btndel.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {int
Position = Mdata.size ();
if (Position > 0) {mdata.remove (position-1);
Mrecycleradapter.notifydatasetchanged ();
}
}
});
}
}
Recycleradapter:
Package com.example.double2.recyclerviewtest;
Import Android.support.v7.widget.RecyclerView;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import java.util.List; /** * Project Name: Recyclerviewtest * Founder: Double2 number * Date Created: 2016/4/18 8:12 * Change NOTE: * * public class Recycleradapter Extend
s recyclerview.adapter<recycleradapter.viewholder> {private list<string> mdata;
Public Recycleradapter (list<string> data) {mdata = data;
//define a Listener object to store the listener event public Onitemclicklistener Monitemclicklistener;
public void Setonitemclicklistener (Onitemclicklistener itemclicklistener) {monitemclicklistener = ItemClickListener; }//define Onitemclicklistener interface to facilitate the implementation of its click Effect when instantiated interface Onitemclicklistener {void Onitemclick (Vi
EW view, int position); } public class Viewholder extends recyclerview.viewhoLder implements View.onclicklistener {public TextView tvviewholder;
Public LinearLayout Llviewholder;
Initializes the Viewholder, where the public Viewholder (View Itemview) {super (Itemview) can be used directly after binding in Onbindviewholder;
Tvviewholder= (TextView) Itemview.findviewbyid (R.id.tv_view_holder);
Llviewholder= (linearlayout) Itemview;
Llviewholder.setonclicklistener (this); //Through an interface callback to implement Recyclerview click event @Override public void OnClick (View v) {if monitemclicklistener!=nu LL) {//Here the Onitemclick method is invoked, and this method is implemented when the Recycleradapter is instantiated Monitemclicklistener.onitemclick (V, GetItem
Count ()); @Override public Viewholder Oncreateviewholder (viewgroup parent, int viewtype) {View views= Layo
Utinflater.from (Parent.getcontext ()). Inflate (R.layout.rc_item,parent,false);
Return to new Viewholder (views); @Override public void Onbindviewholder (viewholder holder, int position) {//establishedHolder.tvViewHolder.setText (Mdata.get (position) +position) trying to correlate with data in Viewholder;
@Override public int GetItemCount () {return mdata.size ();
}
}
Activity_main:
<?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 "android:orientation=" vertical "
android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" > <android.support.v7.widget.recyclerview android:id= "@+id/rc_main" android:layout_width= "Match_paren
T "android:layout_height=" 0DP "android:layout_weight=" 1 "> </android.support.v7.widget.RecyclerView> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:orie
ntation= "Horizontal" > <spinner android:id= "@+id/sp_main" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_conTent "/> <button android:id= @+id/btn_main_add" android:layout_width= "Wrap_content" Andro id:layout_height= "Wrap_content" android:text= "@string/add"/> <button android:id= "@+id/btn_main_ Del "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" @string/D
El "/> </LinearLayout> </LinearLayout>
Rc_item:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_content "
android: Layout_margin= "3DP"
android:background= "@android: Color/darker_gray"
android:gravity= "center"
android:orientation= "vertical"
>
<imageview
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@mipmap/ic_launcher"/>
<textview
Android:id= "@+id/tv_view_holder"
android:layout_width= "wrap_content"
android:layout_height= "WRAP_" Content "
android:textcolor=" @android: Color/black "
android:textsize=" 20sp "/>
</ Linearlayout>
The above is the entire content of this article, I hope to learn about the Android layout helps.