2013-01-14 3 views
2

ExpandableListView에 큰 문제가 있습니다. 내 목표는이 단일 선택 확장 목록보기에 사용자 변경 항목의 위치를 ​​수 있도록하는 것입니다 : 체크 표시된 항목을 확장 가능한 목록보기로 이동

enter image description here

사용자가 항목을 클릭

이 항목이 선택되고 상황에 맞는 작업 표시 줄이 나타납니다. 그래서 사용자가 이동하거나이 방법의 항목 아래 수 있습니다

enter image description here

내가 잘 (가 ArrayList에 이상 스왑 위치에 따라 작품 구현 한 움직임 업/다운 기능, 즉의 데이터 소스의 내 BaseExpandableListAdapter 및 UI 변경 사항을 알립니다. 불행히도 내가 체크 항목을 이동하면 체크 상태를 잃어버린 ... 그리고 내가 (예를 들어, "잠금"또는 "Attachements"보기 전에) 보이는 가시 범위 밖으로 항목을 이동하면 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(); 

    } 
} 

답변

1

expandablelistview 뒤에 코드를 공부 한 후, 내가 필요한 작업에 매우 간단한 방법을 발견했다. 목록보기 항목을 확인하고 위/아래로 이동할 수있는 어댑터의 일부를 게시 할 것입니다. 기억 1) expandablelistview는 "하나의 선택"모드 (ExpandableListView.setChoiceMode (AbsListView.CHOICE_MODE_SINGLE)에 있는지 확인)

2) 사용자 정의 행보기는 안드로이드가 : descendantFocusability = "blocksDescendants"

을 난 안드로이드 JB에서 여러 가지 테스트를 수행하고 오류나 메모리 누수없이 제대로 작동하는 것 같습니다. 버그가 발생하면 improovements 또는 수정 사항을 게시하십시오.

public class MyAdapter extends BaseExpandableListAdapter { 

//protected ArrayList<Object> _list; //TODO: Replace with your datasource 
private ExpandableListView _expandableListView; 
private long _checkedPosition; 

public long getCheckedPosition(){ 
    return _checkedPosition; 
} 
public void setCheckedPosition(int groupPosition){ 
    long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition); 
    this.setCheckedPosition(packedPosition); 
} 
public void setCheckedPosition(int groupPosition, int childPosition){ 
    long packedPosition = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition); 
    this.setCheckedPosition(packedPosition); 
} 
public void setCheckedPosition(long position){ 
    final int flatPosition; 

    //Gets flat position 
    flatPosition = _expandableListView.getFlatListPosition(position); 

    //Set the new position, checked and focused 
    _expandableListView.setItemChecked(flatPosition , true); 
    _checkedPosition = position; 

    //If it's out of bounds, set focus on it and force scroll (postpone because row's views may not exists yes) 
    if(flatPosition<_expandableListView.getFirstVisiblePosition() || 
     flatPosition>_expandableListView.getLastVisiblePosition()) 
     _expandableListView.post(new Runnable() { 

      @Override 
      public void run() { 
       _expandableListView.setSelection(flatPosition);  
      } 
     }); 
} 

public void clearCheckedPosition(){ 
    final int flatPosition; 

    flatPosition = _expandableListView.getFlatListPosition(getCheckedPosition()); 
    _expandableListView.setItemChecked(flatPosition , false); 
} 


private long getPreviousPosition(long position){ 
    int group = ExpandableListView.getPackedPositionGroup(position); 
    int child = ExpandableListView.getPackedPositionChild(position); 

    if(child==-1) 
     if(group>0) 
      return ExpandableListView.getPackedPositionForGroup(group-1); 
     else 
      return -1; 
    else 
     if(child>0) 
      return ExpandableListView.getPackedPositionForChild(group, child-1); 
     else 
      return -1; 
} 

private long getNextPosition(long position){ 
    int group = ExpandableListView.getPackedPositionGroup(position); 
    int child = ExpandableListView.getPackedPositionChild(position); 

    if(child==-1) 
     if(group<getGroupCount()-1) 
      return ExpandableListView.getPackedPositionForGroup(group+1); 
     else 
      return -1; 
    else 
     if(child<getChildrenCount(group)-1) 
      return ExpandableListView.getPackedPositionForChild(group, child+1); 
     else 
      return -1; 
} 


public boolean canMoveUpCheckedItem(){ 

    if(getCheckedPosition()!=-1) 
     if(getPreviousPosition(getCheckedPosition())!=-1) 
      return true; 
    return false; 
} 

public boolean canMoveDownCheckedItem(){ 
    if(getCheckedPosition()!=-1) 
     if(getNextPosition(getCheckedPosition())!=-1) 
      return true; 
    return false; 
} 


public void moveUpFocusedItem(){ 

    long prevPosition = this.getPreviousPosition(getCheckedPosition()); 
    if(prevPosition!=-1L){ 
     //Swap position 
     switch(ExpandableListView.getPackedPositionType(getCheckedPosition())){ 
     case ExpandableListView.PACKED_POSITION_TYPE_GROUP: 
      //Collapse group if expanded 
      _expandableListView.collapseGroup(ExpandableListView.getPackedPositionGroup(getCheckedPosition())); 

      //TODO:Implement group swap 
      /*Collections.swap(this._list, 
          ExpandableListView.getPackedPositionGroup(getCheckedPosition()), 
          ExpandableListView.getPackedPositionGroup(prevPosition));*/ 
      break; 

     case ExpandableListView.PACKED_POSITION_TYPE_CHILD: 

      //TODO:Implement child swap 
      /**Collections.swap(this._list.get(ExpandableListView.getPackedPositionGroup(getCheckedPosition())).getChildren(), 
          ExpandableListView.getPackedPositionChild(getCheckedPosition()), 
          ExpandableListView.getPackedPositionChild(prevPosition));**/ 
      break; 

     } 

     //Set new focused position 
     this.setCheckedPosition(prevPosition); 

     //Notify changes 
     this.notifyDataSetChanged(); 

    } 
} 

public void moveDownFocusedItem(){ 
    long nextPosition = this.getNextPosition(getCheckedPosition()); 
    if(nextPosition!=-1L){ 
     switch(ExpandableListView.getPackedPositionType(getCheckedPosition())){ 
     case ExpandableListView.PACKED_POSITION_TYPE_GROUP: 
      //Collapse group if expanded 
      _expandableListView.collapseGroup(ExpandableListView.getPackedPositionGroup(getCheckedPosition())); 

      //TODO:Implement group swap 
      /*Collections.swap(this._list, 
          ExpandableListView.getPackedPositionGroup(getCheckedPosition()), 
          ExpandableListView.getPackedPositionGroup(nextPosition));*/ 
      break; 

     case ExpandableListView.PACKED_POSITION_TYPE_CHILD: 

      //TODO:Implement child swap 
      /*Collections.swap(this._list.get(ExpandableListView.getPackedPositionGroup(getCheckedPosition())).getChildren(), 
          ExpandableListView.getPackedPositionChild(getCheckedPosition()), 
          ExpandableListView.getPackedPositionChild(nextPosition));*/ 
      break; 
     } 

     //Set new focused position 
     this.setCheckedPosition(nextPosition); 

     //Notify changes 
     this.notifyDataSetChanged(); 

    } 
} 







@Override 
public Object getChild(int groupPosition, int childPosition) { 
    //TODO: Implement 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return ExpandableListView.getPackedPositionForChild(groupPosition, childPosition); 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    //TODO: Implement 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    //TODO: Implement 
} 

@Override 
public Object getGroup(int groupPosition) { 
    //TODO: Implement 
} 

@Override 
public int getGroupCount() { 
    //TODO: Implement 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return ExpandableListView.getPackedPositionForGroup(groupPosition); 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    //TODO: Implement your getGroupView 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    //TODO: Implement 
} 

@Override 
public boolean hasStableIds() { 
    //TODO: Implement 
} 

}

: 이 내 코드입니다
관련 문제