溫馨提示×

如何自定義ExpandableListView

小樊
82
2024-10-14 13:28:10
欄目: 編程語言

要自定義ExpandableListView,你可以按照以下步驟進(jìn)行:

  1. 創(chuàng)建布局文件:首先,你需要為ExpandableListView創(chuàng)建一個布局文件。這個文件將定義每個子項和組頭的布局。例如,你可以創(chuàng)建一個名為list_item_group.xml的文件來定義組頭的布局,以及一個名為list_item_child.xml的文件來定義子項的布局。
  2. 創(chuàng)建適配器:接下來,你需要創(chuàng)建一個適配器來填充和管理ExpandableListView的數(shù)據(jù)。你可以繼承BaseExpandableListAdapter類來實現(xiàn)自定義適配器。在適配器中,你需要實現(xiàn)getGroupViewgetChildView方法來分別返回組頭和子項的視圖。同時,你還需要實現(xiàn)getGroupId、getGroupCount、getChildrenCountgetChildIdhasStableIds等方法來管理組和子項的數(shù)據(jù)。
  3. 設(shè)置適配器:最后,你需要在代碼中將自定義適配器設(shè)置給ExpandableListView。你可以通過調(diào)用setAdapter方法來實現(xiàn)這一點。

下面是一個簡單的示例代碼,展示了如何自定義ExpandableListView

// 創(chuàng)建布局文件
// 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);

        // 初始化數(shù)據(jù)和適配器
        groupList = new ArrayList<>();
        childList = new ArrayList<>();

        // 添加組和子項數(shù)據(jù)
        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);

        // 設(shè)置適配器
        adapter = new CustomExpandableListAdapter(this, groupList, childList);
        expandableListView.setAdapter(adapter);
    }
}

以上示例代碼展示了如何創(chuàng)建自定義布局文件、自定義適配器和在Activity中使用自定義ExpandableListView。你可以根據(jù)自己的需求修改和擴(kuò)展這些代碼。

0