简单的学习使用ExpandableListview
一、Activity
我这里使用List<String[]>
,一般使用的是List<List<String>>
实现的是二级列表展示效果
package com.ahtelit.zbv.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.List;
public class Main4Activity extends AppCompatActivity {
private ExpandableListView expandableListView;
private String[] parents={"语言","爱好"};
private ArrayList<String[]> childs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
expandableListView=findViewById(R.id.expandale_lv);
childs=new ArrayList<String[]>();
childs.add(new String[]{"java","c","javascript"});
childs.add(new String[]{"写作","欣赏","玩游戏"});
expandableListView.setAdapter(new ExLVAdapter(this,childs,parents));
}
}
2、EXAdapter
需要一级层级的布局和二级层级的布局两份即可
package com.ahtelit.zbv.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/4/25.
* qzx
*/
public class ExLVAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String[]> childDatas;
private String[] parentDatas;
public ExLVAdapter(Context context, ArrayList<String[]> childList,String[] parentList){
mContext=context;
childDatas=childList;
parentDatas=parentList;
}
@Override
public int getGroupCount() {
return parentDatas.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return childDatas.size();
}
@Override
public Object getGroup(int groupPosition) {
return parentDatas[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childDatas.get(groupPosition)[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) {
View view= LayoutInflater.from(mContext).inflate(R.layout.left_item_layout,parent,false);
TextView tv=(TextView) view.findViewById(R.id.tv);
tv.setText(parentDatas[groupPosition]);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view= LayoutInflater.from(mContext).inflate(R.layout.left_item_layout,parent,false);
TextView tv=(TextView) view.findViewById(R.id.tv);
tv.setText(childDatas.get(groupPosition)[childPosition]);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
3、xml布局文件
1、Activity布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ahtelit.zbv.myapplication.Main4Activity">
<ExpandableListView
android:id="@+id/expandale_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</android.support.constraint.ConstraintLayout>
2、expandableListview布局
我一级布局和二级布局都一样的都是简单的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="wrap_content"
android:gravity="left">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="七子笑"
/>
</LinearLayout>