Recyclerview Anatomy of new Android controls

Source: Internet
Author: User

Preach wisdom • No Plume (Wisdom Podcast ) (Senior lecturer , Java College, Beijing Campus)

Profile:Apkbus , one of the experts, the Black Horse Technology salon President, in the mobile field has many years of practical development and research experience, proficient in HTML5,Oracle, ee ,Java Web Programming, the Android application development and platform development has a deeper research. From the basic to the advanced course, the teaching style is very popular among the students.


A new Widgetfor displaying complex views has been added to the Android L version of Recyclerview.


First,Recyclerview

AlternativeListViewof theRecyclerviewmakeViewholderstandardization, inListViewin whichConvertviewis multiplexed, inRecyclerviewin, is to putViewholderas the unit of the cache,Convertviewas aViewholderthe member variables remain in theViewholder, that is , assuming there is no screen displayTenentry, you create aTenaViewholdercached, each time the reuse isViewholder, so he putGetViewThis method has becomeOncreateviewholder. Viewholdera more suitable list of multiple seed layouts, especiallyIMList of conversations. RecyclerviewNot availableSetonitemclicklistenermethod, you canViewholderadd an event in the. Recyclerviewcan be used to refer to theMaterial Design uiwidgets".


Second,Recyclerview can realize horizontal and vertical sliding view

The layout of each item is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>  

<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"  

xmlns:tools= "Http://schemas.android.com/tools"  

xmlns:app= "Http://schemas.android.com/apk/res-auto"  

android:layout_width= "Match_parent"  

android:layout_height= "72DP"  

android:layout_margin= "3DP"  

Android:background= "#0000ff"

>

<textview  

android:id= "@+id/text"  

android:layout_width= "Match_parent"  

android:layout_height= "Match_parent"  

android:gravity= "center"  

/>  

</FrameLayout>  

the layout of item is simple, only one TextView, then need to use to recyclerview, so need to put support V7 Add to class pathand add the control to the layout:

<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"

/>

</RelativeLayout>  

then in the onCreate :  

Mdatas = new arraylist<> ();  

for (int i = 0; i <; i++) {

Mdatas.add (" i am Item---" + i);

}

Linearlayoutmanager manager = new Linearlayoutmanager  

(this,linearlayoutmanager.horizontal,false);  

adapter = new Simpleadapter (this, mdatas);

Recyclerview.setadapter (adapter);  

Recyclerview.setlayoutmanager (manager);  

Recyclerview.setitemanimator (New Defaultitemanimator ());  


Look directly at the code:

Public class  

Simpleadapter extends recyclerview.adapter<simpleadapter.myviewholder> {

private Context Mcontext;  

private list<string> Mdatas;  

private Final layoutinflater inflater;  

Public Simpleadapter (Context mcontext, list<string> mdatas) {

this.mcontext = Mcontext;  

This.mdatas =mdatas;  

inflater = Layoutinflater.from (mcontext);  

@Override  

Public Myviewholder Oncreateviewholder (viewgroup viewgroup, int i) {

View view = Inflater.inflate  

(R.layout.item_main, ViewGroup, false);  

Myviewholder viewholder = new Myviewholder (view);  

return viewholder;  

@Override  

Public void Onbindviewholder (final myviewholder holder, final int position) {

Holder.text.setText (mdatas.get (position));  

@Override  

public int GetItemCount () {

return Mdatas.size ();  

class Myviewholder extends recyclerview.viewholder{

Public Final  

TextView text;  

Public Myviewholder (View itemview) {

super (Itemview);  

Text = (TextView) Itemview.findviewbyid (R.id.text);

}

}

As shown in the code above:

Publicrecyclerview.viewholder Oncreateviewholder (viewgroup viewgroup, int i)

This method is primarily born into each item inflater view But the method returns a viewholder view directly encapsulated in viewholder viewholder This example, of course this viewholder We need to write it ourselves, directly omitted the original convertview.settag (holder) and convertview.gettag () these tedious steps.


Public Voidonbindviewholder (Recyclerview.viewholder viewholder, int i) This method is mainly used to fit the rendering data into the View . The method provides you with a viewholder, not the original Convertview.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recyclerview Anatomy of new Android controls

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.