溫馨提示×

ExpandableListView的分組標題樣式

小樊
81
2024-10-14 13:40:12
欄目: 編程語言

ExpandableListView 的分組標題樣式可以通過自定義 GroupHeaderLayout 來實現(xiàn)。以下是一個簡單的示例,展示了如何自定義分組標題的樣式:

  1. 創(chuàng)建自定義的 GroupHeaderLayout
<?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"
    android:padding="8dp">

    <TextView
        android:id="@+id/group_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:textColor="@color/colorPrimary"
        android:textSize="16sp" />
</LinearLayout>

在這個示例中,我們創(chuàng)建了一個包含 TextView 的線性布局作為分組標題。你可以根據(jù)需要自定義 TextView 的樣式,例如設置文本顏色、大小、字體等。

  1. ExpandableListView 中使用自定義的 GroupHeaderLayout
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  1. 在代碼中設置分組標題
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private List<String> groupList;
    private Map<String, List<String>> childListMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView = findViewById(R.id.expandableListView);

        // 初始化數(shù)據(jù)
        groupList = new ArrayList<>();
        childListMap = new HashMap<>();

        // 添加分組
        groupList.add("Group 1");
        groupList.add("Group 2");
        groupList.add("Group 3");

        // 添加子項
        List<String> group1Items = new ArrayList<>();
        group1Items.add("Item 1.1");
        group1Items.add("Item 1.2");
        childListMap.put(groupList.get(0), group1Items);

        List<String> group2Items = new ArrayList<>();
        group2Items.add("Item 2.1");
        group2Items.add("Item 2.2");
        childListMap.put(groupList.get(1), group2Items);

        List<String> group3Items = new ArrayList<>();
        group3Items.add("Item 3.1");
        group3Items.add("Item 3.2");
        childListMap.put(groupList.get(2), group3Items);

        // 設置適配器
        expandableListView.setAdapter(new MyExpandableListAdapter(this, groupList, childListMap));
    }
}
  1. 創(chuàng)建自定義的 ExpandableListAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groupList;
    private Map<String, List<String>> childListMap;

    public MyExpandableListAdapter(Context context, List<String> groupList, Map<String, List<String>> childListMap) {
        this.context = context;
        this.groupList = groupList;
        this.childListMap = childListMap;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childListMap.get(groupList.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childListMap.get(groupList.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.group_header_layout, parent, false);
        }

        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(android.R.layout.simple_list_item_1, parent, false);
        }

        TextView childText = convertView.findViewById(android.R.id.text1);
        childText.setText(childListMap.get(groupList.get(groupPosition)).get(childPosition));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

在這個示例中,我們創(chuàng)建了一個自定義的 ExpandableListAdapter,并在 getGroupView 方法中設置了分組標題的布局。這樣,ExpandableListView 就會使用我們自定義的分組標題樣式了。

0