gpt4 book ai didi

android - ListView 禁用标题 View 的垂直滚动

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:53 26 4
gpt4 key购买 nike

我有一个 ListView,其 header 中有 com.google.android.gms.maps.MapView。当我滚动列表时,我希望 MapView 被隐藏并且它工作得很好。但是,当我从上到下或向下滚动标题时,我不希望 ListView 执行滚动 - 我希望它允许 MapView 执行其缩放/滚动操作。
我知道它是 bad idea一起使用 ListView/MapView ,但我需要找到一个解决方案。我考虑过在 ListView 实例上设置 OnTouchListener 并在需要时将其事件分派(dispatch)给 MapView 但它看起来不是一个非常简单的解决方案。

最佳答案

最后我通过扩展 ListView 类解决了这个问题:

public class SingleScrollableHeaderListView extends ListView {
private View mHeaderView;

public SingleScrollableHeaderListView(Context context) {
super(context);
}

public SingleScrollableHeaderListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SingleScrollableHeaderListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mHeaderView != null) return mHeaderView.onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
}

@Override
public void addHeaderView(View v, Object data, boolean isSelectable) {
if (mHeaderView != null) removeHeaderView(mHeaderView);
mHeaderView = v;
super.addHeaderView(v, data, isSelectable);
}
}

重要的部分是 onInterceptTouchEvent() 方法 - 这里我将 MotionEvent 实例传递给标题 View (如果它存在),否则我让 ListView 处理运动事件。

关于android - ListView 禁用标题 View 的垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com