Android Nested ListView Example (refer to Implementing a friend Circle comment)

Source: Internet
Author: User

Recently in the project to use the ListView and then nested a ListView, two layers also have monitoring, there is no problem. In fact, the main solution inside that layer of the ListView height calculation can be, outside that layer of the ListView automatically calculated. Plus the inside that layer unfolds, is the height. This solution is often applied to comments in a circle of friends. Each one says, then there are comments below.

Project: http://download.csdn.net/detail/qq_16064871/9334993

1. Project Structure Chart



2. Realize


There are two levels of listening on the top, and the buttons are all on the outside. enough to diversify it.

3, Childlistview

Package Com.nest.view;import Android.content.context;import Android.util.attributeset;import Android.widget.listview;public class Childlistview extends ListView {public Childlistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO auto-generated constructor Stub}public Childli Stview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor Stub}public Childlistview (Context context) {super (context);//TODO auto-generated constructor stub} @Overridepublic void Onmeasure ( int widthmeasurespec, int heightmeasurespec) {int expandspec = Measurespec.makemeasurespec (integer.max_value >> 2 , measurespec.at_most); Super.onmeasure (Widthmeasurespec, Expandspec);}}

The calculation of the height of the inside ListView.

4, outside that layer of the ListView adapter Parentadapt

Package Com.nest.adapt;import Java.util.arraylist;import Java.util.list;import android.content.context;import Android.view.view;import Android.view.viewgroup;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.Button; Import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;import Com.nest.activity.r;import Com.nest.base.baseobjectlistadapter;import Com.nest.entity.entity;import com.nest.entity.showentity;/** * Parent ListView Adapter * @author MMSX * */public class Parentadapt extends Baseobjectlistadapter{p Rivate arraylist<showentity> mchildlist;public static int mparentitem = -1;public static Boolean Mbshowchild = False ;p ublic parentadapt (context context, list<? extends entity> datas) {Super (context, datas); InitData ();} private void InitData () {mchildlist = new arraylist<showentity> (); Mchildlist.clear (); for (int i = 0; i < 5; i++) {showentity tempentity = new Showentity ("Word title" + string.valueof (i), "content"); Mchildlist.add (tempentity);}} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Vholder = null;if (convertview = = null) {Vholder = new Viewholder (); Convertview = Minflater.inflate (R.layout.activity_main_list_item, NULL); Vholder.textviewtitle = (TextView) Convertview.findviewbyid (r.id.textview_1); vholder.textviewcontent= (TextView) Convertview.findviewbyid (r.id.textview_2); Vholder.listviewitem = (ListView) Convertview.findviewbyid ( R.id.listview_child) Vholder.buttonstake = (Button) Convertview.findviewbyid (r.id.button_1); ConvertView.setTag ( Vholder);} else {Vholder = (Viewholder) Convertview.gettag ();} Showentity tempentity = (showentity) mdatas.get (position); VHolder.textViewTitle.setText (Tempentity.gettitle ()); VHolder.textViewContent.setText (Tempentity.getcontent ());//Click on that pop-up, if it has popped, take back the listviewif (Mparentitem = = Position && mbshowchild) {//child ListView is actually loading data here childadapt tempadapt = new ChIldadapt (Mcontext, mchildlist); VHolder.listViewItem.setAdapter (TEMPADAPT); VHolder.listViewItem.setVisibility ( view.visible)///Sub-listview VHolder.listViewItem.setOnItemClickListener (New Onitemclicklistener () {@ overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long id) {Toast.maketext (Mcontext, "Child Listview" + string.valueof (position), Toast.length_short). Show ();}}); else {vHolder.listViewItem.setVisibility (view.gone);} VHolder.buttonStake.setOnClickListener (New Parentbuttonlisener ());//Remember that the button event VHolder.buttonStake.setTag (    position); return Convertview;} Class Viewholder{textview Textviewtitle; TextView textviewcontent; ListView ListViewItem; Button Buttonstake;} The monitor for the button of the parent ListView private class Parentbuttonlisener implements onclicklistener{@Overridepublic void OnClick (View V {Integer nposition = (integer) (V.gettag ()); Toast.maketext (Mcontext, "hint" + string.valueof (Nposition.intvalue ()), Toast.length_short). Show ();}}

5, inside that layer of the ListView adapter Childadapt

Package Com.nest.adapt;import Java.util.list;import Android.content.context;import android.view.view;import Android.view.viewgroup;import Android.widget.textview;import Com.nest.activity.r;import Com.nest.base.baseobjectlistadapter;import Com.nest.entity.entity;import com.nest.entity.showentity;/** * Child ListView Adapter * @author MMSX * */public class Childadapt extends Baseobjectlistadapter{public childadapt (context context, L ist<? Extends entity> datas) {Super (context, datas);} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Vholder = null;if (convertview = = null) {Vholder = new Viewholder (); Convertview = Minflater.inflate (r.layout.activity_main_list_item_1, NULL); Vholder.textviewtitle = (TextView) Convertview.findviewbyid (r.id.textview_1); vholder.textviewcontent= (TextView) Convertview.findviewbyid (r.id.textview_2); Convertview.settag (Vholder);} else {Vholder = (Viewholder) Convertview.gettag ();} Showentity tempentity = (showentity) mdatas.get (position); VHolder.textViewTitle.setText (Tempentity.gettitle ()); VHolder.textViewContent.setText ( Tempentity.getcontent ()); return Convertview;} Class Viewholder{textview Textviewtitle; TextView textviewcontent;}}

6. Implement Call Mainactivity

Package Com.nest.activity;import Java.util.arraylist;import Com.nest.adapt.parentadapt;import Com.nest.entity.showentity;import Android.os.bundle;import Android.view.view;import Android.widget.AdapterView; Import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import android.app.Activity /** * ListView Nested ListView Implementation Effect * @author MMSX * */public class Mainactivity extends Activity {private parentadapt Mparentad Apt;private arraylist<showentity> mparentlist; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); InitView ();} private void InitData () {mparentlist = new arraylist<showentity> (); Mparentlist.clear (); for (int i = 0; i <; i+ +) {showentity tempentity = new Showentity ("title" + string.valueof (i), "content"); Mparentlist.add (tempentity);}} private void Initview () {ListView ListView = (ListView) Findviewbyid (r.id.listview1); mparentadapt = new Parentadapt (this , MParentlist); Listview.setadapter (MPARENTADAPT); Listview.setonitemclicklistener (new Adaptitemclick ());// Long press the ListView will not write a demonstration of//listview.setonitemlongclicklistener (listener);} The Click event of the parent ListView listens to the private class Adaptitemclick implements onitemclicklistener{@Overridepublic void Onitemclick ( Adapterview<?> Parent, view view, int Position,long ID) {if (Parentadapt.mparentitem = = Position && Parentad Apt.mbshowchild) {parentadapt.mbshowchild = false;} else {parentadapt.mbshowchild = true;} Parentadapt.mparentitem = Position;mparentadapt.notifydatasetchanged ();}}}

the layout is not affixed, there are several. There is a need to download the project source code.

Project: http://download.csdn.net/detail/qq_16064871/9334993

Android Nested ListView Example (refer to Implementing a friend Circle comment)

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.