您的位置:首页 > 其它

自定义ExpandableListView

2015-08-26 20:28 393 查看
可扩展的listview,类似于好友分组

已班级和学生为例

一,创建ExpandaListView布局,item_class,item_student布局

主布局

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


item_clazz

<?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:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textview_clazz_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp" />

    <TextView
        android:id="@+id/textview_clazz_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textview_clazz_student_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right" />

</LinearLayout>


item_student

<?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"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/textview_student_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="一班"
        />

    <TextView
        android:id="@+id/textview_student_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="201501"
        />

    <TextView
        android:id="@+id/textview_clazz_student_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:text="4"
        />
</LinearLayout>


二,创建moudel clazz和student

clazz:

package com.lingzhuo.myexpandlistview.moudel;

import java.util.List;

/**
 * Created by heinika on 2015/8/26.
 */
public class Clazz {
    private List<Student> students;
    private String clazzName;
    private String id;

    public Clazz(String clazzName, String id) {
        this.clazzName = clazzName;
        this.id = id;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public String getClazzName() {
        return clazzName;
    }

    public void setClazzName(String clazzName) {
        this.clazzName = clazzName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}


student:

package com.lingzhuo.myexpandlistview.moudel;

/**
 * Created by heinika on 2015/8/26.
 */
public class Student {
    private String name;
    private String age;
    private String sex;
    private String lable;

    public Student(String name, String age, String sex, String lable) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        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 getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getLable() {
        return lable;
    }

    public void setLable(String lable) {
        this.lable = lable;
    }
}


三,创建Adapter继承BaseExpandableListAdapter

这里没有做任何优化,若想优化要判断convertView是否为空

并添加内部类viewholder

package com.lingzhuo.myexpandlistview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.lingzhuo.myexpandlistview.R;
import com.lingzhuo.myexpandlistview.moudel.Clazz;
import com.lingzhuo.myexpandlistview.moudel.Student;

import java.util.List;

/**
 * Created by heinika on 2015/8/26.
 */
public class ClazzAdapter extends BaseExpandableListAdapter {
    private List<Clazz> mClazzs;
    private LayoutInflater mInflater;

    public ClazzAdapter(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 groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupPosition;
    }

    @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);
        Clazz clazz = mClazzs.get(groupPosition);
        TextView textViewClassName = (TextView) convertView.findViewById(R.id.textview_clazz_name);
        TextView textViewClassId = (TextView) convertView.findViewById(R.id.textview_clazz_id);
        TextView textViewClassStudentNum = (TextView) convertView.findViewById(R.id.textview_clazz_student_num);
        textViewClassName.setText(clazz.getClazzName());
        textViewClassId.setText(clazz.getId());
        textViewClassStudentNum.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);
        Student student = mClazzs.get(groupPosition).getStudents().get(childPosition);
        TextView textViewStudentName = (TextView) convertView.findViewById(R.id.textview_student_name);
        TextView textViewSdutentAge = (TextView) convertView.findViewById(R.id.textview_student_age);
        TextView textViewStudentSex = (TextView) convertView.findViewById(R.id.textview_clazz_student_sex);
        textViewStudentName.setText(student.getName());
        textViewSdutentAge.setText(student.getAge());
        textViewStudentSex.setText(student.getSex());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}


四,在activity中添加ExpandaListView.setAdapter

package com.lingzhuo.myexpandlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;

import com.lingzhuo.myexpandlistview.adapter.ClazzAdapter;
import com.lingzhuo.myexpandlistview.moudel.Clazz;
import com.lingzhuo.myexpandlistview.moudel.Student;

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

public class MainActivity extends Activity {
    private List<Clazz> clazzs;
    private ExpandableListView mExpandableListView;
    private LayoutInflater inflater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mExpandableListView = (ExpandableListView) findViewById(R.id.mExpandableListView);
        initClazzs();
        inflater = getLayoutInflater();
        ClazzAdapter adapter = new ClazzAdapter(clazzs,inflater);
        mExpandableListView.setAdapter(adapter);
    }

    private void initClazzs() {
        clazzs = new ArrayList<Clazz>();
        Clazz clazz1 = new Clazz("class1","201501");
        List<Student> students = new ArrayList<Student>();
        Student student0 = new Student("zhangsan","19","男","。。。。。。");
        Student student1 = new Student("zhangsan","19","男","。。。。。。");
        Student student2 = new Student("zhangsan","19","男","。。。。。。");
        Student student3 = new Student("zhangsan","19","男","。。。。。。");
        students.add(student0);
        students.add(student1);
        students.add(student2);
        students.add(student3);
        clazz1.setStudents(students);
        Clazz clazz2 = new Clazz("class2","201502");
        clazz2.setStudents(students);
        Clazz clazz3 = new Clazz("class3","201503");
        clazz3.setStudents(students);
        Clazz clazz4 = new Clazz("class4","201504");
        clazz4.setStudents(students);
        clazzs.add(clazz1);
        clazzs.add(clazz2);
        clazzs.add(clazz3);
        clazzs.add(clazz4);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: