標籤:
最近在項目中用到listview中再嵌套一個listview,兩層也有監聽,都沒有問題。其實,主要解決裡面那一層的listview的高度計算就可以,外面那一層listview自動計算。加上裡面那層展開的,就是高度。這種解決方案經常應用於朋友圈的評論。每一條說說,然後下面有評論。
項目:http://download.csdn.net/detail/qq_16064871/9334993
1、項目結構圖
2、實現
這上面的監聽兩層都有,以及外面的button都可以。夠多樣化吧。
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);}}
裡面listview的高度的計算。
4、外面那層listview的適配器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;/** * 父listview適配器 * @author mmsx * */public class ParentAdapt extends BaseObjectListAdapter{private ArrayList<ShowEntity> mChildList;public static int mParentItem = -1;public static boolean mbShowChild = false;public 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("字標題" + String.valueOf(i), "內容");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());//點擊那個彈出那個,如果已經彈出就收回子listviewif (mParentItem == position && mbShowChild) {//子listview實在這裡載入資料的ChildAdapt tempAdapt = new ChildAdapt(mContext, mChildList);vHolder.listViewItem.setAdapter(tempAdapt);vHolder.listViewItem.setVisibility(View.VISIBLE);//子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());//記住是那個button事件vHolder.buttonStake.setTag(position); return convertView;}class ViewHolder{TextView textViewTitle;TextView textViewContent;ListView listViewItem;Button buttonStake;}//父listview的button的監聽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、裡面那層listview的適配器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;/** * 子listview適配器 * @author mmsx * */public class ChildAdapt extends BaseObjectListAdapter{public ChildAdapt(Context context, List<? 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、實現調用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嵌套listview實現效果 * @author mmsx * */public class MainActivity extends Activity {private ParentAdapt mParentAdapt;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 < 20; i++) {ShowEntity tempEntity = new ShowEntity("標題" + String.valueOf(i), "內容");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());//長按listview就不寫示範了//listView.setOnItemLongClickListener(listener);}//父listview的單擊事件的監聽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();}}}
布局就不貼,有好幾個。有需要下載項目源碼。
項目:http://download.csdn.net/detail/qq_16064871/9334993
android 嵌套的listview樣本(可參考實現朋友圈評論)