首先上图看效果
布局:此处我使用的是自定义的(因为我嵌套ScrollView)你可以直接使用ExpandableListView
android:id="@+id/expend_list"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="match_parent">
第一层item:
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/label_group_normal"
android:gravity="left|center_vertical"
android:layoutDirection="ltr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#faf4f4"
android:paddingBottom="8dp"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:textColor="#727272"
android:textSize="20sp"
tools:text="测试数据"/>
第二层itme
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/expand_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
tools:text="测试数据"/>
activity中的实例使用及监听
@BindView(R.id.expend_list)
NestedExpandaleListView expend_list;
ApplyExtendableListViewAdapter expandableAdapter;
expandableAdapter=new ApplyExtendableListViewAdapter();
expend_list.setAdapter(expandableAdapter); //设置分组的监听
// setListViewHeight(expend_list);
expend_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
toast_Base(""+groupPosition);
expandableAdapter.notifyDataSetChanged();
// setListViewHeight(expend_list);
return false;
}
}); //设置子项布局监听
expend_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
toast_Base(groupPosition+""+childPosition);
return true;
}
});
Adapter 适配器:
package adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.example.administrator.hospitalproject.R;
/**
* Created by Administrator on 2019/1/14.
*/
public class ApplyExtendableListViewAdapter extends BaseExpandableListAdapter {
private Context mcontext;
public String[] groupString = {"21", "23", "34", "45"};
public String[][] childString = { {"12", "23", "34", "45"}, {"56", "67", "78", "89"}, {"90", "87", "67", "43"}, {"32", "21", "34", "34"} };
@Override // 获取分组的个数
public int getGroupCount() {
return groupString.length;
} //获取指定分组中的子选项的个数
@Override
public int getChildrenCount(int groupPosition) {
return childString[groupPosition].length;
} // 获取指定的分组数据
@Override public Object getGroup(int groupPosition) {
return groupString[groupPosition];
} //获取指定分组中的指定子选项数据
@Override
public Object getChild(int groupPosition, int childPosition) {
return childString[groupPosition][childPosition];
} //获取指定分组的ID, 这个ID必须是唯一的
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
} //获取子选项的ID, 这个ID必须是唯一的
@Override public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
@Override public boolean hasStableIds() {
return true;
}
/**
*
* 获取显示指定组的视图对象
*
* @param groupPosition 组位置
* @param isExpanded 该组是展开状态还是伸缩状态
* @param convertView 重用已有的视图对象
* @param parent 返回的视图对象始终依附于的视图组
*/ // 获取显示指定分组的视图
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.partent_item,parent,false);
groupViewHolder = new GroupViewHolder();
groupViewHolder.tvTitle = (TextView)convertView.findViewById(R.id.label_group_normal);
convertView.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder)convertView.getTag();
}
groupViewHolder.tvTitle.setText(groupString[groupPosition]);
return convertView;
}
/**
*
* 获取一个视图对象,显示指定组中的指定子元素数据。
*
* @param groupPosition 组位置
* @param childPosition 子元素位置
* @param isLastChild 子元素是否处于组中的最后一个
* @param convertView 重用已有的视图(View)对象
* @param parent 返回的视图(View)对象始终依附于的视图组
* @return
* @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
* android.view.ViewGroup)
*/ //取得显示给定分组给定子位置的数据用的视图
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder;
if (convertView==null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_item,parent,false);
childViewHolder = new ChildViewHolder();
childViewHolder.tvTitle = (TextView)convertView.findViewById(R.id.expand_child);
convertView.setTag(childViewHolder);
}else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.tvTitle.setText(childString[groupPosition][childPosition]);
return convertView;
} //指定位置上的子元素是否可选中
@Override public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class GroupViewHolder {
TextView tvTitle;
}
static class ChildViewHolder {
TextView tvTitle;
}
}
相信大家能看得懂,个人看法笔记 如有错误欢迎留言斧正