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中的json字符:

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);
	}

}

然后是我们父类和子类的两个xml:

parentitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/rela_caigou"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/img_caigou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:background="@drawable/icon_order" />

        <TextView
            android:id="@+id/text_caigou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:lines="1"
            android:text="采购单:112312ssssss1"
            android:textSize="15dp" />

        <RelativeLayout
            android:id="@+id/rela_qianggou_time"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="right" >

            <ImageView
                android:id="@+id/img_qianggou_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/icon_clock" />

            <TextView
                android:id="@+id/text_qianggou_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/img_qianggou_time"
                android:text="20min后失效"
                android:textSize="13sp" />
        </RelativeLayout>
    </LinearLayout>

    <View
        android:id="@+id/view_line"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/rela_caigou"
        android:background="#BFBFBF" />

    <RelativeLayout
        android:id="@+id/rela_shouhuo_sss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_line"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" >

        <RelativeLayout
            android:id="@+id/rela_shouhuo_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/img_shouhuoren"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_people" />

            <TextView
                android:id="@+id/shouhuoren_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/img_shouhuoren"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@id/img_shouhuoren"
                android:textSize="13sp" />

            <ImageView
                android:id="@+id/img_callphone"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_below="@+id/shouhuoren_text"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_phone" />

            <TextView
                android:id="@+id/callphone_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/shouhuoren_text"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/img_callphone"
                android:textSize="13sp" />

            <ImageView
                android:id="@+id/img_shouhuoaddress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/callphone_text"
                android:layout_gravity="center_vertical"
                android:layout_margin="10dp"
                android:background="@drawable/icon_place" />

            <TextView
                android:id="@+id/shouhuodizhi_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/img_shouhuoaddress"
                android:layout_below="@id/callphone_text"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@id/img_shouhuoaddress"
                android:textSize="13sp" />
        </RelativeLayout>
        <!--
     <ImageView 
    android:id="@+id/shouhuoren_phone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:background="@drawable/shouhuoren_phone"
    />
        -->
    </RelativeLayout>

    <com.zl.listview.ChildLiistView
        android:id="@+id/parent_lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#e8e8e8"
        android:dividerHeight="1dp"
        android:fadingEdge="none"
        android:listSelector="@android:color/transparent"
        android:scrollbars="none"
        android:scrollingCache="false" >
    </com.zl.listview.ChildLiistView>

    <View
        android:id="@+id/view_line1"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/parent_lv"
        android:background="#BFBFBF" />

    <Button
        android:id="@+id/button_add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_line1"
        android:text="添加" />

</LinearLayout>

子类的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#f9f9f9"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/exhibit_item_icon"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:layout_centerVertical="true"
        android:layout_margin="10dip"
        android:background="@drawable/ic_launcher" />

    <RelativeLayout
        android:id="@+id/rel_right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/exhibit_item_icon" >

        <TextView
            android:id="@+id/text_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="2"
            android:text="轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧轻轻巧巧"
            android:textColor="@android:color/black"
            android:textSize="15dp" />

        <RelativeLayout
            android:id="@+id/rel_right_right"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_name"
            android:layout_marginTop="10dp" >

            <TextView
                android:id="@+id/text_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="5dp"
                android:scaleType="centerInside"
                android:text="×100" />

            <TextView
                android:id="@+id/text_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:paddingTop="5dp"
                android:text="¥12201"
                android:textSize="13dp" />
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>

具体用到的一些jar包以及xml都在demo中,下载地址在最后~~~~~~~~~~~~~~~

具体的代码下载地址:http://download.csdn.net/detail/wangkai1101/9088931

第二种方法实现方式是重写一个ListView来实现效果的,效果非常好,能实现任意方式。第二种方式近期也会上传到csdn上~~~

时间: 2024-10-11 10:48:25

Android ListView嵌套ListView的实现方式的相关文章

android ScrollView 嵌套 ListView 不让listView滑动

1.重写listview的onMeasure() /** * 解决listview高度显示不正确 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, exp

android:ScrollView嵌套ListView的问题

在ScrollView中嵌套使用ListView,看起来ListView只会显示一行多一点,不能滑动.ListView的高度怎么改都有问题,与预期不符合.搜索了一些解决方案,我觉得最好不要用这样的设计,因为默认情况下android 禁止在ScrollView中放入另外的ScrollView,它的高度是无法计算的. 方法一:设置完ListView的Adapter后,根据ListView的子项目重新计算ListView的高度,然后把高度再作为LayoutParams设置给ListView,这样它的高

一个listview嵌套listview的简单例子

在我刚刚看到一个页面是需要listview嵌套listview的时候  我内心是拒绝的, 这种设计也太傻逼了啊.. 但是不能退缩不是,其实也很简单,关键都在Adapter中 在最外层的listview ,讲第二层的listview当成一个简单的控件,比如textview啥的  你比较好理解的东西 写进viewholder中 比如: public static class ViewHolder { public TextView mTextView; //体检项目的分类 如:车身报告 public

ListView嵌套ListView优化

在做业务时候,一个ListView显示多种数据类型,我们想到的方法是ListView在嵌套一个ListView,对于子ListView 占父ListView的一行,就解决了问题,但是这样的逻辑是不是有点奇怪呢? 第一,父ListView的长度size需要加1,因为增加了一行放子listView public int getCount() { return datas.size()+1; } 第二,需要判断在什么地方插入子listView ,就要用到getViewTypeCount()和getIt

android PopupWindow嵌套ListView(模仿分类下拉菜单效果)

先看下UI效果 这就是使用PopupWindow嵌套ListView实现的,这个简单,不做多介绍直接上代码 1:布局文件 <span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" and

android _scrollview嵌套listview出现高度显示不全解决方案

只要在工具类里写上这一段代码:/** * scrollview嵌套listview显示不全解决 * @param listView */ public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int t

android ScrollView嵌套Listview

如果按照平常的设置,ScrollView里面的ListView是无法显示全部的,而且还不能滑动,从stackover flow搜的代码,亲测可以用! 1>给listView设置点击监视事件: ListView lv = (ListView) findViewById(R.id.layout_lv); lv.setOnTouchListener(new OnTouchListener() { // Setting on Touch Listener for handling the touch i

ScrollView+Fragment+ListView嵌套ListView,麻麻再也不用担心我不会写嵌套

前言 之前写了一篇文章 android ListView/GridView与ScrollView嵌套的滑动冲突解决 介绍了如何解决ScrollView与AdapterView的嵌套,但是没有给出demo,那是因为那些代码比较多,而且是在手头的项目里,业务逻辑代码比较多,所以第一时间没有分享给大家. 今天终于有空把这些代码全部抽离了出来,并且去掉了业务代码和不必要的内容,以便大家能够更好的学习这个知识. 先贴一个效果图吧: 之前项目里我是使用复写ScrollView的onInterceptTouc

ListView嵌套ListView时发生:View too large to fit into drawing cache的问题

可尝试在布局里设置ListView: android:scrollingCache="false" 或者在代码里设置: setScrollingCacheEnabled(false);