2013-02-27 146 views
3

我在可擴展列表視圖中使用,當我在列表視圖中打開其中一個項目時,我的滾動會自動關注打開的項目,我可以防止從列表中關注新項目並留在同一個地方?我試圖從打開的視圖中移除焦點,但它不起作用。Android在可擴展列表視圖中禁用自動滾動

回答

6

您需要重寫OnGroupClickListener並使用該事件。這將避免ExpandableListView執行默認操作,至少在編譯4.3時將smoothScroll移動到該位置。

下面是一個實現的例子:

 expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
     @Override 
     public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
      if(parent.isGroupExpanded(groupPosition)){ 
       parent.collapseGroup(groupPosition); 
      }else{ 
       boolean animateExpansion = false; 
       parent.expandGroup(groupPosition,animateExpansion); 
      } 
      //telling the listView we have handled the group click, and don't want the default actions. 
      return true; 
     } 
    }); 

如果你消耗的情況下,你還需要,如果你需要它來複制一些默認行爲。這包括播放聲音效果和調用展開/摺疊聽衆。

爲了針對默認行爲參考,我將發佈它,從Android源代碼截取的哈克的方式(4.3)

//in ExpandableListView.java 
    boolean handleItemClick(View v, int position, long id) { 
    final PositionMetadata posMetadata = mConnector.getUnflattenedPos(position); 

    id = getChildOrGroupId(posMetadata.position); 

    boolean returnValue; 
    if (posMetadata.position.type == ExpandableListPosition.GROUP) { 
     /* It's a group, so handle collapsing/expanding */ 

     /* It's a group click, so pass on event */ 
     if (mOnGroupClickListener != null) { 
      if (mOnGroupClickListener.onGroupClick(this, v, 
        posMetadata.position.groupPos, id)) { 
       posMetadata.recycle(); 
       return true; 
      } 
     } 

     if (posMetadata.isExpanded()) { 
      /* Collapse it */ 
      mConnector.collapseGroup(posMetadata); 

      playSoundEffect(SoundEffectConstants.CLICK); 

      if (mOnGroupCollapseListener != null) { 
       mOnGroupCollapseListener.onGroupCollapse(posMetadata.position.groupPos); 
      } 
     } else { 
      /* Expand it */ 
      mConnector.expandGroup(posMetadata); 

      playSoundEffect(SoundEffectConstants.CLICK); 

      if (mOnGroupExpandListener != null) { 
       mOnGroupExpandListener.onGroupExpand(posMetadata.position.groupPos); 
      } 

      final int groupPos = posMetadata.position.groupPos; 
      final int groupFlatPos = posMetadata.position.flatListPos; 

      final int shiftedGroupPosition = groupFlatPos + getHeaderViewsCount(); 
      smoothScrollToPosition(shiftedGroupPosition + mAdapter.getChildrenCount(groupPos), 
        shiftedGroupPosition); 
     } 

     returnValue = true; 
    } else { 
     /* It's a child, so pass on event */ 
     if (mOnChildClickListener != null) { 
      playSoundEffect(SoundEffectConstants.CLICK); 
      return mOnChildClickListener.onChildClick(this, v, posMetadata.position.groupPos, 
        posMetadata.position.childPos, id); 
     } 

     returnValue = false; 
    } 

    posMetadata.recycle(); 

    return returnValue; 
} 
0

種類,但是可以延長ExpandableListView並覆蓋其]平滑滾動方法和不做任何事情。代碼是用C#編寫的,但你得到的基本思想是:

{ 
    public class NonSmoothScrollExpandableListView: ExpandableListView 
    { 
     public NonSmoothScrollExpandableListView(Context context): base(context) 
     { 

     } 

     public NonSmoothScrollExpandableListView(Context context, IAttributeSet attrs) : 
      base(context, attrs) 
      { 
     } 

     public NonSmoothScrollExpandableListView(Context context, IAttributeSet attrs, int defStyle) : 
      base(context, attrs, defStyle) 
      { 
     } 

     public override void SmoothScrollByOffset(int offset) 
     { 
     } 

     public override void SmoothScrollBy(int distance, int duration) 
     { 
     } 

     public override void SmoothScrollToPosition(int position) 
     { 
     } 

     public override void SmoothScrollToPosition(int position, int boundPosition) 
     { 
     } 

     public override void SmoothScrollToPositionFromTop(int position, int offset) 
     { 
     } 

     public override void SmoothScrollToPositionFromTop(int position, int offset, int duration) 
     { 
     } 
    } 
}