listview嵌套listview

来源:互联网 发布:淘宝链接转微信二维码 编辑:程序博客网 时间:2024/06/02 04:42

第一种实现方式:这种方式有个问题就像我项目中的问题,子列中的值如果是加减变化的,对应的每个父类的item的总价格会动态变化的话用此方式就会出现一定的问题,如果不需要实现像我项目图中的实现的方式的话可以考虑该方法,上个图看看


主要点就是:设置父类和子类的ListView的宽高都为fill_parent,这样ListView在滚动的时候就不会重新计算高度,保证滑动的流畅性


咱先看看ListView嵌套ListView应该怎样实现这个功能:

首先我存储了一段json数组,便于实现功能:

[html] view plain copy
  1. {  
  2. flag: true,  
  3. message: "10000123",  
  4. result: [  
  5. {  
  6. createtime: "2015-08-27 10:00:00",  
  7. ddaddress: "东北旺南路28号",  
  8. ddarea: "东城区",  
  9. ddcity: "北京市",  
  10. ddhouseId: 133,  
  11. ddhouseName: "北京主库房",  
  12. ddoperator: "陈文东",  
  13. ddoperatorphone: "13810095764",  
  14. ddprovince: "",  
  15. faddress: "北京市东城区东北旺南路28号",  
  16. id: 10000129,  
  17. orderDetailVoList: [  
  18. {  
  19. faddress: "北京市东城区东北旺南路28号",  
  20. goodsname: "罗技(Logitech)MK220 无线光电键鼠套装",  
  21. goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",  
  22. goodssum: 30,  
  23. id: 201508261807445660,  
  24. price: 90,  
  25. totalmoney: 0.01  
  26. },  
  27. {  
  28. faddress: "北京市东城区东北旺南路28号",  
  29. goodsname: "罗技(Logitech)MK220 无线光电键鼠套装",  
  30. goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",  
  31. goodssum: 30,  
  32. id: 201508261227116540,  
  33. price: 90,  
  34. totalmoney: 2700  
  35. },  
  36. {  
  37. faddress: "北京市东城区东北旺南路28号",  
  38. goodsname: "罗技(Logitech)MK120 键鼠套装",  
  39. goodspicurl: "http://static.dingdong.com.cn/img/40fda34a-aa06-430f-bcb8-e2f24854bd7f.jpg",  
  40. goodssum: 50,  
  41. id: 201508261747296800,  
  42. price: 69,  
  43. totalmoney: 0.01  
  44. },  
  45. {  
  46. faddress: "北京市东城区东北旺南路28号",  
  47. goodsname: "TP-LINK TL-WR886N 450M无线路由器",  
  48. goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",  
  49. goodssum: 50,  
  50. id: 201508261747293800,  
  51. price: 89,  
  52. totalmoney: 0.01  
  53. },  
  54. {  
  55. faddress: "北京市东城区东北旺南路28号",  
  56. goodsname: "罗技(Logitech) B175 商用无线鼠标",  
  57. goodspicurl: "http://static.dingdong.com.cn/img/201a96a1-dac7-4100-bdc3-5d2a64e14138.jpg",  
  58. goodssum: 50,  
  59. id: 201508261805522020,  
  60. price: 59,  
  61. totalmoney: 0.01  
  62. },  
  63. {  
  64. faddress: "北京市东城区东北旺南路28号",  
  65. goodsname: "TP-LINK TL-WR886N 450M无线路由器",  
  66. goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",  
  67. goodssum: 50,  
  68. id: 201508261227115650,  
  69. price: 89,  
  70. totalmoney: 4450  
  71. }  
  72. ],  
  73. purchasemoney: 13380.04,  
  74. type: 2  
  75. },  
  76. {  
  77. createtime: "2015-08-27 10:00:00",  
  78. ddaddress: "东北旺南路28号",  
  79. ddarea: "东城区",  
  80. ddcity: "北京市",  
  81. ddhouseId: 133,  
  82. ddhouseName: "北京主库房",  
  83. ddoperator: "陈文东",  
  84. ddoperatorphone: "13810095764",  
  85. ddprovince: "",  
  86. faddress: "北京市东城区东北旺南路28号",  
  87. id: 10000128,  
  88. orderDetailVoList: [  
  89. {  
  90. faddress: "北京市东城区东北旺南路28号",  
  91. goodsname: "腾达(Tenda)N318无线路由器穿墙王300MN318无线路由器穿墙王300MN318无线路由器穿墙王300M",  
  92. goodspicurl: "http://static.dingdong.com.cn/img/44f384cb-a934-482a-8524-24aa6d34c0e7.jpg",  
  93. goodssum: 50,  
  94. id: 201508261227115870,  
  95. price: 44,  
  96. totalmoney: 2200  
  97. }  
  98. ],  
  99. purchasemoney: 2200,  
  100. type: 2  
  101. },  
  102. {  
  103. createtime: "2015-08-27 10:00:00",  
  104. ddaddress: "东北旺南路28号",  
  105. ddarea: "东城区",  
  106. ddcity: "北京市",  
  107. ddhouseId: 133,  
  108. ddhouseName: "北京主库房",  
  109. ddoperator: "陈文东",  
  110. ddoperatorphone: "13810095764",  
  111. ddprovince: "",  
  112. faddress: "北京市东城区东北旺南路28号",  
  113. id: 10000123,  
  114. orderDetailVoList: [  
  115. {  
  116. faddress: "北京市东城区东北旺南路28号",  
  117. goodsname: "罗技(Logitech)MK220 无线光电键鼠套装",  
  118. goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",  
  119. goodssum: 30,  
  120. id: 201508261807445660,  
  121. price: 90,  
  122. totalmoney: 0.01  
  123. },  
  124. {  
  125. faddress: "北京市东城区东北旺南路28号",  
  126. goodsname: "TP-LINK TL-WDR5600 900M无线路由器",  
  127. goodspicurl: "http://static.dingdong.com.cn/img/948fc3f3-bcb0-4743-8727-06249fa497e4.jpg",  
  128. goodssum: 20,  
  129. id: 201508261548064700,  
  130. price: 139,  
  131. totalmoney: 2780  
  132. },  
  133. {  
  134. faddress: "北京市东城区东北旺南路28号",  
  135. goodsname: "罗技(Logitech)MK120 键鼠套装",  
  136. goodspicurl: "http://static.dingdong.com.cn/img/40fda34a-aa06-430f-bcb8-e2f24854bd7f.jpg",  
  137. goodssum: 50,  
  138. id: 201508261420353150,  
  139. price: 69,  
  140. totalmoney: 3450  
  141. },  
  142. {  
  143. faddress: "北京市东城区东北旺南路28号",  
  144. goodsname: "罗技(Logitech)MK220 无线光电键鼠套装",  
  145. goodspicurl: "http://static.dingdong.com.cn/img/4e78376b-c465-4e91-98dd-afb57a71af13.jpg",  
  146. goodssum: 30,  
  147. id: 201508261227116540,  
  148. price: 90,  
  149. totalmoney: 2700  
  150. },  
  151. {  
  152. faddress: "北京市东城区东北旺南路28号",  
  153. goodsname: "罗技(Logitech)MK120 键鼠套装",  
  154. goodspicurl: "http://static.dingdong.com.cn/img/40fda34a-aa06-430f-bcb8-e2f24854bd7f.jpg",  
  155. goodssum: 50,  
  156. id: 201508261747296800,  
  157. price: 69,  
  158. totalmoney: 0.01  
  159. },  
  160. {  
  161. faddress: "北京市东城区东北旺南路28号",  
  162. goodsname: "TP-LINK TL-WR886N 450M无线路由器",  
  163. goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",  
  164. goodssum: 50,  
  165. id: 201508261747293800,  
  166. price: 89,  
  167. totalmoney: 0.01  
  168. },  
  169. {  
  170. faddress: "北京市东城区东北旺南路28号",  
  171. goodsname: "罗技(Logitech) B175 商用无线鼠标",  
  172. goodspicurl: "http://static.dingdong.com.cn/img/201a96a1-dac7-4100-bdc3-5d2a64e14138.jpg",  
  173. goodssum: 50,  
  174. id: 201508261805522020,  
  175. price: 59,  
  176. totalmoney: 0.01  
  177. },  
  178. {  
  179. faddress: "北京市东城区东北旺南路28号",  
  180. goodsname: "TP-LINK TL-WR886N 450M无线路由器",  
  181. goodspicurl: "http://static.dingdong.com.cn/img/6ec91287-fdc6-48e2-9730-8f432414640b.jpg",  
  182. goodssum: 50,  
  183. id: 201508261227115650,  
  184. price: 89,  
  185. totalmoney: 4450  
  186. }  
  187. ],  
  188. purchasemoney: 13380.04,  
  189. type: 2  
  190. }  
  191. ],  
  192. state: true,  
  193. status: 1  
  194. }  

接着我们需要一个Activity:

[java] view plain copy
  1. package com.zl.listview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.ListView;  
  15. import android.widget.Toast;  
  16.   
  17. import com.kjy.kjylistview.R;  
  18. public class QianTaoListviewActivity extends Activity {  
  19.     private ListView listView;  
  20.     private ArrayList<HashMap<String, Object>> parentList, childList;  
  21.     private ParentAdapter parentAdapter;  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         // TODO Auto-generated method stub  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_qiantao);  
  28.         init();  
  29.     }  
  30.   
  31.     private void init() {  
  32.         listView = (ListView) findViewById(R.id.qiantao_lv);  
  33.   
  34.         getHttpMessage();  
  35.           
  36.   
  37.     }  
  38.     public void getHttpMessage(){  
  39.         GetMessageControl getMessageControl = new GetMessageControl(handler, getApplicationContext());  
  40.         try {  
  41.             getMessageControl.setHttpMsg();  
  42.         } catch (Exception e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.       
  48.     Handler handler = new Handler(){  
  49.         public void handleMessage(android.os.Message msg) {  
  50.             List<PurOrderBean> listBeans = (List<PurOrderBean>)msg.obj;  
  51.             parentAdapter = new ParentAdapter(listBeans, QianTaoListviewActivity.this);  
  52.             listView.setAdapter(parentAdapter);  
  53.         };  
  54.     };  
  55. }  



然后我们需要解析存储在res/raw中的介绍哦你字符:

[java] view plain copy
  1. package com.zl.listview;  
  2.   
  3.   
  4. import java.io.InputStream;  
  5. import java.lang.reflect.Type;  
  6. import java.util.LinkedList;  
  7. import java.util.List;  
  8.   
  9.   
  10. import com.base.utils.HandlerParse;  
  11. import com.google.gson.Gson;  
  12. import com.google.gson.JsonParser;  
  13. import com.google.gson.reflect.TypeToken;  
  14. import com.kjy.kjylistview.R;  
  15.   
  16.   
  17. import android.content.Context;  
  18. import android.os.Handler;  
  19. import android.os.Message;  
  20.   
  21.   
  22. /** 
  23.  * @author wangkai 
  24.  * 
  25.  */  
  26. public class GetMessageControl {  
  27.     Handler handler;  
  28.     private List<PurOrderBean> listBeans;  
  29.     private Context context;  
  30.     public GetMessageControl(Handler handler, Context context) {  
  31.         this.handler  = handler;  
  32.         this.context = context;  
  33.     }  
  34.     public void setHttpMsg() throws Exception{  
  35.           
  36.           
  37.         InputStream is = context.getResources().openRawResource(R.raw.json);  
  38.         byte[] buffer;  
  39.         buffer = new byte[is.available()];  
  40.         is.read(buffer);  
  41.   
  42.   
  43.         //将字节数组转换为以GB2312编码的字符串  
  44.         String json = new String(buffer, "utf-8");  
  45.           
  46.         Type listType = new TypeToken<LinkedList<PurOrderBean>>(){}.getType();   
  47.         Gson gson = new Gson();   
  48.         JsonParser jsonparer = new JsonParser();  
  49.         listBeans = gson.fromJson(jsonparer.parse(json.toString()).getAsJsonObject().get("result"), listType);   
  50.         for (PurOrderBean bean : listBeans) {  
  51.             System.out.println("bean======" + bean);  
  52.         }  
  53.         Message msg = Message.obtain();  
  54.         msg.obj = listBeans;  
  55.         msg.what = HandlerParse.HTTP_SUCCESS;  
  56.         handler.sendMessage(msg);  
  57.           
  58.           
  59.     }  
  60. }  


然后我们需要两个Adapter:

首先是父类的Adapter:

[java] view plain copy
  1. package com.zl.listview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.os.Handler;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.Button;  
  17. import android.widget.ListAdapter;  
  18. import android.widget.TextView;  
  19.   
  20. import com.kjy.kjylistview.R;  
  21.   
  22. public class ParentAdapter extends BaseAdapter implements ListAdapter {  
  23.     private List<PurOrderBean> list;  
  24.     private Context context;  
  25.     private LayoutInflater inflater;  
  26.     private ChildAdapter daAdapter;  
  27.   
  28.     public ParentAdapter(List<PurOrderBean> list, Context context) {  
  29.         super();  
  30.         this.list = list;  
  31.         this.context = context;  
  32.         daAdapter = new ChildAdapter(context);  
  33.         this.inflater = LayoutInflater.from(context);  
  34.     }  
  35.   
  36.     @Override  
  37.     public int getCount() {  
  38.         // TODO Auto-generated method stub  
  39.         return list.size();  
  40.     }  
  41.   
  42.     @Override  
  43.     public Object getItem(int position) {  
  44.         // TODO Auto-generated method stub  
  45.         return list.get(position);  
  46.     }  
  47.   
  48.     @Override  
  49.     public long getItemId(int position) {  
  50.         // TODO Auto-generated method stub  
  51.         return position;  
  52.     }  
  53.   
  54.     @Override  
  55.     public View getView(final int position, View convertView, ViewGroup parent) {  
  56.         ChildListViewItem childListViewItem = null;  
  57.         if (convertView == null) {  
  58.             childListViewItem = new ChildListViewItem();  
  59.             convertView = inflater.inflate(R.layout.parentitem, nullfalse);  
  60.             childListViewItem.text_caigou = (TextView) convertView.findViewById(R.id.text_caigou);  
  61.             childListViewItem.child_item_img = (TextView) convertView.findViewById(R.id.text_caigou);  
  62.             childListViewItem.child_item_title = (TextView) convertView.findViewById(R.id.shouhuoren_text);  
  63.             childListViewItem.parent_lv = (ChildLiistView) convertView.findViewById(R.id.parent_lv);  
  64.             childListViewItem.callphone_text = (TextView) convertView.findViewById(R.id.callphone_text);  
  65.             childListViewItem.shouhuodizhi_text = (TextView) convertView.findViewById(R.id.shouhuodizhi_text);  
  66.             convertView.setTag(childListViewItem);  
  67.         } else {  
  68.             childListViewItem = (ChildListViewItem) convertView.getTag();  
  69.         }  
  70.         childListViewItem.text_caigou.setText(list.get(position).getId()+"");  
  71.         childListViewItem.child_item_title.setText(list.get(position).getDdoperator());  
  72.         childListViewItem.callphone_text.setText(list.get(position).getDdoperatorphone());  
  73.           
  74.         String address = list.get(position).getDdprovince() + list.get(position).getDdcity() + list.get(position).getDdarea() + list.get(position).getDdaddress();  
  75.         childListViewItem.shouhuodizhi_text.setText(address);  
  76.         final Button bt_jz = (Button) convertView.findViewById(R.id.button_add);  
  77.         int z = ((List<ItemGoodsBean>)list.get(position).getBeanGoodsList()).size();  
  78. //      if (z <= 2) {  
  79. ////            bt_jz.setVisibility(View.GONE);  
  80.             daAdapter.addAll(((List<ItemGoodsBean>)list.get(position).getBeanGoodsList()));  
  81.             childListViewItem.parent_lv.setAdapter(daAdapter);  
  82.         bt_jz.setOnClickListener(new OnClickListener() {  
  83.             @Override  
  84.             public void onClick(View v) {  
  85.                 bt_jz.setVisibility(View.GONE);  
  86.                 daAdapter.addAll(((List<ItemGoodsBean>) list.get(position).getBeanGoodsList()));  
  87.   
  88.             }  
  89.         });  
  90.   
  91.         childListViewItem.parent_lv.setOnItemClickListener(new OnItemClickListener() {  
  92.   
  93.                     @Override  
  94.                     public void onItemClick(AdapterView<?> arg0, View arg1,  
  95.                             int arg2, long arg3) {  
  96.                         Intent intent = new Intent(context, Activity_2.class);  
  97.                         context.startActivity(intent);  
  98.   
  99.                     }  
  100.                 });  
  101.         return convertView;  
  102.     }  
  103.   
  104.     public class ChildListViewItem {  
  105.         TextView text_caigou;  
  106.         TextView child_item_title, shouhuodizhi_text,  
  107.         callphone_text;  
  108.         TextView child_item_img;  
  109.         ChildLiistView parent_lv;  
  110.     }  
  111.   
  112. }  

其次是子类的Adapter:

[java] view plain copy
  1. package com.zl.listview;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.graphics.Paint;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.ImageView;  
  12. import android.widget.TextView;  
  13.   
  14. import com.eegets.peter.enclosure.network.bitmap.abitmap.AWonderBitmap;  
  15. import com.kjy.kjylistview.R;  
  16.   
  17. public class ChildAdapter extends BaseAdapter {  
  18.     private List<ItemGoodsBean> list;  
  19.     private Context context;  
  20.     private LayoutInflater inflater;  
  21.     private AWonderBitmap aWonderBitmap;  
  22.   
  23.     public ChildAdapter(Context context) {  
  24.         super();  
  25.   
  26.         this.context = context;  
  27.         aWonderBitmap = AWonderBitmap.create(context);  
  28.           
  29.     }  
  30.     public void addAll(List<ItemGoodsBean> list) {  
  31.         this.list=list;  
  32.         notifyDataSetChanged();  
  33.     }  
  34.   
  35.     public void clearAll() {  
  36.         this.list.clear();  
  37.         notifyDataSetChanged();  
  38.     }  
  39.     @Override  
  40.     public int getCount() {  
  41.         // TODO Auto-generated method stub  
  42.         return list.size();  
  43.     }  
  44.   
  45.     @Override  
  46.     public Object getItem(int position) {  
  47.         // TODO Auto-generated method stub  
  48.         return list.get(position);  
  49.     }  
  50.   
  51.     @Override  
  52.     public long getItemId(int position) {  
  53.         // TODO Auto-generated method stub  
  54.         return position;  
  55.     }  
  56.   
  57.     @Override  
  58.     public View getView(int position, View convertView, ViewGroup parent) {  
  59.         ParentListItem parentListItem = null;  
  60.         if (convertView == null) {  
  61.             parentListItem = new ParentListItem();  
  62.             inflater = LayoutInflater.from(context);  
  63.             convertView = inflater.inflate(R.layout.sssssss, nullfalse);  
  64.             parentListItem.exhibit_item_icon = (ImageView) convertView .findViewById(R.id.exhibit_item_icon);  
  65.             parentListItem.text_name = (TextView) convertView .findViewById(R.id.text_name);  
  66.             parentListItem.text_count = (TextView) convertView .findViewById(R.id.text_count);  
  67.             parentListItem.text_price = (TextView) convertView .findViewById(R.id.text_price);  
  68.             convertView.setTag(parentListItem);  
  69.         } else {  
  70.             parentListItem = (ParentListItem) convertView.getTag();  
  71.         }  
  72.         parentListItem.text_name.setText(list.get(position).getGoodsname());  
  73.         parentListItem.text_count.setText(list.get(position).getGoodssum()+"");  
  74.         parentListItem.text_price.setText(list.get(position).getPrice()+"");  
  75.         parentListItem.text_price.getPaint() .setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);  
  76.         aWonderBitmap.display(parentListItem.exhibit_item_icon, list.get(position).getGoodspicurl());  
  77.         return convertView;  
  78.     }  
  79.       
  80.         public class ParentListItem {  
  81.             ImageView exhibit_item_icon;  
  82.         TextView text_name, text_count, text_price;  
  83.     }  
  84.   
  85. }  

然后我们需要一个我们重写的ListView,为的是设置Listview的固定高度:

[java] view plain copy
  1. package com.zl.listview;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.ListView;  
  6.   
  7. public class ChildLiistView extends ListView {  
  8.   
  9.     public ChildLiistView(Context context) {  
  10.         super(context);  
  11.         // TODO Auto-generated constructor stub  
  12.     }  
  13.   
  14.     public ChildLiistView(Context context, AttributeSet attrs, int defStyle) {  
  15.         super(context, attrs, defStyle);  
  16.         // TODO Auto-generated constructor stub  
  17.     }  
  18.   
  19.     public ChildLiistView(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         // TODO Auto-generated constructor stub  
  22.     }  
  23.   
  24.     @Override  
  25.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  26.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  27.                 MeasureSpec.AT_MOST);  
  28.         super.onMeasure(widthMeasureSpec, expandSpec);  
  29.     }  
  30.   
  31. }  


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


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



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

原创粉丝点击