android - 如何在 Android 中将 ResourceCursorTreeAdapter 与展开和折叠的组 View 一起使用?

标签 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"/>

最佳答案

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

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

正如 Aleksander O 在他的例子中展示的那样,可以通过在 getGroupView 方法(和 getChildView如果使用不同的布局,则分别使用)。

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

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

关于android - 如何在 Android 中将 ResourceCursorTreeAdapter 与展开和折叠的组 View 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1990331/

相关文章:

java - ExpandableListView 上的 Volley gson 请求导致 java.lang.IndexOutOfBoundsException

android - 相对布局的百分比宽度

android - 屏幕关闭时重启应用程序

android - 可扩展 ListView 指示器显示标题高度?

android - ListView/ExpandableListView setEmptyView() 无效

android - 数据更改后未调用 ExpandableListView getChildrenCount,导致 IndexOutOfBoundsException

android - 可扩展列表,在 android 中的 grouplayput 上编辑文本

android - Gradle 多模块依赖 - 一起使用 api 和 testImplementation

android - 为什么我不能使用低版本的buildTools?

Android 从媒体播放器中删除快进和倒带按钮