扩展列表时,在ExpandedListView中更改文本颜色,可以通过自定义适配器来实现。以下是一个简单的示例:
list_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">
<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>
CustomExpandableListAdapter
,并在其中更新文本颜色。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;
}
}
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中显示的文本颜色就会更改为红色。
Game Tech
Game Tech
Game Tech
云+未来峰会
云原生正发声
Elastic 中国开发者大会
DBTalk技术分享会
serverless days
云+社区技术沙龙[第17期]
DBTalk技术分享会
领取专属 10元无门槛券
手把手带您无忧上云