Examples of android nested listview (refer to implementing comments in the circle of friends)
A listview is nested in the listview recently used in the project. The two layers also have a listener, and there is no problem. In fact, it mainly solves the calculation of the height of the listview layer inside, And the listview layer outside is automatically calculated. In addition, the layer is expanded to the height. This solution is often applied to comments in the circle of friends. Each article has a comment.
1. Project Structure
2. Implementation
The above listeners both have two layers, and the button outside can be used. Diverse enough.
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 ChildListView(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);}}
Calculation of the height of the listview.
4. 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 {private ArrayList
MChildList; public static int mParentItem =-1; public static boolean mbShowChild = false; public ParentAdapt (Context context, List
Datas) {super (context, datas); initData ();} private void initData () {mChildList = new ArrayList
(); 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 the pop-up one. If the pop-up is exceeded, retrieve the sub listviewif (mParentItem = position & mbShowChild) {// The Sub-listview is actually the ChildAdapt tempAdapt = new ChildAdapt (mContext, mChildList); vHolder. listViewItem. setAdapter (tempAdapt); vHolder. listViewItem. setVisibility (View. VISIBLE); // click the sublistview to listen to 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 the button event vHolder. buttonStake. setTag (position); return convertView;} class ViewHolder {TextView textViewTitle; TextView textViewContent; ListView listViewItem; Button buttonStake ;} // listener of the button of the parent listview private class ParentButtonLisener implements OnClickListener {@ Overridepublic void onClick (View v) {Integer nPosition = (Integer) (v. getTag (); Toast. makeText (mContext, + String. valueOf (nPosition. intValue (), Toast. LENGTH_SHORT ). show ();}}}
5. the adapter ChildAdapt of the listview layer inside
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;/*** sublistview adapter * @ author mmsx **/public class ChildAdapt extends BaseObjectListAdapter {public ChildAdapt (Context context, List
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. 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 mParentAdapt; private ArrayList
MParentList; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initData (); initView ();} private void initData () {mParentList = new ArrayList
(); MParentList. clear (); for (int I = 0; I <20; 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 (); // If you press and hold the listview long, the model will not be written. // listView. setOnItemLongClickListener (listener);} // listener of the parent listview Click Event private class AdaptItemClick implements OnItemClickListener {@ Overridepublic void onItemClick (AdapterView
Parent, View view, int position, long id) {if (ParentAdapt. mParentItem = position & ParentAdapt. mbShowChild) {ParentAdapt. mbShowChild = false;} else {ParentAdapt. mbShowChild = true;} ParentAdapt. mParentItem = position; mParentAdapt. notifyDataSetChanged ();}}}
There are several la S. You need to download the project source code.