Android学习笔记(34)--- 可下拉ListView(ExpandableListView)的使用

今天在网上看到ExpandableListView的效果,之前一直没发现。感觉挺好用的,借用下别人的例子。

主文件 .java

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

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

public class MainActivity extends ExpandableListActivity {  
    
    List<String> group;           //组列表  
    List<List<String>> child;     //子列表  
    ContactsInfoAdapter adapter;  //数据适配器  
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置为无标题  
        setContentView(R.layout.activity_main);          
        initializeData();  
        getExpandableListView().setAdapter(new ContactsInfoAdapter());   
        
        getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				System.out.println("groupPosition--"+groupPosition+"--childPosition--"+childPosition);
				return false;
			}
		});
    }  
      
    private void initializeData(){  
        group = new ArrayList<String>();  
        child = new ArrayList<List<String>>();  
          
        addIndex("Index00",new String[]{"item00","item01","item02"});  
        addIndex("Index01",new String[]{"item10","item11","item12"}); 
        addIndex("Index02",new String[]{"item20","item21","item22"});
    }  
      
    /**  
     * 模拟给组、子列表添加数据  
     * @param g-group  
     * @param c-child  
     */  
    private void addIndex(String g,String[] c){  
        group.add(g);  
        List<String> childitem = new ArrayList<String>();  
        for(int i=0;i<c.length;i++){  
            childitem.add(c[i]);  
        }  
        child.add(childitem);  
    }  
      
    class ContactsInfoAdapter extends BaseExpandableListAdapter{  
   
        @Override  
        public Object getChild(int groupPosition, int childPosition) {  
            return child.get(groupPosition).get(childPosition);  
        }  
          
        @Override  
        public long getChildId(int groupPosition, int childPosition) {  
            return childPosition;  
        }  
          
        @Override  
        public int getChildrenCount(int groupPosition) {  
            return child.get(groupPosition).size();  
        }  
          
        @Override  
        public View getChildView(int groupPosition, int childPosition,  
                boolean isLastChild, View convertView, ViewGroup parent) {  
            String string = child.get(groupPosition).get(childPosition);   
            return getGenericView(string);  
        }  
          
        @Override  
        public Object getGroup(int groupPosition) {  
            return group.get(groupPosition);  
        }                 
  
        @Override  
        public long getGroupId(int groupPosition) {  
            return groupPosition;  
        }     
          
        @Override  
        public int getGroupCount() {  
            return group.size();  
        }     
          
        @Override  
        public View getGroupView(int groupPosition, boolean isExpanded,  
                View convertView, ViewGroup parent) {  
            String string = group.get(groupPosition);    
            return getGenericView(string);  
        }  
  
        //创建组/子视图    
        public TextView getGenericView(String s) {    
  
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(    
                    ViewGroup.LayoutParams.FILL_PARENT, 40);  
            TextView text = new TextView(MainActivity.this);    
            text.setLayoutParams(lp);    
            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);    
            text.setPadding(36, 0, 0, 0);                 
            text.setText(s);    
            return text;    
        }    
          
          
        @Override  
        public boolean hasStableIds() {  
            // TODO Auto-generated method stub  
            return false;  
        }         
  
        @Override  
        public boolean isChildSelectable(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
            return true;  
        }  
          
    }  
}  

布局文件 .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="fill_parent"
    android:orientation="vertical" >
    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />
</LinearLayout>

效果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值