91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

ExpandableListView的分組標題樣式

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

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

  1. 創建自定義的 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>

在這個示例中,我們創建了一個包含 TextView 的線性布局作為分組標題。你可以根據需要自定義 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);

        // 初始化數據
        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. 創建自定義的 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;
    }
}

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

0
河津市| 伊春市| 巴南区| 竹溪县| 渝中区| 景洪市| 淅川县| 邢台市| 和平县| 赤峰市| 嘉禾县| 达拉特旗| 志丹县| 青阳县| 德令哈市| 新余市| 化州市| 盐源县| 西盟| 玉田县| 永福县| 精河县| 罗城| 凉城县| 台北县| 比如县| 延川县| 巫山县| 昭觉县| 虞城县| 平陆县| 盐池县| 崇州市| 章丘市| 陵水| 嘉鱼县| 武邑县| 若羌县| 兴国县| 武功县| 石台县|