ExpandableListView

来源:互联网 发布:java 开发 ssh 编辑:程序博客网 时间:2024/06/16 12:48
二级列表
public class MainActivity extends Activity {

    private ExpandableListView expandableListView;
    private String[] groups;
    private int[] groupImages;

    private String[][] childs;
    private int[][] childImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expandable);
        initData();
        HgzBaseAdapter hgzBaseAdapter = new HgzBaseAdapter();
        expandableListView.setAdapter(hgzBaseAdapter);
        //设置默认展开列表
        for (int i = 0; i < hgzBaseAdapter.getGroupCount();i++) {
            expandableListView.expandGroup(i);
        }
    }

    private void initData() {
        groups = new String[] { "布加迪", "兰博基尼", "玛莎拉蒂" };
        groupImages = new int[] { R.drawable.a, R.drawable.f, R.drawable.m, };
        childs = new String[][] { { "布加迪1", "布加迪2", "布加迪3", "布加迪4" },
                { "兰博基尼1", "兰博基尼2", "兰博基尼3", "兰博基尼4" },
                { "玛莎拉蒂1", "玛莎拉蒂2", "玛莎拉蒂3", "玛莎拉蒂4" } };
        childImages = new int[][] {
                { R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e },
                { R.drawable.g, R.drawable.h, R.drawable.k, R.drawable.l },
                { R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q } };
    }

    private class HgzBaseAdapter extends BaseExpandableListAdapter {
        // 得到分组的数量
        @Override
        public int getGroupCount() {
            return groups.length;
        }

        // 得到groupPosition这一组的子视图数量
        @Override
        public int getChildrenCount(int groupPosition) {
            return childs[groupPosition].length;
        }

        // 获取groupPosition分组对象
        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        // 得到groupPosition分组中的childPosition位置的子视图对象
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childs[childPosition];
        }

        // 得到组id(groupPosition)
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        // 获取groupPosition分组的子child的id(childPosition)
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        // 是否有固定的id
        @Override
        public boolean hasStableIds() {
            return false;
        }

        // 重点!!!!
        // 获取分组view

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,
                    R.layout.activity_expandable_group, null);
            ImageView group_image = (ImageView) view
                    .findViewById(R.id.group_image);
            TextView group_text = (TextView) view.findViewById(R.id.group_text);
            group_image.setImageResource(groupImages[groupPosition]);
            group_text.setText(groups[groupPosition]);
            return view;
        }

        // 重要!!!
        // 获取子view

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,
                    R.layout.activity_expandable_child, null);
            ImageView child_image = (ImageView) view
                    .findViewById(R.id.child_image);
            TextView child_text = (TextView) view.findViewById(R.id.child_text);
            child_image
                    .setImageResource(childImages[groupPosition][childPosition]);
            child_text.setText(childs[groupPosition][childPosition]);
            return view;
        }
        // 子view是否可以被选中
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
}

//布局文件

Activity的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ExpandableListView
        android:id="@+id/expandable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:groupIndicator="@null">
        
    </ExpandableListView>

</RelativeLayout>

GroupView的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/group_image"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <TextView
        android:id="@+id/group_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

</LinearLayout>

ChildView的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/child_image"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/child_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

</LinearLayout>