1. 程式人生 > >Android ListView巢狀ListView的實現方式

Android ListView巢狀ListView的實現方式

首先剛到北京一個月,產品經理讓做一個類似於商城的東東,起初感覺沒什麼難度,(不就一個電子商務app嘛,以前也做過啊),但是當看到有需求是這樣的

然後就開始做,起初太懶了,就在網上找,找到了一個ListView巢狀ListView的一哥們的講解的大致思路的,然後根據那哥們的思路自己寫了一個demo,感覺效果還挺好,不卡,

第一種實現方式:這種方式有個問題就像我專案中的問題,子列中的值如果是加減變化的,對應的每個父類的item的總價格會動態變化的話用此方式就會出現一定的問題,如果不需要實現像我專案圖中的實現的方式的話可以考慮該方法,上個圖看看

主要點就是:設定父類和子類的ListView的寬高都為fill_parent,這樣ListView在滾動的時候就不會重新計算高度,保證滑動的流暢性

咱先看看ListView巢狀ListView應該怎樣實現這個功能:

首先我儲存了一段json陣列,便於實現功能:

{
flag: true,
message: "10000123",
result: [
{
createtime: "2015-08-27 10:00:00",
ddaddress: "東北旺南路28號",
ddarea: "東城區",
ddcity: "北京市",
ddhouseId: 133,
ddhouseName: "北京主庫房",
ddoperator: "陳文東",
ddoperatorphone: "13810095764",
ddprovince: "",
faddress: "北京市東城區東北旺南路28號",
id: 10000129,
orderDetailVoList: [
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK220 無線光電鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",
goodssum: 30,
id: 201508261807445660,
price: 90,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK220 無線光電鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",
goodssum: 30,
id: 201508261227116540,
price: 90,
totalmoney: 2700
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK120 鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/40fda34a-aa06-430f-bcb8-e2f24854bd7f.jpg",
goodssum: 50,
id: 201508261747296800,
price: 69,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "TP-LINK TL-WR886N 450M無線路由器",
goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",
goodssum: 50,
id: 201508261747293800,
price: 89,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech) B175 商用無線滑鼠",
goodspicurl: "http://static.dingdong.com.cn/img/201a96a1-dac7-4100-bdc3-5d2a64e14138.jpg",
goodssum: 50,
id: 201508261805522020,
price: 59,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "TP-LINK TL-WR886N 450M無線路由器",
goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",
goodssum: 50,
id: 201508261227115650,
price: 89,
totalmoney: 4450
}
],
purchasemoney: 13380.04,
type: 2
},
{
createtime: "2015-08-27 10:00:00",
ddaddress: "東北旺南路28號",
ddarea: "東城區",
ddcity: "北京市",
ddhouseId: 133,
ddhouseName: "北京主庫房",
ddoperator: "陳文東",
ddoperatorphone: "13810095764",
ddprovince: "",
faddress: "北京市東城區東北旺南路28號",
id: 10000128,
orderDetailVoList: [
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "騰達(Tenda)N318無線路由器穿牆王300MN318無線路由器穿牆王300MN318無線路由器穿牆王300M",
goodspicurl: "http://static.dingdong.com.cn/img/44f384cb-a934-482a-8524-24aa6d34c0e7.jpg",
goodssum: 50,
id: 201508261227115870,
price: 44,
totalmoney: 2200
}
],
purchasemoney: 2200,
type: 2
},
{
createtime: "2015-08-27 10:00:00",
ddaddress: "東北旺南路28號",
ddarea: "東城區",
ddcity: "北京市",
ddhouseId: 133,
ddhouseName: "北京主庫房",
ddoperator: "陳文東",
ddoperatorphone: "13810095764",
ddprovince: "",
faddress: "北京市東城區東北旺南路28號",
id: 10000123,
orderDetailVoList: [
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK220 無線光電鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",
goodssum: 30,
id: 201508261807445660,
price: 90,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "TP-LINK TL-WDR5600 900M無線路由器",
goodspicurl: "http://static.dingdong.com.cn/img/948fc3f3-bcb0-4743-8727-06249fa497e4.jpg",
goodssum: 20,
id: 201508261548064700,
price: 139,
totalmoney: 2780
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK120 鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/40fda34a-aa06-430f-bcb8-e2f24854bd7f.jpg",
goodssum: 50,
id: 201508261420353150,
price: 69,
totalmoney: 3450
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK220 無線光電鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",
goodssum: 30,
id: 201508261227116540,
price: 90,
totalmoney: 2700
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech)MK120 鍵鼠套裝",
goodspicurl: "http://static.dingdong.com.cn/img/40fda34a-aa06-430f-bcb8-e2f24854bd7f.jpg",
goodssum: 50,
id: 201508261747296800,
price: 69,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "TP-LINK TL-WR886N 450M無線路由器",
goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",
goodssum: 50,
id: 201508261747293800,
price: 89,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "羅技(Logitech) B175 商用無線滑鼠",
goodspicurl: "http://static.dingdong.com.cn/img/201a96a1-dac7-4100-bdc3-5d2a64e14138.jpg",
goodssum: 50,
id: 201508261805522020,
price: 59,
totalmoney: 0.01
},
{
faddress: "北京市東城區東北旺南路28號",
goodsname: "TP-LINK TL-WR886N 450M無線路由器",
goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",
goodssum: 50,
id: 201508261227115650,
price: 89,
totalmoney: 4450
}
],
purchasemoney: 13380.04,
type: 2
}
],
state: true,
status: 1
}

接著我們需要一個Activity:

package com.zl.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.kjy.kjylistview.R;
public class QianTaoListviewActivity extends Activity {
	private ListView listView;
	private ArrayList<HashMap<String, Object>> parentList, childList;
	private ParentAdapter parentAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_qiantao);
		init();
	}

	private void init() {
		listView = (ListView) findViewById(R.id.qiantao_lv);

		getHttpMessage();
		

	}
	public void getHttpMessage(){
		GetMessageControl getMessageControl = new GetMessageControl(handler, getApplicationContext());
		try {
			getMessageControl.setHttpMsg();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			List<PurOrderBean> listBeans = (List<PurOrderBean>)msg.obj;
			parentAdapter = new ParentAdapter(listBeans, QianTaoListviewActivity.this);
			listView.setAdapter(parentAdapter);
		};
	};
}


然後我們需要解析儲存在res/raw中的介紹哦你字元:
package com.zl.listview;


import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;


import com.base.utils.HandlerParse;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.kjy.kjylistview.R;


import android.content.Context;
import android.os.Handler;
import android.os.Message;


/**
 * @author wangkai
 *
 */
public class GetMessageControl {
	Handler handler;
	private List<PurOrderBean> listBeans;
	private Context context;
	public GetMessageControl(Handler handler, Context context) {
		this.handler  = handler;
		this.context = context;
	}
	public void setHttpMsg() throws Exception{
		
		
		InputStream is = context.getResources().openRawResource(R.raw.json);
		byte[] buffer;
		buffer = new byte[is.available()];
		is.read(buffer);


		//將位元組陣列轉換為以GB2312編碼的字串
		String json = new String(buffer, "utf-8");
		
		Type listType = new TypeToken<LinkedList<PurOrderBean>>(){}.getType(); 
		Gson gson = new Gson(); 
		JsonParser jsonparer = new JsonParser();
		listBeans = gson.fromJson(jsonparer.parse(json.toString()).getAsJsonObject().get("result"), listType); 
		for (PurOrderBean bean : listBeans) {
			System.out.println("bean======" + bean);
		}
		Message msg = Message.obtain();
		msg.obj = listBeans;
		msg.what = HandlerParse.HTTP_SUCCESS;
		handler.sendMessage(msg);
		
		
	}
}


然後我們需要兩個Adapter:

首先是父類的Adapter:

package com.zl.listview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.kjy.kjylistview.R;

public class ParentAdapter extends BaseAdapter implements ListAdapter {
	private List<PurOrderBean> list;
	private Context context;
	private LayoutInflater inflater;
	private ChildAdapter daAdapter;

	public ParentAdapter(List<PurOrderBean> list, Context context) {
		super();
		this.list = list;
		this.context = context;
		daAdapter = new ChildAdapter(context);
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ChildListViewItem childListViewItem = null;
		if (convertView == null) {
			childListViewItem = new ChildListViewItem();
			convertView = inflater.inflate(R.layout.parentitem, null, false);
			childListViewItem.text_caigou = (TextView) convertView.findViewById(R.id.text_caigou);
			childListViewItem.child_item_img = (TextView) convertView.findViewById(R.id.text_caigou);
			childListViewItem.child_item_title = (TextView) convertView.findViewById(R.id.shouhuoren_text);
			childListViewItem.parent_lv = (ChildLiistView) convertView.findViewById(R.id.parent_lv);
			childListViewItem.callphone_text = (TextView) convertView.findViewById(R.id.callphone_text);
			childListViewItem.shouhuodizhi_text = (TextView) convertView.findViewById(R.id.shouhuodizhi_text);
			convertView.setTag(childListViewItem);
		} else {
			childListViewItem = (ChildListViewItem) convertView.getTag();
		}
		childListViewItem.text_caigou.setText(list.get(position).getId()+"");
		childListViewItem.child_item_title.setText(list.get(position).getDdoperator());
		childListViewItem.callphone_text.setText(list.get(position).getDdoperatorphone());
		
		String address = list.get(position).getDdprovince() + list.get(position).getDdcity() + list.get(position).getDdarea() + list.get(position).getDdaddress();
		childListViewItem.shouhuodizhi_text.setText(address);
		final Button bt_jz = (Button) convertView.findViewById(R.id.button_add);
		int z = ((List<ItemGoodsBean>)list.get(position).getBeanGoodsList()).size();
//		if (z <= 2) {
////			bt_jz.setVisibility(View.GONE);
			daAdapter.addAll(((List<ItemGoodsBean>)list.get(position).getBeanGoodsList()));
			childListViewItem.parent_lv.setAdapter(daAdapter);
		bt_jz.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				bt_jz.setVisibility(View.GONE);
				daAdapter.addAll(((List<ItemGoodsBean>) list.get(position).getBeanGoodsList()));

			}
		});

		childListViewItem.parent_lv.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						Intent intent = new Intent(context, Activity_2.class);
						context.startActivity(intent);

					}
				});
		return convertView;
	}

	public class ChildListViewItem {
		TextView text_caigou;
		TextView child_item_title, shouhuodizhi_text,
		callphone_text;
		TextView child_item_img;
		ChildLiistView parent_lv;
	}

}

其次是子類的Adapter:

package com.zl.listview;

import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eegets.peter.enclosure.network.bitmap.abitmap.AWonderBitmap;
import com.kjy.kjylistview.R;

public class ChildAdapter extends BaseAdapter {
	private List<ItemGoodsBean> list;
	private Context context;
	private LayoutInflater inflater;
	private AWonderBitmap aWonderBitmap;

	public ChildAdapter(Context context) {
		super();

		this.context = context;
		aWonderBitmap = AWonderBitmap.create(context);
		
	}
	public void addAll(List<ItemGoodsBean> list) {
		this.list=list;
		notifyDataSetChanged();
	}

	public void clearAll() {
		this.list.clear();
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ParentListItem parentListItem = null;
		if (convertView == null) {
			parentListItem = new ParentListItem();
		    inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.sssssss, null, false);
			parentListItem.exhibit_item_icon = (ImageView) convertView .findViewById(R.id.exhibit_item_icon);
			parentListItem.text_name = (TextView) convertView .findViewById(R.id.text_name);
			parentListItem.text_count = (TextView) convertView .findViewById(R.id.text_count);
			parentListItem.text_price = (TextView) convertView .findViewById(R.id.text_price);
			convertView.setTag(parentListItem);
		} else {
			parentListItem = (ParentListItem) convertView.getTag();
		}
		parentListItem.text_name.setText(list.get(position).getGoodsname());
		parentListItem.text_count.setText(list.get(position).getGoodssum()+"");
		parentListItem.text_price.setText(list.get(position).getPrice()+"");
		parentListItem.text_price.getPaint() .setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
		aWonderBitmap.display(parentListItem.exhibit_item_icon, list.get(position).getGoodspicurl());
		return convertView;
	}
	
		public class ParentListItem {
			ImageView exhibit_item_icon;
		TextView text_name, text_count, text_price;
	}

}

然後我們需要一個我們重寫的ListView,為的是設定Listview的固定高度:

package com.zl.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class ChildLiistView extends ListView {

	public ChildLiistView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ChildLiistView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public ChildLiistView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}

}

具體用到的一些jar包以及xml都在demo中,下載地址在最後~~~~~~~~~~~~~~~

具體的程式碼下載地址:http://download.csdn.net/detail/wangkai1101/9088931

第二種方法實現方式是重寫一個ListView來實現效果的,效果非常好,能實現任意方式。第二種方式近期也會上傳到csdn上~~~