要将 ExpandableListView 的图标设置在一行的右边,可以使用自定义 ExpandableListView 的适配器来实现。

首先,在适配器中的 getGroupView 方法中,创建一个布局文件,包含一个 ImageView 和 TextView,将 ImageView 设置为图标,并将 TextView 设置为标题。然后使用 LinearLayout 将它们水平排列,并将 LinearLayout 设置为 GroupView 的返回值。

接下来,在布局文件中,将 ImageView 的布局参数设置为 android:layout_gravity='end',将其放置在 LinearLayout 的右边。

最后,在适配器中的 getChildView 方法中,创建一个布局文件,包含一个 TextView,将其设置为 ChildView 的返回值。

下面是一个示例代码:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

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

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

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childMap.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) {
            convertView = LayoutInflater.from(context).inflate(R.layout.group_item, parent, false);
        }
        ImageView icon = convertView.findViewById(R.id.icon);
        TextView title = convertView.findViewById(R.id.title);
        icon.setImageResource(R.drawable.ic_group);
        title.setText(groupList.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.child_item, parent, false);
        }
        TextView content = convertView.findViewById(R.id.content);
        content.setText(childMap.get(groupList.get(groupPosition)).get(childPosition));
        return convertView;
    }

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

group_item.xml:

<?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='horizontal'
    android:padding='16dp'>

    <ImageView
        android:id='@+id/icon'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:src='@drawable/ic_group'
        android:layout_gravity='end'/>

    <TextView
        android:id='@+id/title'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:textSize='18sp'
        android:textColor='@android:color/black'/>

</LinearLayout>

child_item.xml:

<?xml version='1.0' encoding='utf-8'?>
<TextView xmlns:android='http://schemas.android.com/apk/res/android'
    android:id='@+id/content'
    android:layout_width='match_parent'
    android:layout_height='wrap_content'
    android:padding='16dp'
    android:textSize='16sp'
    android:textColor='@android:color/black'/>
Android ExpandableListView 图标设置在右边的实现方法

原文地址: https://www.cveoy.top/t/topic/nG2y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录