Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android 嵌套的listview示例(可參考實現朋友圈評論)

android 嵌套的listview示例(可參考實現朋友圈評論)

編輯:關於android開發

android 嵌套的listview示例(可參考實現朋友圈評論)


最近在項目中用到listview中再嵌套一個listview,兩層也有監聽,都沒有問題。其實,主要解決裡面那一層的listview的高度計算就可以,外面那一層listview自動計算。加上裡面那層展開的,就是高度。這種解決方案經常應用於朋友圈的評論。每一條說說,然後下面有評論。

 

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 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(字標題 + 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 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 mParentList;

	@Override
	protected 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(標題 + 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();
		}
	}

}

布局就不貼,有好幾個。有需要下載項目源碼。

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved