这两天做东西要用到伸缩菜单,倒腾了两天 终于出来了,分享一下:

1.布局文件shop_info_layout.xml:

<ExpandableListView android:id="@+id/shop_tests"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:scrollbars="none"           ></ExpandableListView>xml中声明一个ExpandableListView 

2.ExpandableListView 一级列表布局shop_product_item_layout.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="40dp"    android:gravity="center"    >    <TextView android:id="@+id/shop_product_name"    style="@style/TextStyle3"    android:gravity="center"    android:textColor="#000"    android:layout_marginLeft="5dp"    />        <TextView android:id="@+id/shop_yuan"        style="@style/TextStyle3"        android:layout_alignParentRight="true"        android:textColor="#000"        android:layout_marginRight="5dp"        android:layout_alignBaseline="@+id/shop_product_name"        android:text="元"/>        <TextView android:id="@+id/shop_productprice"        style="@style/TextStyle3"         android:textColor="#E96C14"         android:layout_alignBaseline="@+id/shop_product_name"         android:layout_toLeftOf="@+id/shop_yuan"        />        </RelativeLayout>

3.每个一级item下的二级信息布局shop_product_attr_layout.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:id="@+id/shop_attrlist"    android:orientation="horizontal"    >        <TextView android:id="@+id/shop_product_attrone"        style="@style/TextStyle3"        android:textColor="#000"        android:textSize="14sp"        android:layout_marginLeft="5dp"        />    <TextView android:id="@+id/shop_product_attrtwo"    android:textColor="#000"    style="@style/TextStyle3"    android:layout_marginLeft="15dp"     android:textSize="14sp"    /><TextView android:id="@+id/shop_product_attrprice"   android:textColor="#FD6F0C"   style="@style/TextStyle3"   android:layout_marginLeft="20dp"    /><TextView android:id="@+id/shop_product_yuan"    style="@style/TextStyle3"    android:textColor="#000"     android:textSize="14sp"    android:text="元"/><ImageView android:id="@+id/shop_product_attrcart"    android:layout_width="35dp"    android:layout_height="35dp"    android:background="@drawable/shop_purchase_cart"    android:layout_marginLeft="15dp"/></LinearLayout>

布局文件中用到的 style="@style/TextStyle3":

<style name="TextStyle3">        <item name="android:textSize">16sp</item>        <item name="android:textColor">#fff</item>        <item name="android:layout_width">wrap_content</item>        <item name="android:layout_height">wrap_content</item>    </style>

4.定义ExpandableAdapter:

import java.util.List;import java.util.Map;import com.zline.app.avtivity.R;import com.zline.app.entity.Product;import com.zline.app.entity.ProductAttr;import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;public class ExpandableAdapter extends BaseExpandableListAdapter{private Context context;private List<Product> productList;private Map<Integer, List<ProductAttr>> attrMap;private LayoutInflater layoutInflater;public ExpandableAdapter(Context context,List<Product> productList,Map<Integer, List<ProductAttr>> attrMap){this.attrMap = attrMap;this.productList = productList;this.context = context;}@Overridepublic Object getChild(int parentPosition, int childPosition) {return attrMap.get(productList.get(parentPosition).getProductId());}@Overridepublic long getChildId(int parentPosition, int childPosition) {return childPosition;}@Overridepublic View getChildView(int parentPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {Holder holder;if(convertView==null){layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);LinearLayout layout = (LinearLayout) layoutInflater.from(context).inflate(R.layout.shop_product_attr_layout, null);convertView = layout;holder = new Holder();holder.attrOne = (TextView) convertView.findViewById(R.id.shop_product_attrone);holder.attrOne.setText(attrMap.get(productList.get(parentPosition).getProductId()).get(childPosition).getDictOne());holder.attrTwo = (TextView) convertView.findViewById(R.id.shop_product_attrtwo);holder.attrTwo.setText(attrMap.get(productList.get(parentPosition).getProductId()).get(childPosition).getDictTwo());holder.price = (TextView) convertView.findViewById(R.id.shop_product_attrprice);holder.price.setText(attrMap.get(productList.get(parentPosition).getProductId()).get(childPosition).getPrice()+"");convertView.setTag(holder);}else{holder = (Holder) convertView.getTag();}return convertView;}@Overridepublic int getChildrenCount(int parentPosition) {return attrMap.get(productList.get(parentPosition).getProductId()).size();}@Overridepublic Object getGroup(int parentPosition) {return productList.get(parentPosition);}@Overridepublic int getGroupCount() {return productList.size();}@Overridepublic long getGroupId(int parentPosition) {return parentPosition;}@Overridepublic View getGroupView(int parentPosition, boolean isExpanded, View convertView, ViewGroup parent) {layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);RelativeLayout relativeLayout = (RelativeLayout) layoutInflater.inflate(R.layout.shop_product_item_layout, null);TextView productName = (TextView) relativeLayout.findViewById(R.id.shop_product_name);productName.setText(productList.get(parentPosition).getProductName());TextView priceRange = (TextView) relativeLayout.findViewById(R.id.shop_productprice);priceRange.setText(productList.get(parentPosition).getPriceRange());return relativeLayout;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic boolean isChildSelectable(int arg0, int arg1) {return true;}public boolean isEmpty() {return false;}public void onGroupExpanded(int groupPosition) {int a = getChildrenCount(groupPosition);if (a == 0) {Toast.makeText(context, "该组下没有数据!", Toast.LENGTH_LONG).show();}}public void onGroupCollapsed(int groupPosition) {Log.i("GFEDCBA", "我被折叠了!");} static class Holder{TextView attrOne;TextView attrTwo;TextView price;}

5.activity:

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.zline.app.entity.Product;import com.zline.app.entity.ProductAttr;import com.zline.app.myclass.shop.ExpandableAdapter;import com.zline.app.myclass.shop.GalleryAdapter;import android.os.Bundle;import android.view.Window;import android.widget.ExpandableListView;import android.widget.Gallery;import android.widget.ImageView;import android.widget.ListView;import android.widget.RatingBar;import android.widget.RatingBar.OnRatingBarChangeListener;import android.widget.TextView;import android.widget.Toast;import android.app.Activity;@SuppressWarnings("deprecation")public class ShopInfoActivity extends Activity {List<Product> products;Map<Integer, List<ProductAttr>> productAttrs;ExpandableListView expandableListView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.shop_info_layout);        createProduct();        createAttr();        initView();    }        //初始化各个组件   private void initView(){    expandableListView = (ExpandableListView) findViewById(R.id.shop_tests);   ExpandableAdapter expandableAdapter = new ExpandableAdapter(this, products, productAttrs);   expandableListView.setAdapter(expandableAdapter);   }          private List<Map<String, Object>> createAttrs(){   attrList = new ArrayList<Map<String,Object>>();   for(int i=0;i<2;i++){   Map<String, Object> m = new HashMap<String, Object>();   m.put(ConstantDef.PRODUTC_ATTR_ONE, "规格:半只");   m.put(ConstantDef.PRODUCT_ATTR_TWO, "口味:纯香");   m.put(ConstantDef.PRODUCT_ATTR_PRICE, 38.0);   attrList.add(m);   }   return attrList;   }*/      private List<Product> createProduct(){   products = new ArrayList<Product>();   for(int i=0;i<4;i++){   Product p = new Product();   p.setPriceRange("29-32");   p.setProductName("港式叉烧饭");   p.setProductId(i+1);   products.add(p);   }   return products;   }      private Map<Integer, List<ProductAttr>> createAttr(){   productAttrs = new HashMap<Integer, List<ProductAttr>>(); //  List<ProductAttr>    for(Product p:products){   List<ProductAttr> attrs = productAttrs.get(p.getProductId());   if(attrs == null){   attrs = new ArrayList<ProductAttr>();   ProductAttr attr = new ProductAttr();   attr.setDictOne("规格:半只");   attr.setDictTwo("风味:纯香");   attr.setPrice(28);   attr.setProductId(p.getProductId());   attr.setAttrId(products.indexOf(p));   attrs.add(attr);   attrs.add(attr);   productAttrs.put(p.getProductId(), attrs);   }else{   ProductAttr attr = new ProductAttr();   attr.setDictOne("规格:半只");   attr.setDictTwo("风味:纯香");   attr.setPrice(28);   attr.setProductId(p.getProductId());   attr.setAttrId(products.indexOf(p));   attrs.add(attr);   attrs.add(attr);   productAttrs.put(p.getProductId(), attrs);   }   }   return productAttrs;   }

效果看附件。

参考文献:http://blog.csdn.net/luck_apple/article/details/6742018

http://wenku.baidu.com/view/f6ec17265901020207409c36.html

http://blog.csdn.net/jianghuiquan/article/details/8350550

更多相关文章

  1. android表格布局
  2. andriod布局常用控件
  3. 关于相对布局RelativeLayout的各种属性
  4. android的TableLayout布局界面元素填满整个屏幕
  5. android流式布局热门标签的实现
  6. Android 自定义View及其在布局文件中的使用示例(三):结合Android
  7. 关于相对布局RelativeLayout的各种属性介绍

随机推荐

  1. android “Debug certificate expired”&
  2. Android学习笔记(3):Android项目结构分析
  3. Android 图片压缩实现过程代码
  4. android拨号器...
  5. Android的SDK与源代码
  6. androidのemail学习
  7. Android String类型转换为float、double
  8. Android网格布局的简单使用
  9. Android(安卓)访问 Tomcat SSL双向验证服
  10. Android常见小问题汇总