5-16
package com.example.expandablelistr;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
    ExpandableListView expandableListView;

    String[] groupStrings = {"西游记", "水浒传", "三国演义", "红楼梦"};
    String[][] childStrings = {
            {"唐三藏", "孙悟空", "猪八戒", "沙和尚"},
            {"宋江", "林冲", "李逵", "鲁智深"},
            {"曹操", "刘备", "孙权", "诸葛亮", "周瑜"},
            {"贾宝玉", "林黛玉", "薛宝钗", "王熙凤"}};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
                
        myadapter myadapter = new myadapter();
        expandableListView.setAdapter(myadapter);
        //点击组的监听
        expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
            
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplication(), groupStrings[groupPosition], 0).show();;
                return false;//返回false不然分组不会展开
            }
        });
        expandableListView.setOnChildClickListener(new OnChildClickListener() {
            
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplication(), childStrings[groupPosition][childPosition], 0).show();;
                return false;
            }
        });
        
        }
    class myadapter extends BaseExpandableListAdapter{
//分组的个数
        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return groupStrings.length;
        }
//获取指定分组中的子选项的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return childStrings[groupPosition].length;
        }
//获取指定的分组数据
        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return groupStrings[groupPosition];
        }
//获取指定分组中指定的子选项的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childStrings[groupPosition][childPosition];
        }
//获取指定分组的ID
        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }
//获取指定分组下的子选项ID
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }
//分组和子选项是否持有稳定的ID,也就是底层数据的改变会不会影响到他们
        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }
//获取指定分组的视图(isExpanded是否扩展)
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            GroupViewHolder groupViewHolder=null;
            if(convertView==null){
                convertView=View.inflate(MainActivity.this,R.layout.hzu, null);
                groupViewHolder=new GroupViewHolder();
                groupViewHolder.tvTitle=(TextView) convertView.findViewById(R.id.label_expand_group);
                convertView.setTag(groupViewHolder);
                
            }else{
                 groupViewHolder = (GroupViewHolder) convertView.getTag();
            }
            groupViewHolder.tvTitle.setText(groupStrings[groupPosition]);
            
            return convertView;
        }
//获取指定分组中的指定子选项的视图
        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ChildViewHolder childViewHolder=null;
            if(convertView==null){
                childViewHolder=new ChildViewHolder();
                convertView=View.inflate(MainActivity.this,R.layout.hzu2, null);
                childViewHolder.tvTitle=(TextView)convertView.findViewById(R.id.label_expand_group2);
                convertView.setTag(childViewHolder);//else这句话有用
            }else{
                childViewHolder = (ChildViewHolder) convertView.getTag();
            }
            Log.e("set", "scoss");
            childViewHolder.tvTitle.setText(childStrings[groupPosition][childPosition]);
            return convertView;
        }
//指定位置上的子元素是否可选中
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }

    
}
     class GroupViewHolder {
        TextView tvTitle;
    }
     class ChildViewHolder {
        TextView tvTitle;
}
}   

主xml:

<ExpandableListView 
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ></ExpandableListView>

在这里我用了两个Textview.xml
第一个是条目

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label_expand_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@android:color/holo_blue_light"
    android:paddingLeft="20dp"
    android:textColor="@android:color/white"
    android:textSize="30sp" 
/>

第二个子目录

<?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label_expand_group2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="#fff"
    android:background="@android:color/darker_gray"
    android:textSize="20sp" 
/>

阅读更多
想对作者说点什么? 我来说一句

ML5238锂电池管理源码

2018年01月25日 581KB 下载

figure matching update 5-16

2010年05月16日 138KB 下载

没有更多推荐了,返回首页

不良信息举报

5-16

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭