先展示一下效果图:
其中购物车布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<include layout="@layout/title_two_button_refsh"/>
<include android:id="@+id/shop_progress" layout="@layout/progress" />
<include android:id="@+id/shop_empty" layout="@layout/shop_car_empty" />
<ExpandableListView
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listAccount">
</ExpandableListView>
</LinearLayout>
ExpendableListView的adapter代码如下:
package com.jemsn.adapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.jemsn.android.MyShopCar;
import com.jemsn.android.R;
import com.jemsn.util.EoItems;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ShopCarExpandadapter extends BaseExpandableListAdapter {
private Context context;
// 准备一级列表中显示的数据
List<Map<String, String>> accountFather = new ArrayList<Map<String, String>>();
// 准备二级列表中显示的数据
List<List<EoItems>> accountChild = new ArrayList<List<EoItems>>();
public ShopCarExpandadapter(Context context,
List<Map<String, String>> accountFathers,
List<List<EoItems>> accountChilds) {
this.accountFather = accountFathers;
this.accountChild = accountChilds;
this.context = context;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view =inflater.inflate(R.layout.shopcar_listv_item, null);
}
//一级列表中显示的标题
TextView txtAddDate = (TextView) view.findViewById(R.id.txtAddDate);
TextView txtAddCount = (TextView) view.findViewById(R.id.txtAddCount);
Map<String, String> title_map=(Map<String, String>) getGroup(groupPosition);
txtAddDate.setText(title_map.get("AddDate"));
txtAddCount.setText(title_map.get("ItemCount"));
return view;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return accountFather.get(groupPosition);
}
public int getGroupCount() {
return accountFather.size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.shopcar_items_child, null);
}
EoItems temp_EoItems=(EoItems) getChild(groupPosition,childPosition);
final TextView txt_GdsName = (TextView) view
.findViewById(R.id.shopCar_GdsName);
final TextView txt_Ref = (TextView) view
.findViewById(R.id.shopCar_Ref);
final TextView txt_Price = (TextView) view
.findViewById(R.id.shopCar_Price);
final TextView txt_ZQua = (TextView) view
.findViewById(R.id.shopCar_ZQua);
final TextView txt_Qua = (TextView) view
.findViewById(R.id.shopCar_Qua);
final TextView txt_PtAmo = (TextView) view
.findViewById(R.id.shopCar_PtAmo);
txt_GdsName.setText(temp_EoItems.getGdsName());
txt_Ref.setText(temp_EoItems.getRef());
txt_Price.setText(temp_EoItems.getPrice());
txt_ZQua.setText(temp_EoItems.getZQua());
txt_Qua.setText(temp_EoItems.getQua());
txt_PtAmo.setText(temp_EoItems.getPtAmo());
return view;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return accountChild.get(groupPosition).get(childPosition);
}
public int getChildrenCount(int groupPosition) {
return accountChild.get(groupPosition).size();
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
最后,Activity的代码如下:
package com.jemsn.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.jemsn.adapter.ShopCarExpandadapter;
import com.jemsn.db.Query;
import com.jemsn.db.SystemFields;
import com.jemsn.util.EoItems;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
public class MyShopCar extends Activity {
public final static String FATHERACCOUNT = "fatherAccount";
public final static String CHILDACCOUNT = "childAccount";
public final static String BALANCEACCOUNT = "accountBalance";
Query shopQuery;
List<Map<String, String>> ListTitle;
List<List<EoItems>> ItemsList;
ShopCarExpandadapter adapter;
ExpandableListView listShopCar;
private View LoadProcessBar;
private View shopcar_empty;
private TextView tv_title;
private Button BackBtn;
private Button refreshBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myshopcar);
listShopCar = (ExpandableListView) findViewById(R.id.listAccount);
listShopCar.setDividerHeight(0);
shopQuery = new Query(getApplicationContext());
LoadProcessBar = (View) this.findViewById(R.id.shop_progress);
shopcar_empty = (View) this.findViewById(R.id.shop_empty);
tv_title = (TextView) this.findViewById(R.id.textView);
tv_title.setText(SystemFields.Cur_View_CUSTOMER_Name+"购物车");
BackBtn = (Button) this.findViewById(R.id.title_bt_back);
BackBtn.setText("返回");
refreshBtn = (Button) this.findViewById(R.id.title_bt_refresh);
BackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
refreshBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LoadProcessBar.setVisibility(View.VISIBLE);
new Thread(new loadRunnerable()).start();
}
});
new Thread(new loadRunnerable()).start();
}
class loadRunnerable implements Runnable {
public void run() {
doFatherCount();
doChildCount();
Post_handler.sendEmptyMessage(0);
}
}
final Handler Post_handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:// 加载完毕
LoadProcessBar.setVisibility(View.GONE);
if(ListTitle.size()>0){
shopcar_empty.setVisibility(View.GONE);
refreshData();
}else{
shopcar_empty.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
}
private void refreshData() {
adapter = new ShopCarExpandadapter(MyShopCar.this, ListTitle, ItemsList);
listShopCar.setAdapter(adapter);
// 去掉系统自带的按钮
listShopCar.setGroupIndicator(null);
int width = getWindowManager().getDefaultDisplay().getWidth();
listShopCar.setIndicatorBounds(width - 40, width - 10);
// 展开所有二级列表
int groupCount = adapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
listShopCar.expandGroup(i);
}
// 监听二级列表
listShopCar.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
return false;
}
});
}
};
private void doChildCount() {
ItemsList=new ArrayList<List<EoItems>>();
if (ListTitle.size() > 0) {
for (Map<String, String> titleMap : ListTitle) {
JSONArray itemsArry = shopQuery.Get_Local_JsonArry(null,
"AddDate=? AND CustomerID=?",
new String[] { titleMap.get("AddDate"),
SystemFields.Cur_View_CUSTOMER_ID }, "ItemsID",
"Temp_EoItems");
List<EoItems> temp_ListItem = new ArrayList<EoItems>();
buildEoitems(itemsArry, temp_ListItem);
ItemsList.add(temp_ListItem);
}
}
}
private void buildEoitems(JSONArray itemsArry, List<EoItems> temp_ListItem) {
if (itemsArry.length() > 0) {
for (int i = 0; i < itemsArry.length(); i++) {
EoItems arryItems = new EoItems();
try {
arryItems.setAddDate(itemsArry.optJSONObject(i).getString(
"AddDate"));
arryItems.setPtAmo(itemsArry.optJSONObject(i).getString(
"PtAmo"));
arryItems.setPrice(itemsArry.optJSONObject(i).getString(
"Price"));
arryItems.setQua(itemsArry.optJSONObject(i)
.getString("Qua"));
arryItems.setZQua(itemsArry.optJSONObject(i).getString(
"ZQua"));
arryItems.setRef(itemsArry.optJSONObject(i)
.getString("Ref"));
arryItems.setUnit(itemsArry.optJSONObject(i).getString(
"Unit"));
arryItems.setBarCode(itemsArry.optJSONObject(i).getString(
"BarCode"));
arryItems.setGdsName(itemsArry.optJSONObject(i).getString(
"GdsName"));
arryItems.setGdsCode(itemsArry.optJSONObject(i).getString(
"GdsCode"));
arryItems.setGoodsID(itemsArry.optJSONObject(i).getString(
"GoodsID"));
arryItems.setCustomerID(itemsArry.optJSONObject(i)
.getString("CustomerID"));
arryItems.setItemsID(itemsArry.optJSONObject(i).getString(
"ItemsID"));
temp_ListItem.add(arryItems);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void doFatherCount() {
ListTitle=new ArrayList<Map<String, String>>();
JSONArray fatherArry = shopQuery.Get_Local_JsonArry(
SystemFields.ShopCatDate_SQL,
new String[] { SystemFields.Cur_View_CUSTOMER_ID });
if (fatherArry.length() > 0) {
for (int i = 0; i < fatherArry.length(); i++) {
Map<String, String> itemTitleMap = new HashMap<String, String>();
try {
itemTitleMap.put("AddDate", fatherArry.optJSONObject(i)
.getString("AddDate"));
itemTitleMap.put("ItemCount", fatherArry.optJSONObject(i)
.getString("ItemCount"));
ListTitle.add(itemTitleMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
就这样:一个简单的expendableListView的用实例就出来了,当然我们还可以加很多修饰,使界面更加漂亮一些!