我使用ExpandableListFragment在TabHost片段
中创建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吗?我试过但无法弄明白。感谢。答案 0 :(得分:6)
只需覆盖listadapter的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;
}
};
答案 1 :(得分:0)
我以一种方式制定了
我创建了一个像这样的SimpleExpandableListAdapter的派生类(或者java所说的)
class MySimpleExpandableListAdapter extends SimpleExpandableListAdapter {
public MySimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, int groupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, groupLayout, groupFrom, groupTo, childData,
childLayout, childFrom, childTo);
// TODO Auto-generated constructor stub
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final TextView tv2 = (TextView)itemRenderer.findViewById(android.R.id.text2);
tv2.setTextColor(0xff0000ff);
return itemRenderer;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){
final View itemRenderer = super.getGroupView(groupPosition, isExpanded, convertView, parent);
final TextView tv = (TextView)itemRenderer.findViewById(android.R.id.text1);
tv.setTextColor(0xff00ff00);
return itemRenderer;
}
}
这里的关键是getChildView和getGroupView的覆盖,它让我可以搞乱android.R.id.text1和2资源。我怀疑这是最有效的方法,但似乎有效。
答案 2 :(得分:0)
感谢Anand,语法看起来比创建新类更清晰。我不知道你可以覆盖方法而不必创建派生类,这是c ++没有的。
但是,你的代码在getChildView中有点错误,它会崩溃。以下作品
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)
{
final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
final TextView tv = (TextView)itemRenderer.findViewById(android.R.id.text2);
tv.setTextColor(Color.BLACK);
return itemRenderer;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView tv = (TextView) super.getGroupView(groupPosition, isExpanded, convertView, parent);
tv.setTextColor(Color.BLUE);
return tv;
}
};