【问题标题】:ListView disable vertical scrolling of header viewListView 禁用标题视图的垂直滚动
【发布时间】:2014-05-20 20:28:50
【问题描述】:

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

【问题讨论】:

    标签: android android-listview android-mapview vertical-scrolling


    【解决方案1】:

    最后我通过扩展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 实例传递给标题视图,否则我让ListView 处理运动事件。

    【讨论】:

    • 感谢您提供这么好的解决方案。这就是我想要的。+1 为您的回答。
    【解决方案2】:

    在 MapView 上的 OnTouch 中尝试向列表视图调用 requestDisallowInterceptTouchEvent(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 2019-06-27
      • 2019-03-07
      • 2016-09-01
      • 2017-07-30
      • 1970-01-01
      • 2020-11-14
      • 2011-11-01
      相关资源
      最近更新 更多