码迷,mamicode.com
首页 > 移动开发 > 详细

android 嵌套的listview示例(可参考实现朋友圈评论)

时间:2015-12-09 01:55:55      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:

最近在项目中用到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
	}

	@Override
	public 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);
		}
	}

	@Override
	public 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());
		
		//点击那个弹出那个,如果已经弹出就收回子listview
		if (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() {

				@Override
				public 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{
		@Override
		public 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);
	}

	@Override
	public 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;

	@Override
	protected 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{
		@Override
		public 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示例(可参考实现朋友圈评论)

标签:

原文地址:http://blog.csdn.net/qq_16064871/article/details/50223469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!