如何在Android中将ResourceCursorTreeAdapter与展开和折叠的组视图一起使用?
如何将ResourceCursorTreeAdapter与以下构造函数一起使用如何在Android中将ResourceCursorTreeAdapter与展开和折叠的组视图一起使用?,android,expandablelistview,Android,Expandablelistview,如何将ResourceCursorTreeAdapter与以下构造函数一起使用 ResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, int childLayout) 我尝试使用一个如下所示的it: _resultsCursorTreeAdapter = new ResourceCursorTreeAdapter(_resultsL
ResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, int childLayout)
我尝试使用一个如下所示的it:
_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;
}
...
<?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_expanded.xml和timing_group_view_collapsed.xml显示在问题的底部。由于某些原因,无论组节点是展开还是折叠,都不会使用组视图展开。我用错了吗?是否有其他人能够使用此构造函数使ResourceCursorTreeAdapter工作
timing_group_view_expanded.xml如下所示:
_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;
}
...
<?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"/>
以下是我针对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;
}
}
公共类LocalAdapter扩展了SimpleExpandableListAdapter{
私有列表>管理器组数据;
私有字符串[]mGroupFrom;
私人int[]管理组;
私人列表>>麦克希尔达;
私人停车场;
公共LocalAdapter(上下文,
列表>组数据,int-groupLayout,
字符串[]groupFrom,int[]groupTo,
列表>>儿童数据,
int childLayout,字符串[]childFrom,int[]childTo){
这(上下文、groupData、groupLayout、groupLayout、groupFrom、groupTo、childData、,
childLayout、childLayout、childFrom、childTo);
}
公共LocalAdapter(上下文,
列表>组数据,int expandedGroupLayout,
int collapsedGroupLayout,字符串[]groupFrom,int[]groupTo,
列表>>儿童数据,
int childLayout,字符串[]childFrom,int[]childTo){
这(上下文、groupData、expandedGroupLayout、collapsedGroupLayout、,
groupFrom、groupTo、childData、childLayout、childLayout、,
childFrom,childTo);
}
公共LocalAdapter(上下文,
列表>组数据,int expandedGroupLayout,
int collapsedGroupLayout,字符串[]groupFrom,int[]groupTo,
列表>>儿童数据,
int childLayout,int lastChildLayout,字符串[]childFrom,
int[]childTo){
超级(上下文、组数据、expandedGroupLayout、,
collapsedGroupLayout、groupFrom、groupTo、childData、,
childLayout、lastChildLayout、childFrom、childTo);
mGroupData=groupData;
mGroupFrom=groupFrom;
mGroupTo=groupTo;
mChildData=childData;
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
}
私有void bindView(视图视图,映射数据,字符串[]from,int[]to){
int len=to.length;
对于(int i=0;iResourceCursorTreeAdapter
仅在创建视图时创建正确的布局。因此,如果一个组被折叠或展开,则不会再次调用newGroupView()
,,因此传递到构造函数中的不同组布局不会按预期使用
当对正常子布局和最后一个子布局使用不同布局时,子布局也存在同样的问题。它发生在那里,甚至不改变数据!重新扩展组后,第一个组具有页脚布局,反之亦然。完全随机,不基于列表中的位置
正如Aleksander O在其示例中所示,可以通过始终在getGroupView
方法中调用newGroupView()
来解决此问题(如果使用不同的布局,则分别调用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的模拟):
@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;
}
谢谢你的回答。但根据我的经验,当我点击组以展开/折叠它时,甚至不会调用
getGroupView(…)
。相反,它需要一些滚动,直到最后调用getGroupView(…)
并更新groupView
。点击groupView
时,是否有人立即更改了它的布局?@hadi很抱歉我的回复太晚了。我仍然在我的一个应用程序中使用答案中显示的解决方案。直接在组上单击并隐藏所有子视图时切换布局可见性效果良好。也许你可以打开一个新的问题,展示你的代码片段和关于测试设备的信息,这些设备不能立即工作。谢谢你的解决方案,+1。但是,我们可以省略第二步(将视图id存储为适配器的成员),因为我们已经在布局xml文件中添加了id(@+id/group_折叠,@+id/group_展开,…),等等)。所以,在第三步中,在代码中,只需比较convertView.getId()==R.id.group_,…etc没问题。我认为您需要检查convertview==null
,否则每次创建新的groupview时,列表项概念的循环在这里都不起作用,从而导致scroll等中的性能不足。如果解释一下如何解决问题,这个答案会更有帮助。