gpt4 book ai didi

android - 如何更改 SimpleExpandableListAdapter 中的文本颜色

转载 作者:行者123 更新时间:2023-11-29 14:52:16 26 4
gpt4 key购买 nike

我正在使用 ExpandableListFragment 在 TabHost fragment 中创建一个 ExpandableList

我从这里得到了 ExpandableListFragment 的代码 https://gist.github.com/1316903

它似乎工作正常,只是我不知道如何更改文本颜色。

我的实现是这样的

public class LocalListFragment extends ExpandableListFragment {

private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";

private ArrayList<String> mGroups;
private ArrayList<ArrayList<Song>> mChildren;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Set up our adapter

List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "GROUP" + i);
//curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 5; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(IS_EVEN, "Child " + j);
}
childData.add(children);
}


mAdapter = new SimpleExpandableListAdapter(
getActivity().getApplicationContext(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);

//TextView tv = (TextView)getActivity().findViewById(android.R.id.text1);
//tv.setTextColor(0xffff0000);
setListAdapter(mAdapter);

return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("TAG", "Item selected");
}

}

我的猜测是“new int[] { android.R.id.text1, android.R.id.text2 }”这行与它有很大关系,但我对此有点困惑。

有人可以帮忙吗?我需要创建新的 TextView id 并指定 textColor 吗?我试过了,但想不通。谢谢。

最佳答案

只需重写列表适配器的 getview 方法:

mAdapter = new SimpleExpandableListAdapter(
getActivity().getApplicationContext(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }){


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

TextView tv = (TextView) super.getChildView(groupPosition, childPosition, isLastChild,convertView, parent);
//change background of tv here
return tv;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = (TextView) super.getGroupView(groupPosition, isExpanded, convertView, parent);
//change background of tv here
return tv;
}

};

关于android - 如何更改 SimpleExpandableListAdapter 中的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com