ExpanableListView

自我對ExpanableListView的理解:

ExpanableListView界面如下圖所示:
這裏寫圖片描述

ExpanableListView是一個一個帶有下拉列表的一個View,點擊Item1,將會顯示Item1下的所有子列表。ExpanableListView中要用到兩個佈局,可以這樣理解,一個是父佈局,一個是子佈局。父佈局就是上面圖片中顯示的佈局,子佈局就是Item下的子列表的佈局。廢話不說,附上ExpanableListView學習的代碼:

首先是學習ExpanableListView時候的運行結果:

說明一點:這裏面的佈局是我經過精心設計後做出來的
這裏寫圖片描述

Layout文件夾中有三個xml文件:activity_main.xml、layout_myclass.xml、layout_student.xml

activity_main.xml中的代碼如下:

說明:activity_main.xml中使用自己創建的relativelayout佈局,裏面新建一個ExpanableListView,只指定了一下id。


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

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

    </ExpandableListView>

</RelativeLayout>

layout_myclass.xml中的代碼如下:

說明:整體採用水平方向的線性佈局,裏面添加三個TextView,後兩個又有一個統一的線性佈局,

效果圖如下圖:
這裏寫圖片描述

<?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="80dp"
    android:paddingBottom="25dp"
    android:paddingTop="25dp"
    android:paddingLeft="25dp"
    android:paddingRight="15dp"
    android:orientation="horizontal"
    android:gravity="center"
    >
    <TextView
        android:id="@+id/editText_className"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="班級名稱:"
        android:textSize="30sp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/editText_classNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="班級編號:"
            />
        <TextView
            android:id="@+id/editText_classStudent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="班級學生:"
            />


    </LinearLayout>


</LinearLayout>

layout_student.xml代碼如下:

說明:整體採用線性佈局,裏面添加三個TextView

效果如下圖:
這裏寫圖片描述

<?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="50dp"
    android:orientation="vertical"
    android:gravity="center"

    >

    <TextView
        android:id="@+id/editText_stuName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="學生姓名:"
        />
    <TextView
        android:id="@+id/editText_stuAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="學生年齡:"
        />
    <TextView
        android:id="@+id/editText_stuSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="學生性別:"
        />

</LinearLayout>

其次是java文件夾中的代碼

說明:在Java文件夾目錄如下圖所示:

這裏寫圖片描述

MyAdapter.java代碼如下

package com.example.administrator.myexpanablelistview.adapter;

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

import com.example.administrator.myexpanablelistview.R;
import com.example.administrator.myexpanablelistview.model.MyClass;
import com.example.administrator.myexpanablelistview.model.Student;

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

/**
 * Created by Administrator on 2015/8/26.
 */
public class MyAdapter extends BaseExpandableListAdapter {
    private List<MyClass> myClass;
    private LayoutInflater inflater;

    public MyAdapter(List<MyClass> myClass, LayoutInflater inflater) {
        this.myClass = myClass;
        this.inflater = inflater;
    }

    @Override
    public int getGroupCount() {
        return myClass.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return myClass.get(groupPosition).getClassStudent().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

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

    @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) {

        ViewHolder viewHolder=null;
        if (convertView==null)
        {
            convertView=inflater.inflate(R.layout.layout_myclass,null);
            viewHolder=new ViewHolder();
            viewHolder.textViewMyClassName = (TextView) convertView.findViewById(R.id.editText_className);
            viewHolder.textViewMyClassNumber = (TextView) convertView.findViewById(R.id.editText_classNumber);
            viewHolder.textViewMyClassStudent = (TextView) convertView.findViewById(R.id.editText_classStudent);
            convertView.setTag(viewHolder);

        }else
        {
            viewHolder=(ViewHolder)convertView.getTag();
        }
        MyClass classOne = myClass.get(groupPosition);
        viewHolder.textViewMyClassName.setText(classOne.getClassName());
        viewHolder.textViewMyClassNumber.setText("班級編號:"+classOne.getClassNumber());
        viewHolder.textViewMyClassStudent.setText("班級人數:"+classOne.getClassStudent().size());
        return convertView;
    }

    class ViewHolder{
        TextView textViewMyClassName;
        TextView textViewMyClassNumber;
        TextView textViewMyClassStudent;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ViewHolder2 viewHolder2=null;
        if(convertView==null)
        {
            convertView = inflater.inflate(R.layout.layout_student,null);
            viewHolder2=new ViewHolder2();
            viewHolder2.textViewStuName = (TextView) convertView.findViewById( R.id.editText_stuName);
            viewHolder2.textViewStuAge = (TextView) convertView.findViewById( R.id.editText_stuAge);
            viewHolder2.textViewStuSex = (TextView) convertView.findViewById( R.id.editText_stuSex);
            convertView.setTag(viewHolder2);
        }else
        {
            viewHolder2= (ViewHolder2) convertView.getTag();
        }
        MyClass clazz = myClass.get(groupPosition);
        List<Student> studentList=clazz.getClassStudent();
        Student student=studentList.get(childPosition);
        viewHolder2.textViewStuName.setText("姓名:"+student.getStuName());
        viewHolder2.textViewStuAge.setText("年齡:"+student.getStuAge());
        viewHolder2.textViewStuSex.setText("性別:"+student.getStuSex());
        return convertView;
    }
    class ViewHolder2{
        TextView textViewStuName;
        TextView textViewStuAge;
        TextView textViewStuSex;
    }


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

MyClass.java代碼如下

package com.example.administrator.myexpanablelistview.model;

import java.util.List;

/**
 * Created by Administrator on 2015/8/26.
 */
public class MyClass {
    private String className;
    private String classNumber;
    private List<Student> classStudent;

    public MyClass(String className, String classNumber) {
        this.className = className;
        this.classNumber = classNumber;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getClassNumber() {
        return classNumber;
    }

    public void setClassNumber(String classNumber) {
        this.classNumber = classNumber;
    }

    public List<Student> getClassStudent() {
        return classStudent;
    }

    public void setClassStudent(List<Student> classStudent) {
        this.classStudent = classStudent;
    }
}

Student.java代碼如下

package com.example.administrator.myexpanablelistview.model;

/**
 * Created by Administrator on 2015/8/26.
 */
public class Student {
    private  String stuName;
    private String stuAge;
    private String stuSex;

    public Student(String stuName, String stuAge, String stuSex) {
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.stuSex = stuSex;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuAge() {
        return stuAge;
    }

    public void setStuAge(String stuAge) {
        this.stuAge = stuAge;
    }

    public String getStuSex() {
        return stuSex;
    }

    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }
}

MainActivity.java代碼如下

package com.example.administrator.myexpanablelistview;

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

import com.example.administrator.myexpanablelistview.adapter.MyAdapter;
import com.example.administrator.myexpanablelistview.model.MyClass;
import com.example.administrator.myexpanablelistview.model.Student;

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


public class MainActivity extends Activity {
    private ExpandableListView expandableListView;
    private List<MyClass> myClass;
    private MyAdapter myAdapter;
    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expanablelistview_id);
        initmyClass();
        inflater=getLayoutInflater();
        myAdapter = new MyAdapter(myClass,inflater);

        expandableListView.setAdapter(myAdapter);
    }
    private List<MyClass> initmyClass() {
        myClass = new ArrayList<MyClass>();
        MyClass classOne = new MyClass("七年級一班", "20150701");
        List<Student> student_1 = new ArrayList<Student>();
        student_1.add(new Student("zhangsan","20","男"));
        student_1.add(new Student("lisi","21","女"));
        student_1.add( new Student("wangwu","20","男"));
        student_1.add(new Student("zhaoliu","21","女"));
        student_1.add(new Student("maqi","22","男"));
        classOne.setClassStudent(student_1);
        myClass.add(classOne);

        MyClass classTwo = new MyClass("七年級二班", "20150702");
        List<Student> student_2 = new ArrayList<Student>();
        student_2.add(new Student("zhangsan","20","男"));
        student_2.add(new Student("lisi","21","女"));
        student_2.add( new Student("wangwu","20","男"));
        student_2.add(new Student("zhaoliu","21","女"));
        student_2.add(new Student("maqi","22","男"));
        classTwo.setClassStudent(student_2);
        myClass.add(classTwo);

        MyClass classThree = new MyClass("八年級一班", "20150801");
        List<Student> student_3 = new ArrayList<Student>();
        student_3.add(new Student("zhangsan","20","男"));
        student_3.add(new Student("lisi","21","女"));
        student_3.add( new Student("wangwu","20","男"));
        student_3.add(new Student("zhaoliu","21","女"));
        student_3.add(new Student("maqi","22","男"));
        classThree.setClassStudent(student_3);
        myClass.add(classThree);

        MyClass classFour = new MyClass("八年級二班", "20150802");
        List<Student> student_4 = new ArrayList<Student>();
        student_4.add(new Student("zhangsan","20","男"));
        student_4.add(new Student("lisi","21","女"));
        student_4.add( new Student("wangwu","20","男"));
        student_4.add(new Student("zhaoliu","21","女"));
        student_4.add(new Student("maqi","22","男"));
        classFour.setClassStudent(student_4);
        myClass.add(classFour);


        MyClass classFive = new MyClass("九年級一班", "20150901");
        List<Student> student_5 = new ArrayList<Student>();
        student_5.add(new Student("zhangsan","20","男"));
        student_5.add(new Student("lisi","21","女"));
        student_5.add( new Student("wangwu","20","男"));
        student_5.add(new Student("zhaoliu","21","女"));
        student_5.add(new Student("maqi","22","男"));
        classFive.setClassStudent(student_5);
        myClass.add(classFive);

        MyClass classSix = new MyClass("九年級二班", "20150902");
        List<Student> student_6 = new ArrayList<Student>();
        student_6.add(new Student("zhangsan","20","男"));
        student_6.add(new Student("lisi","21","女"));
        student_6.add( new Student("wangwu","20","男"));
        student_6.add(new Student("zhaoliu","21","女"));
        student_6.add(new Student("maqi","22","男"));
        classSix.setClassStudent(student_6);
        myClass.add(classSix);


        return myClass;
    }

}

最後附上manifests中的代碼文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myexpanablelistview" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/china_flag"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

總結:
本項目中最重要的是要在MyAdapter.java中的getGroupView()和getChildView()這兩個方法,就是因爲ExpanableListView要用兩個佈局。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章