2015-02-18 50 views
1

我有BaseExpandableListAdapter與ExpandableListView但我有麻煩讓我的孩子ViewHolder初始化到正確的看法。我把一個破發點中getChildView並在第一時間它被稱爲一切都好BaseExpandableListAdapter與ExpandableListView返回錯誤的子視圖

groupPosition:0,childPosition:0,convertView:空

的ViewHolder獲取設置和我們是很好的第一行(0 )。下一次它被稱爲我得到:

groupPosition:0,childPosition:1,convertView:從排ViewHolder對象(0)

爲什麼返回從第0行的看法?我已經包含了我從BaseExpandableListAdapter重寫的方法。爲了獲得正確的視圖,是否還需要重寫另一種方法?

child_layout.xml 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="9"> 
    <LinearLayout android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_weight="1"> 
     <EditText.../> 
    </LinearLayout> 
    ... 
</LinearLayout> 




// Group and children are both collections 
@Override 
public int getGroupCount() { 
    return group.size(); 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return group.get(groupPosition).getChildren().size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return group.get(groupPosition); 
} 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return group.get(groupPosition).getChildren().toArray()[childPosition]; 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return getGroup(groupPosition).hashCode(); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return getChild(groupPosition, childPosition).hashCode(); 
} 

@Override 
public boolean hasStableIds() { 
    return true; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

    final GroupViewHolder groupViewHolder; 
    if(null == convertView) { 
     convertView = LayoutInflater.from(context).inflate(R.layout.group_layout, parent, false); 
     groupViewHolder = new GroupViewHolder(); 
     // add delete group button 
    } else { 
     groupViewHolder = (GroupViewHolder) convertView.getTag(); 
    } 
    return convertView; 
} 

@Override 
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    View v = convertView; 
    final ViewHolder viewHolder; 
    if (null == v){ 
     v = LayoutInflater.from(context).inflate(child_layout, parent, false); 
     viewHolder = new ViewHolder(); 
     // bind edittext fields 
     v.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) v.getTag(); 
    } 
    return v; 
} 
+0

刪除您的if和else,並把這個..如果(convertView == NULL){ \t LayoutInflater infalInflater =(LayoutInflater)this._context \t .getSystemService(Context.LAYOUT_INFLATER_SERVICE); \t convertView = infalInflater.inflate(R.layout.child_layout,null); \t} – Pavya 2015-02-18 06:12:51

+0

沒有運氣,相同的問題 – bmurmistro 2015-02-18 13:34:57

回答

1

我的頁面佈局包含一個垂直的LinearLayout,它包裹了一切。我有layout_height =「wrap_content」。將其更改爲match_parent可解決問題。希望這可以幫助某人。

+0

嘿,你真棒。在Android中這太愚蠢了,如果你的可擴展列表視圖是包裝內容,那麼這個孩子的位置有多麼不一致......這真的是拋棄了我的數據/視圖。我幾乎只是使用小組的位置,並拉出所有的孩子,併爲每一個都創建了佈局(兒童數只設置爲1)。謝謝你,先生! – 2016-08-17 14:20:51

+0

很高興能幫到你! – bmurmistro 2016-08-18 03:46:31

相關問題