ExpandableListView

如何自定义ExpandableListView

小樊
82
2024-10-14 13:28:10
栏目: 编程语言

要自定义ExpandableListView,你可以按照以下步骤进行:

  1. 创建布局文件:首先,你需要为ExpandableListView创建一个布局文件。这个文件将定义每个子项和组头的布局。例如,你可以创建一个名为list_item_group.xml的文件来定义组头的布局,以及一个名为list_item_child.xml的文件来定义子项的布局。
  2. 创建适配器:接下来,你需要创建一个适配器来填充和管理ExpandableListView的数据。你可以继承BaseExpandableListAdapter类来实现自定义适配器。在适配器中,你需要实现getGroupViewgetChildView方法来分别返回组头和子项的视图。同时,你还需要实现getGroupIdgetGroupCountgetChildrenCountgetChildIdhasStableIds等方法来管理组和子项的数据。
  3. 设置适配器:最后,你需要在代码中将自定义适配器设置给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。你可以根据自己的需求修改和扩展这些代码。

0
看了该问题的人还看了