我在expandable listview中使用,当我打开listview中的一个项目时,我的滚动会自动关注打开的项目,我可以阻止列表关注新项目并保持在同一个地方吗?我试图从公开的视图中删除焦点,但它没有用。
答案 0 :(得分: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;
}
});
如果您使用该事件,则在需要时需要复制一些默认行为。这包括播放声音效果和调用expand / collapse-listeners。
对于默认行为的参考,我将发布它,取自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;
}
答案 1 :(得分:0)
一种hacky方式但你可以扩展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)
{
}
}
}