如何在Android中使用ResourceCursorTreeAdapter扩展和折叠组视图?

时间:2010-01-02 01:49:52

标签: android expandablelistview

如何将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"/>

3 个答案:

答案 0 :(得分:7)

问题是ResourceCursorTreeAdapter 仅在视图的创建上创建正确的布局。因此,如果某个组被折叠或展开,则newGroupView() 不再被调用,因此传递给构造函数的不同组布局不会按预期使用。

对于普通和最后一个孩子使用不同的布局时,子布局存在同样的问题。它发生在那里甚至没有改变数据!重新扩展组后,第一个具有页脚布局,反之亦然。完全随机,而不是基于列表中的位置。

正如Aleksander O在他的示例中所示,可以通过始终在newGroupView()方法中调用getGroupView来解决此问题(如果使用不同的布局,则分别调用getChildView)。< / p>

为了避免总是创建新视图,我尝试了一种适用于我的情况的不同方法:

  • 将不同的ID添加到用于组/子视图的布局的根元素
  • 将这些view-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;
    }
    

答案 1 :(得分:1)

这是我对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;
    }
}

答案 2 :(得分:1)

易于解决,回收良好。

@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;
}