![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiIXZ05WZD9CX5RXa2Fmcn9CXwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwVPJR1T1UkaiBnSXFmNk1mYohWblZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39jM2EDNykDM1ETOyYDM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
使用ExpandableListView不需要依賴任何架構直接使用即可。
首先,搭個簡單的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.zzb.expandablelistview.MainActivity">
<ExpandableListView
android:id="@+id/edb_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
接下來執行個體化,然後寫個擴充卡,難點:主要是擴充卡比listview的複雜了點,慢慢看看還是很容易弄明白的。
public class MainActivity extends AppCompatActivity {
private MyExpandableListViewAdapter adapter;
private ExpandableListView listview;
private String[] parentList = new String[]{"水果", "蔬菜", "肉類"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ExpandableListView) findViewById(R.id.edb_lv);
adapter = new MyExpandableListViewAdapter(this,parentList);
initialData();
listview.setAdapter(adapter);
listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//groupPosition和childPosition都是從0開始
Toast.makeText(MainActivity.this, "點選了第"+(groupPosition+1)+"項的第"+(childPosition+1)+"個", Toast.LENGTH_SHORT).show();
return true;
}
});
}
private void initialData() {
adapter.childrenList1.add("蘋果");
adapter.childrenList1.add("西瓜");
adapter.childrenList1.add("香蕉");
adapter.childrenList2.add("青菜");
adapter.childrenList2.add("黃瓜");
adapter.childrenList2.add("豆苗");
adapter.childrenList3.add("雞");
adapter.childrenList3.add("鴨");
adapter.childrenList3.add("魚");
}
}
然後是擴充卡部分,擴充卡要繼承BaseExpandableListAdapter來寫
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
public Map<String, List<String>> dataset = new HashMap<>();
public List<String> childrenList1 = new ArrayList<>();
public List<String> childrenList2 = new ArrayList<>();
public List<String> childrenList3 = new ArrayList<>();
private Context context;
private LayoutInflater inflater;
private String[] parentList;
private static final String TAG = "TAG";
public MyExpandableListViewAdapter(Context context,String[] parentList) {
this.context = context;
this.parentList = parentList;
dataset.put(parentList[0], childrenList1);
dataset.put(parentList[1], childrenList2);
dataset.put(parentList[2], childrenList3);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// 獲得某個父項的某個子項
@Override
public Object getChild(int parentPos, int childPos) {
return dataset.get(parentList[parentPos]).get(childPos);
}
// 獲得父項的數量
@Override
public int getGroupCount() {
return dataset.size();
}
// 獲得某個父項的子項數目
@Override
public int getChildrenCount(int parentPos) {
return dataset.get(parentList[parentPos]).size();
}
// 獲得某個父項
@Override
public Object getGroup(int parentPos) {
return dataset.get(parentList[parentPos]);
}
// 獲得某個父項的id
@Override
public long getGroupId(int parentPos) {
return parentPos;
}
// 獲得某個父項的某個子項的id
@Override
public long getChildId(int parentPos, int childPos) {
return childPos;
}
// 按函數的名字來了解應該是是否具有穩定的id,這個方法目前一直都是傳回false,沒有去改動過
@Override
public boolean hasStableIds() {
return false;
}
// 獲得父項顯示的view
@Override
public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.parent_item, null);
}
view.setTag(R.layout.parent_item, parentPos);
view.setTag(R.layout.child_item, -1);
TextView text = (TextView) view.findViewById(R.id.parent_title);
text.setText(parentList[parentPos]);
return view;
}
// 獲得子項顯示的view
@Override
public View getChildView(int parentPos, int childPos, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.child_item, null);
}
view.setTag(R.layout.parent_item, parentPos);
view.setTag(R.layout.child_item, childPos);
TextView text = (TextView) view.findViewById(R.id.child_title);
text.setText(dataset.get(parentList[parentPos]).get(childPos));
/*text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "點到了内置的textview", Toast.LENGTH_SHORT).show();
}
});*/
return view;
}
// 子項是否可選中,如果需要設定子項的點選事件,需要傳回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
父布局的item布局如下
<?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">
<TextView
android:id="@+id/parent_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:text="這是父item"
android:paddingLeft="30dp"
android:layout_margin="5dp"/>
</LinearLayout>
子布局的item也是一個簡單的文字
<?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">
<TextView
android:id="@+id/child_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/black"
android:text="這是子item"
android:layout_margin="5dp"/>
</LinearLayout>