要自定義ExpandableListView
,你可以按照以下步驟進行:
ExpandableListView
創建一個布局文件。這個文件將定義每個子項和組頭的布局。例如,你可以創建一個名為list_item_group.xml
的文件來定義組頭的布局,以及一個名為list_item_child.xml
的文件來定義子項的布局。ExpandableListView
的數據。你可以繼承BaseExpandableListAdapter
類來實現自定義適配器。在適配器中,你需要實現getGroupView
和getChildView
方法來分別返回組頭和子項的視圖。同時,你還需要實現getGroupId
、getGroupCount
、getChildrenCount
、getChildId
和hasStableIds
等方法來管理組和子項的數據。ExpandableListView
。你可以通過調用setAdapter
方法來實現這一點。下面是一個簡單的示例代碼,展示了如何自定義ExpandableListView
:
// 創建布局文件
// list_item_group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/group_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall" />
</LinearLayout>
// list_item_child.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="40dp">
<TextView
android:id="@+id/child_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall" />
</LinearLayout>
// 自定義適配器
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> groupList;
private List<List<String>> childList;
public CustomExpandableListAdapter(Context context, List<String> groupList, List<List<String>> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).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 true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_group, null);
}
TextView groupTitle = convertView.findViewById(R.id.group_title);
groupTitle.setText(groupList.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_child, null);
}
TextView childTitle = convertView.findViewById(R.id.child_title);
childTitle.setText(childList.get(groupPosition).get(childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
// 在Activity中使用自定義ExpandableListView
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private CustomExpandableListAdapter adapter;
private List<String> groupList;
private List<List<String>> childList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.expandable_list_view);
// 初始化數據和適配器
groupList = new ArrayList<>();
childList = new ArrayList<>();
// 添加組和子項數據
groupList.add("Group 1");
groupList.add("Group 2");
groupList.add("Group 3");
List<String> child1 = new ArrayList<>();
child1.add("Child 1.1");
child1.add("Child 1.2");
List<String> child2 = new ArrayList<>();
child2.add("Child 2.1");
child2.add("Child 2.2");
List<String> child3 = new ArrayList<>();
child3.add("Child 3.1");
child3.add("Child 3.2");
childList.add(child1);
childList.add(child2);
childList.add(child3);
// 設置適配器
adapter = new CustomExpandableListAdapter(this, groupList, childList);
expandableListView.setAdapter(adapter);
}
}
以上示例代碼展示了如何創建自定義布局文件、自定義適配器和在Activity中使用自定義ExpandableListView
。你可以根據自己的需求修改和擴展這些代碼。