Android拖动和触摸事件

时间:2018-06-15 15:45:08

标签: android android-toolbar ontouchevent

enter image description here

public class Toolbar extends RelativeLayout {

private Context context;
private LinearLayoutCompat li;
private long startClickTime;
private  static  final int MAX_CLICK_DURATION = 200;

public Toolbar(Context context) {
    super(context);
    this.context = context;
    init();
}

public Toolbar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    init();
}

public Toolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    init();
}

public void init(){
    LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.button,this,true);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    switch (ev.getActionMasked()){

        case MotionEvent.ACTION_DOWN:{
            Log.w("test","you touched down");
            startClickTime = Calendar.getInstance().getTimeInMillis();
            break;
        }

        case MotionEvent.ACTION_UP:{
            long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
            Log.w("test","you touched up");
            if (clickDuration < MAX_CLICK_DURATION) {
                return true;
            }
            else {
                onInterceptTouchEvent(ev);
            }
        }

    }
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(event);
}

我想实现响应拖动事件的自定义android工具栏。 但是当我触摸工具栏区域时,搜索视图和按钮没有响应。 所以我现在试图区分拖动和触摸事件。 我想在拖动时调用ontouchevent。或者我想在触摸事件的情况下将事件发送到子视图。 我该怎么办?

0 个答案:

没有答案