2010-01-02 35 views
3

如何將ResourceCursorTreeAdapter與以下構造函數一起使用?如何在Android中使用ResourceCursorTreeAdapter和展開和摺疊的組視圖?

ResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, int childLayout) 

我想如下使用它:

_resultsCursorTreeAdapter = new ResourceCursorTreeAdapter(_resultsList.getContext(), _dbAdapter.getAllGroups(), 
     R.layout.timing_group_view_collapsed, R.layout.timing_group_view_expanded, R.layout.timing_result_view) { 

@Override 
protected Cursor getChildrenCursor(Cursor groupCursor) { 
    // Given the group, we return a cursor for all the children within that group 
    int groupId = groupCursor.getInt(0); 
    Cursor childCursor = _dbAdapter.getContractionsForGroup(groupId); 
    return childCursor; 
} 

@Override 
protected void bindGroupView(View groupView, Context context, Cursor cursor, 
    boolean isExpanded) { 

    TimingGroupView timingGroupItem = null; 
    if(groupView instanceof LinearLayout){ 
    Log.i("TimingGroupView", "Has Header"); 
    LinearLayout layout = (LinearLayout)groupView; 

    timingGroupItem = (TimingGroupView) layout.getChildAt(0); 
    } else{ 
    Log.i("TimingGroupView", "No Header"); 
    timingGroupItem = (TimingGroupView) groupView; 
    } 
... 

如果組節點展開,我希望組節點包括每一行被保持在一個表中的標題一個孩子節點。 timing_group_view_expanded.xml和timing_group_view_collapsed.xml顯示在這個問題的底部。出於某種原因,無論組節點是展開還是摺疊,都不會使用group_view_expanded。我使用這個錯誤嗎?是否有其他人能夠使用此構造函數獲取ResourceCursorTreeAdapter?

timing_group_view_expanded.xml如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/timing_group_view" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@color/header_timing_color"> 

<com.contractiontracker.TimingGroupView 
    android:id="@+id/timing_group_item" xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:layout_marginLeft="30px" android:padding="10dp" 
    android:scrollbars="vertical" 
    android:fadingEdge="vertical" 
    android:background="@color/header_timing_color" 
    android:textColor="@color/text_color"/> 

<com.contractiontracker.RowLayout android:id="@+id/timing_group_view" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/header_color" android:textColor="@color/text_color"> 

    <TextView android:id="@+id/interval_header" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:text="Interval" android:layout_weight="1" 
    android:layout_gravity="left|bottom" android:gravity="center" 
    android:textColor="@color/text_color"> 
    </TextView> 
    <TextView android:id="@+id/duration_header" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:text="Duration" android:layout_weight="1" 
    android:layout_gravity="center_horizontal|bottom" android:gravity="center" 
    android:textColor="@color/text_color" 
    > 
    </TextView> 
    <TextView android:id="@+id/intensity_header" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:text="Intensity" android:layout_weight="1" 
    android:layout_gravity="right|bottom" android:gravity="center" 
    android:textColor="@color/text_color" 
    > 
    </TextView> 
</com.contractiontracker.RowLayout> 
</LinearLayout> 

timing_group_view_collapsed.xml如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<com.contractiontracker.TimingGroupView 
    android:id="@+id/timing_group_item" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="50px" 
android:padding="10dp" 
android:scrollbars="vertical" 
android:textColor="@color/text_color" 
android:fadingEdge="vertical"/> 

回答

0

這裏是我的SimpleExpandableListAdapter臨時解決方案:

public class LocalAdapter extends SimpleExpandableListAdapter { 
    private List<? extends Map<String, ?>> mGroupData; 
    private String[] mGroupFrom; 
    private int[] mGroupTo; 

    private List<? extends List<? extends Map<String, ?>>> mChildData; 

    private LayoutInflater mInflater; 

    public LocalAdapter(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) { 
     this(context, groupData, groupLayout, groupLayout, groupFrom, groupTo, childData, 
       childLayout, childLayout, childFrom, childTo); 
    } 

    public LocalAdapter(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) { 
     this(context, groupData, expandedGroupLayout, collapsedGroupLayout, 
       groupFrom, groupTo, childData, childLayout, childLayout, 
       childFrom, childTo); 
    } 

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

     super(context, groupData, expandedGroupLayout, 
       collapsedGroupLayout, groupFrom, groupTo, childData, 
       childLayout, lastChildLayout, childFrom, childTo); 

     mGroupData = groupData; 
     mGroupFrom = groupFrom; 
     mGroupTo = groupTo; 
     mChildData = childData; 
     mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    }   

    private void bindView(View view, Map<String, ?> data, String[] from, int[] to) { 
     int len = to.length; 

     for (int i = 0; i < len; i++) { 
      TextView v = (TextView)view.findViewById(to[i]); 
      if (v != null) { 
       v.setText((String)data.get(from[i])); 
      } 
     } 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
      ViewGroup parent) { 
     View v = newGroupView(isExpanded, parent); 
     bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo); 
     return v; 
    } 
} 
+0

我認爲你需要檢查'convertview == null'否則每一次新groupview將被創建並列表項的概念不會在這裏工作,導致缺乏業績在滾動等 – kAmol 2017-04-26 10:26:07

6

的問題是那ResourceCursorTreeAdapter僅在創建視圖的創建時創建正確的佈局。因此,如果一個組被摺疊或展開,newGroupView()不會再次調用,因此傳遞給構造函數的不同組佈局不按預期方式使用。

對於正常孩子和最後一個孩子使用不同的孩子佈局時,也存在同樣的問題。它發生在那裏甚至沒有改變數據!在重新擴展一個組之後,第一個有了頁腳佈局,反之亦然。完全隨機的,而不是基於列表中的位置。

作爲亞歷山大·ö顯示在他的實施例,可以通過總是調用newGroupView()getGroupView方法(以及分別getChildView如果將用於所有不同的佈局)解決此問題。

爲了避免總是創造新的觀點,我嘗試了不同的方法,對我來說,工作原理:

  • 添加不同的ID以用於組佈局的根元素/子視圖
  • 存儲這些視圖-ID作爲INT我的適配器的成員:

    // Get the view ids for comparison 
    View view = newGroupView(mContext, null, false, null); 
    mCollapsedGroupLayoutViewId = view.getId(); 
    view = newGroupView(mContext, null, true, null); 
    mExpandedGroupLayoutViewId = view.getId(); 
    
    view = newChildView(mContext, null, false, null); 
    mChildLayoutViewId = view.getId(); 
    view = newChildView(mContext, null, true, null); 
    mLastChildLayoutViewId = view.getId(); 
    
  • getGroupView的執行情況(並用getChildView模擬):

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
         Cursor cursor = getGroup(groupPosition); 
         if (cursor == null) { 
          throw new IllegalStateException("this should only be called when the cursor is valid"); 
         } 
    
         View v; 
         if (convertView == null 
          || (isExpanded && convertView.getId() == mCollapsedGroupLayoutViewId) 
          || (!isExpanded && convertView.getId() == mExpandedGroupLayoutViewId)) { 
          v = newGroupView(mContext, cursor, isExpanded, parent); 
         } 
         else { 
          v = convertView; 
         } 
         bindGroupView(v, mContext, cursor, isExpanded); 
         return v; 
    } 
    
+0

sunadorer和回收@丹德雷謝謝你的回答。但是根據我的經驗,即使'getGroupView(...)'__不被稱爲_當我點擊group_展開/摺疊它。相反,它將需要一些滾動,直到最後調用'getGroupView(...)'並更新'groupView'。有沒有人設法**立即**改變'groupView'的佈局,當它被點擊? – hadi 2013-08-15 19:50:02

+0

@hadi對不起,我遲到了。我仍然在我的一個應用程序中使用我的答案中顯示的解決方案。它可以很好地直接切換佈局可見性,然後隱藏所有子視圖。也許你可以打開一個新的問題,並顯示你的代碼片段和有關測試設備的信息,它不能立即工作。 – sunadorer 2013-09-13 17:13:03

+0

感謝您的解決方案+1。但是,我們可以省略第2步(將視圖ID存儲爲適配器的成員),因爲我們已經在佈局xml文件中添加了id(@ + id/group_collapsed,@ + id/group_expanded,...等)。因此,在第三步中,在代碼中,只需比較convertView.getId()== R.id.group_collapsed,...等就可以了。 – 2014-08-18 11:39:27

0

易於回收的解決方案。

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    Cursor cursor = getGroup(groupPosition); 
    if (cursor == null) { 
     throw new IllegalStateException("this should only be called when the cursor is valid"); 
    } 

    View view; 
    if (convertView == null || convertView.getTag() != isExpanded) { 
     view = newGroupView(mActivity, cursor, isExpanded, parent); 
     view.setTag(isExpanded); 
    } else { 
     view = convertView; 
    } 
    bindGroupView(view, mActivity, cursor, isExpanded); 
    return view; 
}