Android Learning note 25 ListView Multi-layout implementation
This section describes the last section of the ListView control, implementing a multi-layout of item. As we often use the various Instant messaging tools, QQ, etc., if their conversational interface is the ListView implementation, then the ListView has a variety of item layout, this section, we will implement a ListView of a variety of item.
To implement a ListView with multiple items, rewrite the adapter's two methods Getviewtypecount () and getitemviewtype (int position), the first method is to return the type number of item, The second is to return the type of the current item. Here's what we do to illustrate:
Custom Adapter Code:
Package Com.example.listviewdemo.adapter;import Android.content.context;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;import Com.example.listviewdemo.chat;import com.example.listviewdemo.r;import java.util.list;/** * Created by Devin on 2016/7 /11. * Implement a variety of item layouts * Override two methods Getviewtypecount () and getitemviewtype (int position) */public class Chatadapter extends Baseadapter { private static String TAG = "Chatadapter";/** * layout displayed on right */public static final int item_right = 0;/** * Middle display time */public STA Tic Final int item_time = 1;/** * Layout */public static final int item_left = 2;private static int item_total_count = 3; Private context Mcontext;private list<chat> chatlist;public chatadapter (Context mcontext, list<chat> chatlist) {this.mcontext = Mcontext; This.chatlist = chatlist;} @Overridepublic int GetCount () {LOG.I (TAG, Chatlist.size () + ""); return Chatlist.size ();} @Overridepublic Object getItem (int i) {return chatlist.get (i);} @Overridepublic long Getitemid (int i) {return i;} /** * Returns the number of ITEM layouts * * @return */@Overridepublic int getviewtypecount () {return item_total_count;} /** * Returns the corresponding item according to position * * @param position * @return */@Overridepublic int getitemviewtype (int position) {LOG.I (TAG , Chatlist.get (position). GetType () + ""); return Chatlist.get (position). GetType ();} @Overridepublic View GetView (int i, view view, ViewGroup viewgroup) {int type = Getitemviewtype (i); Chat chat = Chatlist.get (i); Viewholderleft viewholderleft = null; Viewholderright viewholderright = null; Viewholdertime viewholdertime = null; if (view = = null) {switch (type) {Case item_left:view = Layoutinflater.from (Mcontext). Inflate (r.layout.item_left, NULL); Viewholderleft = new Viewholderleft (); Viewholderleft.ivleftimage = (ImageView) View.findviewbyid (r.id.iv_left_image); Viewholderleft.tvlefttext = (TextView) View.findviewbyid (R.id.tv_left_text); ViewHolderLeft.ivLeftImage.setImageResource (R.drawable.icon); ViewHolderLeft.tvLeftText.setText (Chat.getvalue ()); View.settag (Viewholderleft); Break Case Item_time:view = Layoutinflater.from (Mcontext). Inflate (r.layout.item_center, NULL); Viewholdertime = new Viewholdertime (); Viewholdertime.tvtime = (TextView) View.findviewbyid (r.id.tv_time); ViewHolderTime.tvTime.setText (Chat.getvalue ()); View.settag (Viewholdertime); Break Case Item_right:view = Layoutinflater.from (Mcontext). Inflate (r.layout.item_right, NULL); Viewholderright = new Viewholderright (); Viewholderright.ivrightimage = (ImageView) View.findviewbyid (r.id.iv_right_image); Viewholderright.tvrighttext = (TextView) View.findviewbyid (R.id.tv_right_text); ViewHolderRight.ivRightImage.setImageResource (R.drawable.touxiang); ViewHolderRight.tvRightText.setText (Chat.getvalue ()); View.settag (Viewholderright); Break }} else {switch (type) {case item_left:viewholderleft = (viewholderleft) View.gett AG (); ViewHolderLeft.ivLeftImage.setImageResource (R.drawable.icon); ViewHolderLeft.tvLeftText.setText (Chat.getvalue ()); Break Case item_time:viewholdertime = (viewholdertime) view.gettag (); ViewHolderTime.tvTime.setText (Chat.getvalue ()); Break Case item_right:viewholderright = (viewholderright) view.gettag (); ViewHolderRight.ivRightImage.setImageResource (R.drawable.touxiang); ViewholdeRRight.tvRightText.setText (Chat.getvalue ()); Break }} return view;} private static Class Viewholderleft {//left display avatar ImageView ivleftimage; The text shown on the left TextView Tvlefttext;} private static Class Viewholdertime {//middle display time TextView tvtime;} private static Class Viewholderright {//On the right side of the Avatar ImageView ivrightimage; The text shown on the right TextView Tvrighttext;}}
Here is a simple definition of three types of item, showing a chat on the left, a time in the middle, and another chat on the right.
Middle Item Layout Code:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextView android:id="@+id/tv_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="4dp" android:text="shijian" android:textColor="#000000" android:textSize="12sp" /></RelativeLayout>
Left Item layout code:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " ><relativelayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_al Ignparentleft= "true" android:layout_marginright= "10DP" android:layout_margintop= "10DP" android:padding= "8DP" > <imageview android:id= "@+id/iv_left_image" android:layout_width= "40DP" android:layout_height= "4 0DP "android:layout_marginright=" 5DP "android:src=" @drawable/touxiang "/> <textview android: Id= "@+id/tv_left_text" android:textcolor= "#000000" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:layout_centervertical= "true" android:layout_torightof= "@id/iv_left_image" Android:text= "This is text" Android:textsizE= "14SP"/></relativelayout></relativelayout>
Right Item layout code:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " ><relativelayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_al Ignparentright= "true" android:layout_marginright= "10DP" android:layout_margintop= "10DP" android:padding= "8DP" > <textview android:id= "@+id/tv_right_text" android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:layout_centervertical= "true" android:layout_marginright= "5DP" Android:te Xt= "This is the text" android:textcolor= "#000000" android:textsize= "14sp"/> <imageview android:id= "@+id /iv_right_image "android:layout_width=" 40DP "android:layout_height=" 40DP "android:layout_torightof=" @ Id/tv_right_text "android:src=" @drawablE/icon "/></relativelayout></relativelayout>
Middle display time, left and right both sides are simple to display an avatar and a text.
Activity code:
Package Com.example.listviewdemo.activity;import Android.os.bundle;import android.support.annotation.Nullable; Import Android.support.v7.app.appcompatactivity;import Android.widget.listview;import Com.example.listviewdemo.chat;import Com.example.listviewdemo.r;import Com.example.listviewdemo.adapter.chatadapter;import java.util.arraylist;import java.util.List;/** * Created by Devin On 2016/7/11. */public class Moreitemactivity extends Appcompatactivity {private ListView lv_chart;private Chatadapter adapter; Private list<chat> chatlist; @Overrideprotected void OnCreate (@Nullable Bundle savedinstancestate) {Super.oncrea Te (savedinstancestate); Setcontentview (R.layout.activity_moreitem); Lv_chart = (ListView) Findviewbyid (R.id.lv_chart); Chatlist = new arraylist<> (); InitData (); adapter = new Chatadapter (Getapplicationcontext (), chatlist); Lv_chart.setadapter (adapter);} /** * Initialize data */private void InitData () {chat CHAT1 = new Chat (); Chat1.settype (ChatadApter. Item_time); Chat1.setvalue ("2016-7-11 16:39"); Chatlist.add (CHAT1); Chat chat2 = new Chat (); Chat2.settype (Chatadapter.item_left); Chat2.setvalue ("This is the first test data on the left"); Chatlist.add (CHAT2); Chat CHAT3 = new Chat (); Chat3.settype (Chatadapter.item_right); Chat3.setvalue ("This is the first test data on the right"); Chatlist.add (CHAT3); Chat chat4 = new Chat (); Chat4.settype (Chatadapter.item_time); Chat4.setvalue ("2016-7-11 16:40"); Chatlist.add (CHAT4); Chat chat5 = new Chat (); Chat5.settype (Chatadapter.item_left); Chat5.setvalue ("This is the second test data on the left"); Chatlist.add (CHAT5); Chat chat6 = new Chat (); Chat6.settype (Chatadapter.item_right); Chat6.setvalue ("This is the second Test data on the right"); Chatlist.add (CHAT6); Chat chat7 = new Chat (); Chat7.settype (Chatadapter.item_time); Chat7.setvalue ("2016-7-11 16:41"); Chatlist.add (CHAT7); Chat chat8 = new Chat (); Chat8.settype (Chatadapter.item_left); Chat8.setvalue ("This is the third Test data on the left"); Chatlist.add (CHAT8); Chat Chat9 = new Chat (); Chat9.settype (Chatadapter.item_right); Chat9.setvalue ("This is the third Test data on the right"); Chatlist.add (CHAT9); Chat chat10 = new Chat (); Chat10.settype (Chatadapter.item_left); Chat10.setvalue ("This is the fourth Test data on the left"); Chatlist.add (CHAT10); Chat chat11 = new Chat (); Chat11.settype (Chatadapter.item_right); Chat11.setvalue ("This is the fourth Test data on the right"); Chatlist.add (CHAT11); Chat chat12 = new Chat (); Chat12.settype (Chatadapter.item_left); Chat12.setvalue ("This is the fifth Test data on the left"); Chatlist.add (chat12); Chat chat13 = new Chat (); Chat13.settype (Chatadapter.item_right); Chat13.setvalue ("This is the fifth Test data on the right"); Chatlist.add (CHAT13); Chat chat14 = new Chat (); Chat14.settype (Chatadapter.item_right); Chat14.setvalue ("This is the sixth test data on the right"); Chatlist.add (chat14);}}
And there's an ordinary javabean.
package com.example.listviewdemo;/** * Created by Devin on 2016/7/11. */public class Chat {private int type;private String value;public Chat() {}public Chat(int type, String value) { this.type = type; this.value = value;}public int getType() { return type;}public void setType(int type) { this.type = type;}public String getValue() { return value;}public void setValue(String value) { this.value = value;}}
Finally realize:
The ListView Multi-layout implementation is mainly to rewrite two methods, determine which item is currently, and finally load a different content. The ListView study is over, let's introduce the GridView grid view.
Android Learning note 25 ListView Multi-layout implementation