1.一、二级目录adapter
package com.example.testing.expand;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.testing.R;
import java.util.List;
/**
* @author Liushihua
* @date 2022-4-27 11:52
* @desc
*/
public class TopExpandAdapter extends BaseExpandableListAdapter {
private List<ExpandBean> dataList;
private Context context;
private onClickChildListener listener;
public interface onClickChildListener {
void onClickLevel2(int index1, int index2);
void onClickLevel3(int index1, int index2, int index3);
}
public void setListener(onClickChildListener listener) {
this.listener = listener;
}
private int clickGroupIndex = -1, clickChildIndex = -1;
public void setClickChild(int clickGroupIndex, int clickChildIndex) {
if (this.clickChildIndex != clickChildIndex || this.clickGroupIndex != clickGroupIndex) {
this.clickGroupIndex = clickGroupIndex;
this.clickChildIndex = clickChildIndex;
} else {
this.clickGroupIndex = -1;
this.clickChildIndex = -1;
}
notifyDataSetChanged();
}
public TopExpandAdapter(Context context, List<ExpandBean> dataList) {
this.dataList = dataList;
this.context = context;
}
@Override
public int getGroupCount() {
return dataList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return dataList.get(groupPosition).getSubBeans().size();
}
@Override
public ExpandBean getGroup(int groupPosition) {
return dataList.get(groupPosition);
}
@Override
public ExpandBean getChild(int groupPosition, int childPosition) {
return dataList.get(groupPosition).getSubBeans().get(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) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent, false);
}
TextView titleT = convertView.findViewById(R.id.titleT);
titleT.setText(getGroup(groupPosition).getTitle());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand, parent, false);
}
ExpandBean bean = getChild(groupPosition, childPosition);
ListView listView = convertView.findViewById(R.id.listView);
RelativeLayout titleBox = convertView.findViewById(R.id.titleBox);
TextView title2 = convertView.findViewById(R.id.title2);
title2.setText(bean.getTitle());
titleBox.setOnClickListener(v -> {
if (listener != null)
listener.onClickLevel2(groupPosition, childPosition);
if (bean.getSubBeans() != null && bean.getSubBeans().size() > 0) {
setClickChild(groupPosition, childPosition);
}
});
ListAdapter listAdapter = new ListAdapter(context,
R.layout.item_expand_three,
getChild(groupPosition, childPosition).getSubBeans());
listView.setAdapter(listAdapter);
listView.setOnItemClickListener((parent1, view, position, id) -> {
if (listener != null)
listener.onClickLevel3(groupPosition, childPosition, position);
});
int totalHeight = 0;
for (int k = 0; k < listAdapter.getCount(); k++) {//通过其适配器获取总高度
View mView = listAdapter.getView(k, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);//设置ListView的显示高度【外层是SrcollView,或
listView.setVisibility((groupPosition == clickGroupIndex && childPosition == clickChildIndex) ? View.VISIBLE : View.GONE);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
2.第三级目录adapter
package com.example.testing.expand;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.testing.R;
import java.util.List;
/**
* @author Liushihua
* @date 2022-4-27 11:42
* @desc
*/
public class ListAdapter extends ArrayAdapter<ExpandBean> {
public ListAdapter(@NonNull Context context, int resource, @NonNull List<ExpandBean> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertViewIn, @NonNull ViewGroup parent) {
if (convertViewIn == null) {
convertViewIn = LayoutInflater.from(getContext()).inflate(R.layout.item_expand_three, parent, false);
}
TextView text3 = convertViewIn.findViewById(R.id.text3);
text3.setText(getItem(position).getTitle());
return convertViewIn;
}
}
3.实体类
package com.example.testing.expand;
import java.util.List;
/**
* @author Liushihua
* @date 2022-4-26 15:23
* @desc
*/
public class ExpandBean {
private String title;
private List<ExpandBean> subBeans;
public ExpandBean(String title, List<ExpandBean> subBeans) {
this.title = title;
this.subBeans = subBeans;
}
public ExpandBean() {
}
public ExpandBean(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<ExpandBean> getSubBeans() {
return subBeans;
}
public void setSubBeans(List<ExpandBean> subBeans) {
this.subBeans = subBeans;
}
}
4.Activity调用
package com.example.testing;
import android.os.Bundle;
import android.widget.ExpandableListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.testing.expand.ExpandBean;
import com.example.testing.expand.TopExpandAdapter;
import com.example.testing.utils.L;
import java.util.ArrayList;
import java.util.List;
//com.example.test
public class TestActivity extends AppCompatActivity {
private ExpandableListView listView;
private List<ExpandBean> dataList;
private TopExpandAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initData();
initView();
}
private void initData() {
dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<ExpandBean> children2 = new ArrayList<>();
for (int j = 0; j < 3; j++) {
List<ExpandBean> children3 = new ArrayList<>();
for (int k = 0; k < 5; k++) {
children3.add(new ExpandBean("3级标题" + k));
}
children2.add(new ExpandBean("二级标题" + j, children3));
}
dataList.add(new ExpandBean("一级标题" + i, children2));
}
}
private int lastClickGroupIndex = -1;
private void initView() {
listView = findViewById(R.id.listView);
adapter = new TopExpandAdapter(this, dataList);
adapter.setListener(new TopExpandAdapter.onClickChildListener() {
@Override
public void onClickLevel2(int index1, int index2) {
ExpandBean expandBean = dataList.get(index1).getSubBeans().get(index2);
if (expandBean.getSubBeans() == null || expandBean.getSubBeans().size() == 0) {
//一级菜单无子菜单-直接加载二级菜单内容
L.d("onClickLevel2:" + expandBean.getTitle());
}
}
@Override
public void onClickLevel3(int index1, int index2, int index3) {
ExpandBean expandBean = dataList.get(index1).getSubBeans().get(index2).getSubBeans().get(index3);
//加载三级菜单内容
L.d("onClickLevel3:" + expandBean.getTitle());
}
});
listView.setOnGroupClickListener((parent, v, groupPosition, id) -> {
if (dataList.get(groupPosition).getSubBeans() == null || dataList.get(groupPosition).getSubBeans().size() == 0) {
//一级菜单无子菜单-直接加载一级菜单内容
}
return false;
});
listView.setAdapter(adapter);
listView.setOnGroupExpandListener(groupPosition -> {
if (lastClickGroupIndex != groupPosition && lastClickGroupIndex != -1) {
listView.collapseGroup(lastClickGroupIndex);
adapter.setClickChild(-1, -1);
}
lastClickGroupIndex = groupPosition;
});
listView.setOnGroupCollapseListener(groupPosition -> {
if (groupPosition == lastClickGroupIndex)
adapter.setClickChild(-1, -1);
});
}
}
5.资源文件 TestActivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".TestActivity">
<ExpandableListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="350px"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/titleBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50px"
android:textSize="32dp" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
item_expand.xml
<?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:orientation="vertical">
<RelativeLayout
android:id="@+id/titleBox"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10px">
<TextView
android:id="@+id/title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="100px" />
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="200px" />
</LinearLayout>
item_expand_three.xml
<?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:orientation="horizontal">
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
效果图:
|