简体   繁体   English

ListView禁用标题视图的垂直滚动

[英]ListView disable vertical scrolling of header view

I have a ListView with com.google.android.gms.maps.MapView in its header. 我的标题中有一个带有com.google.android.gms.maps.MapViewListView When I'm scrolling the list I want the MapView to be hidden and it works just fine. 当我滚动列表时,我希望隐藏MapView ,它可以正常工作。 But, when I'm scrolling the header from up to down, or down up I don't want ListView to perform scrolling - I want it to allow MapView to perform its zoom/scroll operations. 但是,当我从上到下滚动标题或向下滚动时我不希望ListView执行滚动 - 我希望它允许MapView执行其缩放/滚动操作。
I know that it's bad idea to use ListView / MapView together, but I need to find a solution. 我知道一起使用ListView / MapView是个坏主意 ,但我需要找到一个解决方案。 I thought about setting the OnTouchListener on the ListView instance and dispatching its events to the MapView when needed but it looks like a not very straightforward solution. 我考虑过在ListView实例上设置OnTouchListener并在需要时将其事件调度到MapView ,但它看起来不是一个非常简单的解决方案。

In the end I've solved it by extending the ListView class: 最后我通过扩展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);
    }
}

The significant part is the onInterceptTouchEvent() method - here I'm passing the MotionEvent instance to the header view if it exists, otherwise I let the ListView to deal with the motion event. 该显著的部分是onInterceptTouchEvent()方法-在这里我传递的MotionEvent实例的标题视图,如果它存在,否则我让ListView来处理运动事件。

尝试在MapView上的OnTouch中调用requestDisallowInterceptTouchEvent(true)到listview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM