欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android ExpandableListView伸缩列表

程序员文章站 2022-06-03 12:50:19
...

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

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

	@Override
	public Object getChild(int parentPosition, int childPosition) {
		return attrMap.get(productList.get(parentPosition).getProductId());
	}

	@Override
	public long getChildId(int parentPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public 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;
	}

	@Override
	public int getChildrenCount(int parentPosition) {
		return attrMap.get(productList.get(parentPosition).getProductId()).size();
	}

	@Override
	public Object getGroup(int parentPosition) {
		return productList.get(parentPosition);
	}

	@Override
	public int getGroupCount() {
		return productList.size();
	}

	@Override
	public long getGroupId(int parentPosition) {
		return parentPosition;
	}

	@Override
	public 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;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public 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

  • android  ExpandableListView伸缩列表
            
    
    博客分类: java编程android javaandroidExpandableListView 
  • 大小: 26.8 KB