我正在摆弄我的自定义BaseExpandableListAdapter,在我的getChildView方法中,我似乎得到了子视图的随机位置。无论回收如何,我都会得到随机的排名顺序。例如,在convertView = null条件分支中,我得到:
LogCat打印在(ConvertView == null):
childPosition:childPosition - 0 childPosition:childPosition - 1 childPosition:childPosition - 0 childPosition:childPosition - 3 childPosition:childPosition - 4 childPosition:childPosition - 0
LogCat打印在(ConvertView!= null):
childPosition:childPosition - 1 childPosition:childPosition - 2 childPosition:childPosition - 3 childPosition:childPosition - 4 childPosition:childPosition - 5 childPosition:childPosition - 6 childPosition:childPosition - 7 childPosition:childPosition - 8 childPosition:childPosition - 0 childPosition:childPosition - 1 childPosition:childPosition - 0 childPosition:childPosition - 1
以下是我的适配器的代码:
public class MyExapndableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> headerList;
private HashMap<String, List<String>> _listDataChild;
public MyExapndableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this.context = context;
this._listDataChild = listChildData;
this.headerList = listDataHeader;
Log.d("LIST CHILD", "" + _listDataChild.size());
Log.d("LIST HEADER", "" + this.headerList.size());
}
@Override
public int getGroupCount() {
return this.headerList.size();
}
@Override
public int getChildrenCount(int i) {
return this._listDataChild.get(this.headerList.get(i)).size();
}
@Override
public Object getGroup(int i) {
return this.headerList.get(i);
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this.headerList.get(groupPosition)).get(childPosititon);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v = convertView;
String headerTitle = (String) getGroup(groupPosition);
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_group, null);
ViewHolder holder = new ViewHolder();
holder.addView(v.findViewById(R.id.blaGroup));
v.setTag(holder);
}
ViewHolder holder = (ViewHolder) v.getTag();
MyText title = (MyText) holder.getView(R.id.blaGroup);
title.setText(headerTitle);
return v;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
View row = convertView;
if (row == null) {
// Log.d("NULL VIEW", "childText - " +childText);
Log.d("childPosition", "childPosition - " +childPosition);
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_child, null);
ViewGroup.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final float GESTURE_THRESHOLD_DIP = 30.0f;
// Convert the dips to pixels
final float scale = context.getResources().getDisplayMetrics().density;
int pixel = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/pacifico.ttf");
int fontColor = context.getResources().getColor(R.color.testColor);
int strokeWidth = 5;
int strokeColor = context.getResources().getColor(R.color.card_white);
// Add TextView on the fly
MyText text = new MyText(context, pixel, childText, font, fontColor, strokeWidth, strokeColor);
text.setLayoutParams(lp);
text.setText(childText);
text.setTextSize(GESTURE_THRESHOLD_DIP);
text.setId(3005);
((ViewGroup) row).addView(text);
ViewHolder holder = new ViewHolder();
holder.addView(text);
row.setTag(holder);
}
else {
// Log.d("NOT NULL VIEW", "childText - " +childText);
Log.d("NOT NULL VIEW childPosition", "childPosition - " +childPosition);
}
ViewHolder holder = (ViewHolder) row.getTag();
MyText text = (MyText) holder.getView(3005);
text.setText(childText);
return row;
}
public static class ViewHolder {
private HashMap<Integer, View> storedViews = new HashMap<Integer, View>();
public ViewHolder() {
}
public ViewHolder addView(View view) {
int id = view.getId();
storedViews.put(id, view);
return this;
}
public View getView(int id) {
return storedViews.get(id);
}
}
@Override
public boolean isChildSelectable(int i, int i2) {
return true;
}
}
答案 0 :(得分:0)
您获得了一个随机位置,因为您使用的是 Hashmap 。 Hashmap有自己的方法来对其中的数据进行排序。所以我建议使用自定义对象而不是Hashmap。
答案 1 :(得分:0)
那么childView的位置不是随机的,只是它们是来自不同组的子位置,与groupId一起打印它们你会明白发生了什么。此外,childPosition并不依赖于convertview是否为null,通常会为列表项调用getChildView,该列表项将在滚动时进入屏幕。 希望这会有所帮助,如果你想要一个例子,请回复。