手指松动前不要触摸新活动。错误:由于没有窗口焦点而导致事件下降:MotionEvent

时间:2018-12-24 20:17:29

标签: android android-activity event-handling ontouchlistener ontouchevent

长按后,我的第一个活动(MainActivity)打开一个新活动,当用户抬起手指时,我的第二个活动应完成。 我的问题是,第一个活动仍然可以触摸,而不是第二个活动,而且我遇到了错误:

无论我如何尝试使用第二个屏幕(布局,新片段,新活动...),底部视图都可以触摸到(MainActivity上的回收站仍在滚动),而新布局不会得到触摸。用户抬起手指后,第二个活动/片段开始跟踪事件。

[MainActivity]:由于没有窗口焦点而导致的删除事件:MotionEvent {action = ACTION_MOVE,actionButton = 0,id [0] = 0,x [0] = 292.53247,y [0] = 1557.9918,toolType [0] = TOOL_TYPE_FINGER,buttonState = 0,metaState = 0,标志= 0x0,edgeFlags = 0x0,pointerCount = 1,historySize = 2,eventTime = 880322463,downTime = 880302619,deviceId = 9,source = 0x1002}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:id="@+id/group_drag_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/drag_blur"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:clickable="false"
        android:focusable="false"
        android:visibility="gone"></FrameLayout>

</FrameLayout>

第二次活动:

public class GroupDragForArchive extends BaseActivity {

@BindView(R2.id.group_drag_layout)
FrameLayout frameLayout;

@BindView(R2.id.drag_blur)
FrameLayout dragBlur;

private static final String DEBUG_TAG = "Gestures";
private Bitmap bitmap;

@Override
protected int getLayoutId() {
    return R.layout.group_drag_for_archive_layout;
}

@Override
public boolean shouldBeLoggedInForActivity() {
    return true;
}

@Override
public boolean shouldPollInActivity() {
    return false;
}

@Override
protected void onCreated(Bundle savedInstanceState) {

}

public static void startActivity(Activity activity, Bitmap imageFile) {
    Intent intent = new Intent(activity, GroupDragForArchive.class);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    imageFile.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    intent.putExtra("BitmapImage", byteArray);
    activity.overridePendingTransition(0, 0);
    activity.startActivityForResult(intent, IntConst.RequestCodes.REQUEST_DRAG_FOR_ARCHIVE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    byte[] byteArray = getIntent().getByteArrayExtra("BitmapImage");
    bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
    frameLayout.setBackgroundDrawable(ob);
    this.getCurrentFocus();

}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onDragGroupEvent(MotionEvent event) {
    Log.d(DEBUG_TAG,"Action was onDragGroupEvent" + event);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,"Action was MOVE");
        case (MotionEvent.ACTION_UP) :
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,"Action was CANCEL");
            finish();
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                    "of current screen element");
        default :
            return false;
    }
}

然后用该代码打开我的活动:

public void stopClick() {
    try {
        View v1 = mDrawerLayout.getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        GroupDragForArchive.startActivity(this, bitmap);
    } catch (Throwable e) {
        e.printStackTrace();
    }

}

0 个答案:

没有答案