ExpandableListView基本实现—能运行

效果图:

 

一、activity代码 

package com.liudan.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.liudan.adapter.ExpandableAdapter;

public class ExpandableActivity extends Activity {
    private static final String TAG = "ExpandableActivity";
 
 private ExpandableListView expandableList;
 private List<String> groupArray; 
 private List<List<String>> childArray; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        expandableList = (ExpandableListView)findViewById(R.id.expandableList);
        initData();
        ExpandableAdapter expandableAdapter = new ExpandableAdapter(this);
        expandableList.setAdapter(expandableAdapter);
        expandableList.setOnChildClickListener(new OnChildClickListener(){

   public boolean onChildClick(ExpandableListView parent, View v,
     int groupPosition, int childPosition, long id) {
    Log.i(TAG, "ExpandableActivity----------------------groupPosition="+groupPosition);
    Log.i(TAG, "ExpandableActivity----------------------childPosition="+childPosition);
//    Intent intent = new Intent(ExpandableActivity.this,ChildActivity.class);
//    startActivity(intent);
    return false;
   }
         
        });
       
    }

 private void initData(){
     groupArray = new ArrayList<String>();
     childArray = new ArrayList<List<String>>(); 
     groupArray.add("第一行"); 
     groupArray.add("第二行"); 
     List<String> tempArray = new ArrayList<String>(); 
     tempArray.add("第一条"); 
     tempArray.add("第二条"); 
     tempArray.add("第三条"); 
     for(int index = 0; index <groupArray.size(); ++index)  { 
      childArray.add(tempArray); 
      } 
    }
 
 public List<String> getGroupArray() {
  return groupArray;
 }

 public List<List<String>> getChildArray() {
  return childArray;
 }
}

 

二、adapter代码

package com.liudan.adapter;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.liudan.activity.ExpandableActivity;

public class ExpandableAdapter extends BaseExpandableListAdapter  {
 
 private ExpandableActivity expandable;
 
 public ExpandableAdapter(ExpandableActivity expandable){
  this.expandable = expandable;
 }

 public int getGroupCount() {
  // TODO Auto-generated method stub
  return expandable.getGroupArray().size();
 }

 public int getChildrenCount(int groupPosition) {
  // TODO Auto-generated method stub
 return expandable.getChildArray().get(groupPosition).size();

 }

 public Object getGroup(int groupPosition) {
  // TODO Auto-generated method stub
  return expandable.getGroupArray().get(groupPosition);
 }

 public Object getChild(int groupPosition, int childPosition) {
  // TODO Auto-generated method stub
  return expandable.getChildArray().get(groupPosition).get(childPosition);
 }

 public long getGroupId(int groupPosition) {
  // TODO Auto-generated method stub
  return groupPosition;
 }

 public long getChildId(int groupPosition, int childPosition) {
  // TODO Auto-generated method stub
  return childPosition;
 }

 public boolean hasStableIds() {
  // TODO Auto-generated method stub
  return false;
 }

 public View getGroupView(int groupPosition, boolean isExpanded,
   View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
   String string = expandable.getGroupArray().get(groupPosition);
   return getGenericView(string); 
 }

 public View getChildView(int groupPosition, int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  String string = expandable.getChildArray().get(groupPosition).get(childPosition); 
  return getGenericView(string); 
 }

 public boolean isChildSelectable(int groupPosition, int childPosition) {
  // TODO Auto-generated method stub
  return true;
 }
 public TextView getGenericView(String string) 
 { 
  // Layout parameters for the ExpandableListView  
   AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( 
  ViewGroup.LayoutParams.FILL_PARENT, 64); 
  TextView text = new TextView(expandable); 
  text.setLayoutParams(layoutParams); 
  // Center the text vertically  
  text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
  // Set the text starting position  
  text.setPadding(36, 0, 0, 0); 
  text.setText(string); 
  return text; 
  
 }

}

 

三、xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <ExpandableListView android:id="@+id/expandableList"
   android:layout_height="fill_parent" android:layout_width="fill_parent"
   android:cacheColorHint="#00000000"></ExpandableListView>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值