我面临着ExpandableListView的一个大问题。 我的目标是让用户改变项目的位置到这个单一的选择扩展列表视图:将选中的项目移动到可扩展列表视图
当用户点击一个项目,该项目成为检查,并显示上下文操作栏。 所以用户可以向上或向下移动一个项目这样:
工作正常(它是基于在一个ArrayList互换位置,我已经实现了向上/向下移动的功能,这就是的数据源我的BaseExpandableListAdapter,并通知UI的更改)。不幸的是,当我移动一个选中的项目,它失去了检查状态......如果我将项目移出可见范围(在示例中,在“锁定”之后或“附件”视图之前)expandablelistview不滚动到新的位置。
我该如何做到这一点?
下面,我“moveDownFocusedItem()”编码到我的自定义适配器类:
//This class is ok!
//Contains Group and Child position of an ExpandableListView
public class Position{
int GroupPosition;
int ChildPosition;
public boolean isChild(){
return (ChildPosition != AbsListView.INVALID_POSITION);
}
public Position getPreviousPosition(){
if(ChildPosition==AbsListView.INVALID_POSITION)
if(GroupPosition>0)
return new Position(GroupPosition-1);
else
return null;
else
if(ChildPosition>0)
return new Position(GroupPosition, ChildPosition-1);
else
return null;
}
public Position getNextPosition(){
if(ChildPosition==AbsListView.INVALID_POSITION)
if(GroupPosition<_fieldConfigurationList.size()-1)
return new Position(GroupPosition+1);
else
return null;
else
if(ChildPosition<_fieldConfigurationList.get(GroupPosition).getChildren().size()-1)
return new Position(GroupPosition, ChildPosition+1);
else
return null;
}
public Position(int groupPosition){
this.GroupPosition = groupPosition;
this.ChildPosition = AbsListView.INVALID_POSITION;
}
public Position(int groupPosition, int childPosition){
this.GroupPosition = groupPosition;
this.ChildPosition = childPosition;
}
}
public void moveDownFocusedItem(){
//This function returns the next position to move to
//(_focusedPosition is the current checked item position)
Position nextPosition = this._focusedPosition.getNextPosition();
//Swap ArrayList (This works!)
if(nextPosition!=null){
Collections.swap(this._fieldConfigurationList, //that's my datasource
this._focusedPosition.GroupPosition, //Start position for swapping
nextPosition.GroupPosition); //Destination position for swapping
//Set the new focused position (This works!)
this._focusedPosition = nextPosition;
//TODO:
//Now i have to call "SetItemChecked()" method to uncheck old focused position and check the new one.
//How to do it? How can i get the required "position" argument from _focusedPosition.GroupPosition and _focusedPosition.ChildPosition?
//And also...if the focused view has moved to an invisible position (such as the end of the expandablelistview) how can i scroll
//to this position?
//Notify changes (This works!)
this.notifyDataSetChanged();
}
}