List-Adapters
Target group: Android Beginner without foundation
Knowledge Points: The use of Recyclerview
Goal: Use Recyclerview to display list data in a page
Brief introduction
- Recyclerview Instructions for the adapter
Body
1. Create a class named Itemadapter.java in your project to act as an adapter for Recyclerview, one of the most basic examples is shown below
Public class itemadapter extends recyclerview. Adapter<itemadapter. Viewholder> { Privatearraylist<string> infos =NewArraylist<string> (); PublicItemadapter (arraylist<string> infos) { This. infos = Infos; } @Override PublicViewholder Oncreateviewholder (ViewGroup viewgroup,intViewType) {View view = Layoutinflater.from (Viewgroup.getcontext ()). Inflate (R.layout.item_card, Vie Wgroup,false);return NewViewholder (view); } @Override Public voidOnbindviewholder (Viewholder Viewholder,intPosition) {String info = infos.get (position); View view = Viewholder.itemview; TextView TextView = (TextView) View.findviewbyid (R.id.info_text); Textview.settext (info); } @Override Public intGetItemCount () {returnInfos.size (); } class viewholder extends recyclerview. Viewholder { PublicViewholder (View itemview) {Super(Itemview); } }}
-
This class inherits from Recyclerview.adapter and needs to pass in a Recyclerview.viewholder implementation class to guarantee the implementation of Viewholder, in this class for the inner class Viewholder
-
At a minimum, you need to implement three methods, Oncreateviewholder (create a corresponding sub-view based on the ViewType parameter), Onbindviewholder (bind the data to the Viewholder at the corresponding location based on the specified location), GetItemCount (length of Recyclerview)
- Oncreateviewholder can return sub-view of different layout formats according to different ViewType parameters, but you need to override the adapter getitemviewtype (int position) method to specify ViewType values for different locations
Add the following code to the 2.mainactivity.java
@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Recyclerview Mrecyclerview = (recyclerview) Findviewbyid (R.id.recyclerview);When the size of each sub-view does not change in the layout, this sentence can be called to improve performanceMrecyclerview.sethasfixedsize (true);//Specify a layout manager of type Linearlayoutmanager for RecyclerviewLinearlayoutmanager Mlayoutmanager =NewLinearlayoutmanager ( This); Mrecyclerview.setlayoutmanager (Mlayoutmanager);//Create a temporary data sourcearraylist<string> infos =NewArraylist<string> (); for(intA =0; A < -; a++) {Infos.add ("section"+ (A +1) +"line"); }//Create an Adapter objectItemadapter Itemadapter =NewItemadapter (infos);//Specify an Adapter object for RecycleviewMrecyclerview.setadapter (Itemadapter); }
3. For Recyclerview aesthetic considerations, add a line spacing for each itemview, the simplest way to find item_ Card.xml Add a new attribute in Android.support.v7.widget.CardView (card_view:cardusecompatpadding= "true"), the code is as follows
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:card_view="Http://schemas.android.com/apk/res-auto"Android:layout_width="Fill_parent"android:layout_height="Fill_parent"> <android. Support. V7. Widgets. CardViewAndroid:id="@+id/card_view"android:layout_gravity="Center"Android:layout_width="200DP"android:layout_height="200DP"card_view:cardusecompatpadding="true"card_view:cardcornerradius="4DP"> <textview android:id="@+id/info_text"Android:layout_width="Match_parent"android:layout_height="Match_parent"/> </android. Support. V7. Widgets. CardView></LinearLayout>
4. Re-build your project and run it on the emulator, if you can see a vertical list, then congratulations, this chapter is complete.
Android Basics (12)