问题
i use in expandable listview and when i open one of the items in the listview my scroll is automatically focus on the item that open, can i prevent from the list to focus on the new item and stay in the same place? i've try to remove the focusable from the open's view but it didn't work.
回答1:
You need to override the OnGroupClickListener and consume the event. This will avoid the ExpandableListView doing the default action, which at least when compiling against 4.3 is to smoothScroll to the position.
Here is an implementation example:
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;
}
});
If you consume the event, there is some default behavior you need to replicate if you need it. This includes playing sound effects and calling expand/collapse-listeners.
For reference against the default behavior, I will post it, taken from the Android source code (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;
}
回答2:
Kind of a hacky way but you can extend ExpandableListView
and override its ] smooth scroll methods and not do anything. Code is in C# but you get the basic idea:
{
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)
{
}
}
}
来源:https://stackoverflow.com/questions/15106471/android-disable-auto-scroll-in-expandable-list-view