The Recyclerview of Android development

Source: Internet
Author: User
<span id="Label3"></p><p><p>Recyclerview is a very useful control, its effect and the ListView is very similar, even can say that the appearance of Recyclerview is to replace the ListView</p></p><p><p>Recyclerview is more flexible than the ListView and has higher scalability</p></p><p><p>The following is a detailed introduction to the use of Recyclerview</p></p><p><p>Since Recyclerview came up to fetch the listview, it shows that the usage of the Recyclerview and the ListView is very similar, indeed, that is, two things, a control and an adapter (Adapter)</p></p>One: Guide Package<p><p>I developed here to use the androidstudio, and then use Recyclerview before the introduction of the Recyclerview package (this is necessary, is not explained)</p></p><p><p>Find the dependencies code block in the build file, add compile ' com.android.support:recyclerview-v7:23.0.0 ', and re-build the project to use Recyclerview</p></p><pre><pre><span style="color: #000000;">dependencies { compile filetree (include: [</span>' *.jar '], dir: ' Libs '<span style="color: #000000;">) </span>' com.android.support: recyclerview-v7:23.0.0 '<span style="color: #000000;">}</span></pre></pre>Two: Write XML file<p><p>Finish the work and you can start writing the code, first create an activity to generate an XML layout file, write the Recyclerview control in the layout file</p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_ Parent " android:layout_height=" match_parent "> <android.support.v7.widget.recyclerview android:id= "@+id/recyclerview" android:layout_width= "match_parent" android:layout_height= "match_parent "> </android.support.v7.widget.RecyclerView></RelativeLayout></pre></pre><p><p>Then write the above code is recyclerview when not directly write recyclerview, Must add the previous android.support.v7.widget, if you can't remember the previous content, to give you a simple way, now declare a variable in activity Recyclerview</p></p><p><p></p></p><p><p></p></p><p><p>You can see that the above import is automatically generated after the declaration of recyclerview, and the content behind the import is the full path of RECYCLERVIEW.</p></p>Three: Write Adapter class<p><p>First create a class that inherits from Recyclerview.adapter</p></p><p><p>I believe in the ListView is already familiar with Viewholder this class, in the Recyclerview adapter Viewholder built-in, So do not control what, You must first write an Viewholder inner class that inherits from the recyclerview.viewholder, overriding the constructor, and don't worry about implementing Adapter's abstract method at this Time.</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">public class Myrecyclerviewadapter extends Recyclerview.adapter { class Myviewholder extends Recyclerview.viewholder{public myviewholder (View itemview) { Super (itemview);}} }</pre></pre><p><p>Finish the above and then go to our writing Viewholder to adapter tied, is recyclerview.adapter behind the angle bracket content</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">public class Myrecyclerviewadapter extends recyclerview.adapter<myrecyclerviewadapter.myviewholder> { Class Myviewholder extends Recyclerview.viewholder{public myviewholder (View itemview) { Super (itemview); } }}</pre></pre><p><p>The above steps last night to implement the abstract method of adapter</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Myadapter<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Recyclerview.adapter<myadapter.myviewholder><span style="color: #000000;"><span style="color: #000000;">{@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Myviewholder Oncreateviewholder (viewgroup viewgroup,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">I) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onbindviewholder (myviewholder myviewholder,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">i) {} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">getitemcount () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>0<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Myviewholder<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">recyclerview.viewholder{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Myviewholder (View Itemview) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(itemview); } }}</span></span></pre><p><p>If you first implement the above abstract method, do not first do the above steps you will be very troublesome, and may even be confused at the thought of confusion</p></p><p><p>So this adapter class is basically molded, and the following describes how these methods work</p></p><p><p>First Oncreateviewholder () This method to see the name to know is used to create the Viewholder class, although the above write the Viewholder class but has not generated an instance, that is, new process so this method is used to new viewholder, See Myviewholder has a construction method, the constructor has a view object, so when creating viewholder here to pass a parameter, The view here is simply the object of the view of every item you recyclerview, loaded with layoutinflater, and finally returned Viewholder</p></p><p><p>Next Onbinderviewholder (); There are two parameters Myviewholder Myviewholder the Viewholder instance that you generated, int I represents the current line, So this method is to assign a value to the controls inside the Viewholder.</p></p><p><p>Look at GetItemCount (); just make sure how many lines you have for this RECYCLERVIEW.</p></p><p><p>Myviewholder is where each line of space is initialized</p></p><p><p>The above methods are implemented to form the following adapter class</p></p><pre class="brush:java;gutter:true;">public class Myadapter extends recyclerview.adapter<myadapter.myviewholder> {private list<string> mList; Private context context; Public myadapter (list<string> mlist,context Context) {this.mlist = mlist; This.context = context; } @Override public Myadapter.myviewholder oncreateviewholder (viewgroup viewgroup, int i) {view view = Layout Inflater.from (context). Inflate (r.layout.item,viewgroup,false); Myviewholder viewholder = new Myviewholder (view); Return viewholder; } @Override public void Onbindviewholder (myviewholder myviewholder, int i) {myViewHolder.tv_msg.setText (mlis T.get (i)); } @Override public int getitemcount () {return mlist.size (); } class Myviewholder extends Recyclerview.viewholder{TextView tv_msg; Public Myviewholder (View Itemview) {super (itemview); tv_msg = (TextView) Itemview.findviewbyid (r.id.tv_msg);   } }}</pre>Iv: contents of the last activity<p><p>Content in the activity is very simple, that is, setadapter, it is worth mentioning that in addition to setting adapter, in Recyclerview there is a layoutmanager of things, This is also the main reason for the high scalability of Recyclerview</p></p><p><p>The code is as follows</p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">public class Mainactivity extends Activity { private Recyclerview recyclerview; Private list<string> mlist; @Override protected void onCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Initview (); function (); } private void function () { mlist = new arraylist<> (); for (int i = 0; i <; i++) { mlist.add ("Hello" +i); } Myadapter adapter = new Myadapter (mlist,this); Linearlayoutmanager LLM = new Linearlayoutmanager (this,0,false); Recyclerview.setlayoutmanager (llm); Recyclerview.setadapter (adapter); } private void Initview () { recyclerview = (recyclerview) Findviewbyid (r.id.recyclerview);} }</pre></pre><p><p>explain, first find Recyclerview through Findviewbyid</p></p><p><p>Then initialize the list</p></p><p><p>Then generate the adapter instance</p></p><p><p>The next generation of LayoutManager objects</p></p><p><p>Finally set LayoutManager and Adapter.</p></p>V: Last<p><p>Explain Layoutmanager.</p></p><p><p>There are three kinds of layoutmanager in recyclerview, there are two kinds of commonly used, there are two kinds of common</p></p><p><p>The first kind of linearlayoutmanager</p></p><p><p>This is a look at the linear layout management, The construction method has three parameters, the first context object (not described), The second is an int parameter (only 0 and 1) 0 for the landscape, 1 for the portrait, the third parameter Boolean to indicate the order or reverse</p></p><p><p>False is the order, and true is reversed; the effect of this layout manager is the same as the ListView effect, and only one item can be placed on a row</p></p><p><p>The second kind of Gridlayoutmanager</p></p><p><p>This is known as the grid layout manager, which means that the manager can put more than one item in a row, it has two parameters, the first context is the object, the second int denotes the number of items placed on one line (1: indicates a line with a item;2: indicates that 2 items are placed on a line)</p></p><p><p>The Recyclerview of Android development</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.