我擺弄我的自定義BaseExpandableListAdapter,並在我的getChildView方法中,我似乎得到了隨機位置的子視圖。無論是否回收,我都會得到隨機的職位訂單。例如,在convertView = NULL條件分支我得到:在BaseExpandableListAdapter - getChildView隨機位置
logcat的照片(ConvertView == NULL):
childPosition:childPosition - 0 childPosition:childPosition - 1 childPosition:childPosition - 0 childPosition:childPosition - 3 childPosition:childPosition - 4 childPosition:childPosition - 在(ConvertView!= NULL)0
logcat的打印:
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;
}
}
您是否使用getchild方法獲取錯誤的項目。 – Piyush 2014-11-10 06:31:52