Android UI设计:ExpandableListView

来源:互联网 发布:阿里云招聘官网 编辑:程序博客网 时间:2024/05/21 09:30

ExpandableListView

ExpandableListView效果

ExpandableListView相当于ListView的嵌套,能够展开ListView中的每个Item。效果类似于扣扣界面。
这里写图片描述

ExpandableListView用法

1、在layout中添加ExpandableListView控件
2、创建clazz类
3、创建student类
4、创建Expenableadapter 继承BaseExpandableListAdapter

public class Expenableadapter extends BaseExpandableListAdapter{}

5、分别创建item_clazz布局和item_student布局
6、在主函数中进行数据初始化与调用

Layout

<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" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"    android:background="#5500ff00">    <ExpandableListView        android:id="@+id/expanablelistview"        android:layout_width="match_parent"        android:layout_height="match_parent"></ExpandableListView></RelativeLayout>

Clazz

public class Clazz {    private String clazzname;    private String clazznum;    private List<Student>  students;    public Clazz(String clazzname, String clazznum) {        this.clazzname = clazzname;        this.clazznum = clazznum;    }    public String getClazzname() {        return clazzname;    }    public void setClazzname(String clazzname) {        this.clazzname = clazzname;    }    public String getClazznum() {        return clazznum;    }    public void setClazznum(String clazznum) {        this.clazznum = clazznum;    }    public List<Student> getStudents() {        return students;    }    public void setStudents(List<Student> students) {        this.students = students;    }}

Student

public class Student {    private String name;    private String  age;    private String lable;    public Student(String name, String age, String lable) {        this.name = name;        this.age = age;        this.lable = lable;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getLable() {        return lable;    }    public void setLable(String lable) {        this.lable = lable;    }}

Expenableadapter

public class Expenableadapter extends BaseExpandableListAdapter {    private List<Clazz> mclazzs;    private LayoutInflater minflater;    public Expenableadapter(List<Clazz> mclazzs, LayoutInflater minflater) {        this.mclazzs = mclazzs;        this.minflater = minflater;    }    @Override    public int getGroupCount() {        return mclazzs.size();    }    @Override    public int getChildrenCount(int groupPosition) {        return mclazzs.get(groupPosition).getStudents().size();    }    @Override    public Object getGroup(int groupPosition) {        return mclazzs.get(groupPosition);    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return mclazzs.get(groupPosition).getStudents();    }    @Override    public long getGroupId(int groupPosition) {        return groupPosition;    }    @Override    public long getChildId(int groupPosition, int childPosition) {        return childPosition;    }    @Override    public boolean hasStableIds() {        return false;    }    @Override    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {        convertView=minflater.inflate(R.layout.item_clazz,null);        TextView textview_name= (TextView) convertView.findViewById(R.id.clazzname);        TextView textview_num= (TextView) convertView.findViewById(R.id.clazznum);        TextView textview_stu=(TextView) convertView.findViewById(R.id.clazzstu);        Clazz clazz=mclazzs.get(groupPosition);        textview_name.setText(clazz.getClazzname());        textview_num.setText(clazz.getClazznum());        textview_stu.setText(""+clazz.getStudents().size());        return convertView;}    @Override    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {        convertView=minflater.inflate(R.layout.item_student,null);        TextView textview_name= (TextView) convertView.findViewById(R.id.stuname);        TextView textview_age= (TextView) convertView.findViewById(R.id.stuage);        TextView textview_lable=(TextView) convertView.findViewById(R.id.stulable);        Clazz clazz=mclazzs.get(groupPosition);        Student student=clazz.getStudents().get(childPosition);        textview_name.setText(student.getName());        textview_age.setText(student.getAge());        textview_lable.setText(" ("+student.getLable()+")");        return convertView;    }    @Override    public boolean isChildSelectable(int groupPosition, int childPosition) {        return false;    }}

item_clazz

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/clazzname"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="班级名称"/>    <TextView        android:id="@+id/clazznum"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="班级号"/>    <TextView        android:id="@+id/clazzstu"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:text="学生"/></LinearLayout>

item_student

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/stuname"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="学生姓名"/>    <TextView        android:id="@+id/stuage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="学生年龄"/>    <TextView        android:id="@+id/stulable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="学生宣言"/></LinearLayout>

MainActivity

package com.grid.administrator.myexpenablelistview;import android.app.Activity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.widget.ExpandableListView;import java.util.ArrayList;import java.util.List;import modle.Clazz;import modle.Student;public class MainActivity extends Activity {   private ExpandableListView mElistView;    private Expenableadapter madapter;    private List<Clazz> mclazzs;    private LayoutInflater minflater;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mElistView= (ExpandableListView) findViewById(R.id.expanablelistview);        initClazz();        minflater=getLayoutInflater();        madapter=new Expenableadapter(mclazzs,minflater);        mElistView.setAdapter(madapter);    }    private void initClazz() {        mclazzs=new ArrayList<>();        Clazz clazz1=new Clazz("一班","201501");        List<Student> students01=new ArrayList<>();        students01.add(new Student("张三","19","生如夏花之绚烂"));        students01.add(new Student("李四","20","我为自己代言"));        students01.add(new Student("王五","21","世人笑我太疯癫,我笑世人看不穿"));        students01.add(new Student("赵六","22","赵六就是我,我就是赵六"));        clazz1.setStudents(students01);        Clazz clazz2=new Clazz("二班","201502");        List<Student> students02=new ArrayList<>();        students02.add(new Student("张三","19","生如夏花之绚烂"));        students02.add(new Student("李四","20","我为自己代言"));        students02.add(new Student("王五","21","世人笑我太疯癫,我笑世人看不穿"));        students02.add(new Student("赵六","22","赵六就是我,我就是赵六"));        clazz2.setStudents(students02);        Clazz clazz3=new Clazz("三班","201503");        List<Student> students03=new ArrayList<>();        students03.add(new Student("张三","19","生如夏花之绚烂"));        students03.add(new Student("李四","20","我为自己代言"));        students03.add(new Student("王五","21","世人笑我太疯癫,我笑世人看不穿"));        students03.add(new Student("赵六","22","赵六就是我,我就是赵六"));        clazz3.setStudents(students03);        Clazz clazz4=new Clazz("四班","201504");        List<Student> students04=new ArrayList<>();        students04.add(new Student("张三","19","生如夏花之绚烂"));        students04.add(new Student("李四","20","我为自己代言"));        students04.add(new Student("王五","21","世人笑我太疯癫,我笑世人看不穿"));        students04.add(new Student("赵六","22","赵六就是我,我就是赵六"));        clazz4.setStudents(students04);        clazz4.setStudents(students04);        mclazzs.add(clazz1);        mclazzs.add(clazz2);        mclazzs.add(clazz3);        mclazzs.add(clazz4);    }}

这里写图片描述

0 0