android listview标题通过触摸查看后面

时间:2016-10-16 21:57:46

标签: android listview android-linearlayout

我有一个listview,其中包含一个完全透明的自定义类标题。在列表视图后面,我有一个通过透明标题显示的mapview。

我正在努力使这项工作成为如果用户滚动我触摸列表视图中的任何行,那么列表视图会滚动。但是如果用户正在触摸列表视图的顶部透明标题部分,那么列表视图不应该拦截触摸,而应该将其传递到后面的mapview(这允许用户在mapview上平移/缩放)。

我目前无法实现这一点,因为listview一直在偷窃。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

nvm,我在类似的问题上找到了解决方案。不知道为什么我在发布这个问题之前没有找到它,但之后,SO在右边的相关问题中向我展示了。

解决方案: how to make header of listview not to consume the touch event

使用自定义ListView类:

package xxx.xxx.xxxxxx;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

public class HeaderUntouchableListView extends ListView {
    private View mHeaderView;
    private boolean isDownEventConsumed;

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

    @Override
    public void addHeaderView(View v) {
        super.addHeaderView(v);
        this.mHeaderView = v;
    }

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

    /**
     * list header should not consume the event, and list item should consume the event
     * consumed here is replaced with super.dispatchTouchEvent(motionEvent)
     * @param motionEvent
     * @return is event consumed
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        if(mHeaderView == null) return super.dispatchTouchEvent(motionEvent);
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
            //if touch header not to consume the event
            Rect rect = new Rect((int) mHeaderView.getX(), (int) mHeaderView.getY(), mHeaderView.getRight(), mHeaderView.getBottom());
            if(rect.contains((int)motionEvent.getX(), (int)motionEvent.getY())){
                isDownEventConsumed = false;
                return isDownEventConsumed;
            }else {
                isDownEventConsumed = true;
                return super.dispatchTouchEvent(motionEvent);
            }
        }else{
            //if touch event not consumed, then move/up event should be the same
            if(!isDownEventConsumed)return isDownEventConsumed;
            return super.dispatchTouchEvent(motionEvent);
        }
    }
}