溫馨提示×

ExpandableListView的適配器怎么寫

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

ExpandableListView 的適配器需要繼承自 BaseAdapter,并重寫其中的方法。以下是一個簡單的示例:

首先,創(chuàng)建一個自定義的 ExpandableListAdapter 類:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyExpandableListAdapter extends BaseAdapter {

    private Context context;
    private List<String> groupHeaders;
    private List<List<String>> childItems;

    public MyExpandableListAdapter(Context context, List<String> groupHeaders, List<List<String>> childItems) {
        this.context = context;
        this.groupHeaders = groupHeaders;
        this.childItems = childItems;
    }

    @Override
    public int getCount() {
        // 返回數(shù)據(jù)的總行數(shù),包括分組和子項
        return groupHeaders.size() + childItems.size();
    }

    @Override
    public Object getItem(int position) {
        // 返回指定位置的數(shù)據(jù)項
        if (position < groupHeaders.size()) {
            return groupHeaders.get(position);
        } else {
            return childItems.get(position - groupHeaders.size());
        }
    }

    @Override
    public long getItemId(int position) {
        // 返回指定位置的 ID,通??梢允褂?position 作為 ID
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 返回指定位置的視圖
        if (position < groupHeaders.size()) {
            // 如果是分組標題,則使用 groupHeaders 中的數(shù)據(jù)
            TextView textView = (TextView) convertView;
            if (convertView == null) {
                textView = (TextView) LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            textView.setText(groupHeaders.get(position));
            return textView;
        } else {
            // 如果是子項,則使用 childItems 中的數(shù)據(jù)
            TextView textView = (TextView) convertView;
            if (convertView == null) {
                textView = (TextView) LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            textView.setText(childItems.get(position - groupHeaders.size()).get(0));
            return textView;
        }
    }

    @Override
    public int getGroupCount() {
        // 返回分組的數(shù)量
        return groupHeaders.size();
    }

    @Override
    public int getGroupView(int groupPosition, View convertView, ViewGroup parent) {
        // 返回分組標題的視圖
        TextView textView = (TextView) convertView;
        if (convertView == null) {
            textView = (TextView) LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        textView.setText(groupHeaders.get(groupPosition));
        return textView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // 返回子項是否可選中,通常設(shè)置為 true
        return true;
    }
}

接下來,在你的 ActivityFragment 中設(shè)置適配器:

import android.os.Bundle;
import android.widget.ExpandableListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private MyExpandableListAdapter adapter;

    private List<String> groupHeaders;
    private List<List<String>> childItems;

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

        expandableListView = findViewById(R.id.expandableListView);

        // 準備數(shù)據(jù)
        groupHeaders = new ArrayList<>();
        childItems = new ArrayList<>();

        groupHeaders.add("Group 1");
        List<String> childItem1 = new ArrayList<>();
        childItem1.add("Child 1.1");
        childItem1.add("Child 1.2");
        childItems.add(childItem1);

        groupHeaders.add("Group 2");
        List<String> childItem2 = new ArrayList<>();
        childItem2.add("Child 2.1");
        childItem2.add("Child 2.2");
        childItems.add(childItem2);

        // 設(shè)置適配器
        adapter = new MyExpandableListAdapter(this, groupHeaders, childItems);
        expandableListView.setAdapter(adapter);
    }
}

這樣,你就創(chuàng)建了一個簡單的 ExpandableListView 適配器。你可以根據(jù)需要修改數(shù)據(jù)結(jié)構(gòu)和視圖樣式。

0