首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

扩展列表时,在ExpandedListView中更改文本颜色

扩展列表时,在ExpandedListView中更改文本颜色,可以通过自定义适配器来实现。以下是一个简单的示例:

  1. 创建一个自定义布局文件,例如list_item.xml,并在其中定义要显示的文本视图。
代码语言: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">

   <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textColor="@android:color/black" />

</LinearLayout>
  1. 创建一个自定义适配器,例如CustomExpandableListAdapter,并在其中更新文本颜色。
代码语言:java
复制
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

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

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

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

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

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

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

    @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.list_group, parent, false);
        }

        TextView textView = convertView.findViewById(R.id.text_view);
        textView.setText(getGroup(groupPosition).toString());

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

        TextView textView = convertView.findViewById(R.id.text_view);
        textView.setText(getChild(groupPosition, childPosition).toString());
        textView.setTextColor(Color.RED); // 更改文本颜色

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  1. 在主活动中,使用自定义适配器填充ExpandedListView。
代码语言:java
复制
public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private CustomExpandableListAdapter adapter;

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

        expandableListView = findViewById(R.id.expandable_list_view);

        List<String> groupList = new ArrayList<>();
        groupList.add("Group 1");
        groupList.add("Group 2");
        groupList.add("Group 3");

        Map<String, List<String>> childList = new HashMap<>();
        childList.put("Group 1", Arrays.asList("Item 1", "Item 2", "Item 3"));
        childList.put("Group 2", Arrays.asList("Item 1", "Item 2", "Item 3"));
        childList.put("Group 3", Arrays.asList("Item 1", "Item 2", "Item 3"));

        adapter = new CustomExpandableListAdapter(this, groupList, childList);
        expandableListView.setAdapter(adapter);
    }
}

这样,在ExpandedListView中显示的文本颜色就会更改为红色。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

5分24秒

一小时学会Redis系列教程-05-Redis 命令-在 Redis 中存储列表

11分33秒

061.go数组的使用场景

1分31秒

SNP BLUEFIELD是什么?如何助推SAP系统数据快捷、安全地迁移至SAP S/4 HANA

26分24秒

Game Tech 腾讯游戏云线上沙龙--英国/欧盟专场

37分20秒

Game Tech 腾讯游戏云线上沙龙--美国专场

22分30秒

Game Tech 腾讯游戏云线上沙龙--中东专场

2分43秒

ELSER 与 Q&A 模型配合使用的快速演示

领券