117.info
人生若只如初见

ExpandableListView的分组标题样式

ExpandableListView 的分组标题样式可以通过自定义 GroupHeaderLayout 来实现。以下是一个简单的示例,展示了如何自定义分组标题的样式:

  1. 创建自定义的 GroupHeaderLayout



    

在这个示例中,我们创建了一个包含 TextView 的线性布局作为分组标题。你可以根据需要自定义 TextView 的样式,例如设置文本颜色、大小、字体等。

  1. ExpandableListView 中使用自定义的 GroupHeaderLayout

  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 groupList;
    private Map> 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 group1Items = new ArrayList<>();
        group1Items.add("Item 1.1");
        group1Items.add("Item 1.2");
        childListMap.put(groupList.get(0), group1Items);

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

        List 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 groupList;
    private Map> childListMap;

    public MyExpandableListAdapter(Context context, List groupList, Map> 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 就会使用我们自定义的分组标题样式了。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe576AzsNAAFeBw.html

推荐文章

  • ExpandableListView的滑动效果如何优化

    使用 ViewHolder 缓存子项视图:在 ExpandableListView 的适配器中重写 getChildView() 方法时,可以使用 ViewHolder 来缓存子项视图,减少布局的重复加载,提高...

  • ExpandableListView能否实现复杂布局

    ExpandableListView是Android中可折叠的列表控件,通常用于展示具有父子关系的数据。虽然ExpandableListView本身并不支持复杂的布局,但可以通过自定义Adapter来...

  • ExpandableListView的适配器使用技巧

    继承BaseExpandableListAdapter类:创建一个自定义的ExpandableListAdapter类,继承BaseExpandableListAdapter类,并实现其抽象方法。 实现getGroupCount()方法:...

  • ExpandableListView怎样保存展开状态

    ExpandableListView可以通过保存每个组的展开状态来实现展开状态的保存。可以使用SharedPreferences或者数据库来保存展开状态,以下是一种常见的做法: 创建一个...

  • 如何在ExpandableListView中显示图片

    在ExpandableListView中显示图片需要以下几个步骤: 首先,确保你已经将所需的图片添加到项目的资源文件夹(res/drawable)中。 创建一个自定义的布局文件,例如...

  • ExpandableListView的子项布局

    ExpandableListView 是 Android 中的一种列表视图,它允许用户展开和折叠每个子项,从而显示更多的内容。ExpandableListView 的子项布局通常包含两部分:一个用于...

  • ExpandableListView的缓存机制

    ExpandableListView 是 Android 中一个可展开的列表视图,它允许用户点击列表项来展开并查看子项。为了提高性能和用户体验,ExpandableListView 提供了一种缓存机...

  • 怎样处理ExpandableListView滑动

    处理ExpandableListView滑动时,可以采取以下几种方法来优化性能和用户体验: 使用ViewHolder模式:在自定义的ExpandableListAdapter中,使用ViewHolder模式可以...