Android-recylerview controls

Source: Internet
Author: User

Recylerview is a new control introduced by Android5.0, with the following features compared to the ListView and Gridview,recylerview:

1, do not care whether the item is displayed in the correct location, how to display. Use LayoutManager to control the display style of Recylerview

2, do not care about how to split between item. Use Itemdecoration to set the effect of a split line

3, do not care about the animation effect of item addition and deletion. Use Itemanimator to control animation effects

4, just focus on how to recycle and reuse view.

In Recylerview, it is important to remember that we are forced to use the viewholder pattern.

1. Create a layout file

A, build the layout of each item

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:background= "#44ff"5 Android:layout_margin= "3DP"6 Android:layout_height= "Match_parent">7     <TextView8         Android:id= "@+id/id_textview"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One android:gravity= "Center" A         /> - </LinearLayout>

B, establish the main layout

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.example.android_recylerview." Mainactivity ">    <Android. Support.v7.widget.RecyclerView Android:id= "@+id/id_recylerview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"/></Relativelayout>

2. Realizing the adapter of Recylerview

1  PackageCom.example.android_recylerview;2 3 ImportAndroid.content.Context;4 ImportAndroid.support.v7.widget.RecyclerView;5 ImportAndroid.view.LayoutInflater;6 ImportAndroid.view.View;7 ImportAndroid.view.ViewGroup;8 ImportAndroid.widget.TextView;9 Ten Importjava.util.List; One  A /** - * Created by a previous farewell to a paper book on 2017/2/23. -  */ the  -  Public classMyadapterextendsRecyclerview.adapter<myadapter.myviewholder> { -  -     PrivateLayoutinflater Minflater =NULL; +     PrivateList<string> Mdatas =NULL; -      PublicMyadapter (context context, list<string>datas) +     { A          This. Mdatas =datas; at          This. Minflater =Layoutinflater.from (context); -     } -     //Create Viewholder - @Override -      PublicMyviewholder Oncreateviewholder (viewgroup parent,intViewType) { -View view = Minflater.inflate (r.layout.item_layout, parent,false); inMyviewholder Viewholder =Newmyviewholder (view); -         returnViewholder; to     } +     //binding Viewholder - @Override the      Public voidOnbindviewholder (Myviewholder holder,intposition) { * Holder.mTextView.setText (mdatas.get (position)); $     }Panax Notoginseng  - @Override the      Public intGetItemCount () { +         returnmdatas.size (); A     } the     classMyviewholderextendsRecyclerview.viewholder +     { -          PublicTextView Mtextview =NULL; $          PublicMyviewholder (View itemview) { $             Super(Itemview); -Mtextview =(TextView) Itemview.findviewbyid (R.id.id_textview); -         } the     } -}

4. Using Recylerview in Mainactivity

 //set up layout managementLinearlayoutmanager Linearlayoutmanager =NewLinearlayoutmanager ( This, Linearlayoutmanager.vertical,false);//layout of the LinearLayoutGridlayoutmanager Gridlayoutmanager =NewGridlayoutmanager ( This, 4);//layout of the GridViewStaggeredgridlayoutmanager GridLayoutManager1 =NewStaggeredgridlayoutmanager (3, staggeredgridlayoutmanager.horizontal);//the layout of the horizontal GridViewMrecylerview.setlayoutmanager (GridLayoutManager1); //set up split lines//mrecylerview.additemdecoration (New Divideritemdecoration (this, divideritemdecoration.vertical_list));

5. Turn the GridView into a waterfall stream

The following code was implemented on Eclipse and I did not succeed on Android studio ....

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "72DP"5 Android:background= "#44ff"6 Android:layout_margin= "3DP"7 android:orientation= "vertical" >8     <TextView9         Ten         Android:id= "@+id/id_textview" One Android:layout_width= "Match_parent" A Android:layout_height= "Match_parent" - android:gravity= "Center" -         /> the  - </Framelayout>

At the same time, an array of list types is defined on the Myadapter to store the height of each item, and the initialization list array is initialized with a random number.

1          for (int i = 0; i < mdatas.size (); i++) 2         {3             mheights.add ((int) (+ math.random () *)); 4         }

After successful initialization, use the Onbindviewholder in the

1     //binding Viewholder2 @Override3      Public voidOnbindviewholder (Myviewholder holder,intposition) {4Viewgroup.layoutparams Layoutparams =holder.mTextView.getLayoutParams ();5Layoutparams.height =Mheights.get (position);6 Holder.mTextView.setLayoutParams (layoutparams);7 Holder.mTextView.setText (mdatas.get (position));8 9}

6. Adding and removing the item function

Under the menu file, add a few item and add the item to the Actionbar

1 <Item2         Android:id= "@+id/action_add"3 android:orderincategory= "+"4 android:showasaction= "Ifroom"5 Android:icon= "@drawable/ic_menu_add"6 Android:title= "Add"/>7     <Item8         Android:id= "@+id/action_delete"9 android:orderincategory= "+"Ten android:showasaction= "Ifroom" One Android:icon= "@drawable/ic_menu_delete" A Android:title= "Delete"/>

7. Adding animations to Recylerview

This animation is system to us, is the system implementation

1 mrecylerview.setitemanimator (new defaultitemanimator ());

Android-recylerview 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.