簡體   English   中英

SimpleExpandableListAdapter和expandedGroupLayout

[英]SimpleExpandableListAdapter and expandedGroupLayout

我正在嘗試使用SimpleExpandableListAdapter ,我需要不同的布局,無論它們是否被擴展。

有一個構造函數允許為組傳遞兩個布局:

SimpleExpandableListAdapter(Context context,
 List<? extends Map<String, ?>> groupData,
 int expandedGroupLayout,
 int collapsedGroupLayout,
 String[] groupFrom,
 int[] groupTo,
 List<? extends List<? extends Map<String, ?>>> childData,
 int childLayout,
 String[] childFrom,
 int[] childTo)

我傳遞了兩種不同的布局,但只顯示了collapsedGroupLayout。

任何人都有這個工作嗎?

謝謝

我查看了SimpleExpandableListAdapter的源代碼,看起來有一個bug:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {
    View v;
    if (convertView == null) {
        v = newGroupView(isExpanded, parent);
    } else {
        v = convertView;
    }
    bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
    return v;
}

我們可以看到,如果convertView不為null,因此如果listview重用其單元格,則“isExpanded”參數將永遠不會被使用。 因此,如果單元格可見並被單擊,則convertView將按原樣重復使用,並且永遠不會使用其他布局。

對我來說,一個快速的解決方案是覆蓋getGroupView並每次為convertView傳遞null:

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        return super.getGroupView(groupPosition, isExpanded, null, parent);
    }

更好的解決方案是保留組位置及其狀態列表,如果狀態發生變化則不重用convertView。

我想出了另一個想法來解決這個問題。

我正在使用以下getGroupView覆蓋:

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

    View v;

    if (convertView != null && ((Boolean) convertView.getTag()) == isExpanded) {
        v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
    } else {
        v = super.getGroupView(groupPosition, isExpanded, null, parent);
        v.setTag(isExpanded);
    }

    return v;
}

這個想法是只有當convertView具有不同的布局時才強制獲得新的布局,具體取決於isExpanded參數。 在創建(展開和折疊)2個視圖后,Android將重用正確的convertView。 這種方法最大的問題是我在視圖中使用了一個標簽。 但我認為,只要沒有人使用標簽,但我可以。

我希望它有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM