如何使用gesturedetector将触摸事件传递到底部视图

5q4ezhmt  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(234)

我用的是来自
https://www.journaldev.com/28900/android-gesture-detectors
我想做的是,当我单触tvtop时,我想将触摸事件传递给tvbottom,tvbottom显示toast。
此外,tvtop与其他触摸事件保持在顶部。
当我刷tvtop时,它不能将touch事件传递给tvbottom。
只有单触必须将触摸事件传递给tvbooth。
如果我返回假tvboottom得到传递的touch事件,但是使用gesturedector它不工作。

tvTop.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
   Toast.makeText(getApplicationContext(),"i am top",Toast.LENGTH_SHORT).show();
    return false;
}
});

我怎么能这么做!
活动\u main.xml

<?xml version="1.0" 
encoding="utf-8"?>
<FrameLayout 

xmlns:android="http://schemas.
 android.com/apk/res/android"
android:id="@+id/holder"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

</FrameLayout>

tv\u bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_bottom"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:background="#FFC700"
android:textColor="#000000"
android:textSize="15sp"

android:text="tv bottom"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingLeft="10dp"

 />
</LinearLayout>

tv\u top.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_top"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="Orientation">
<TextView
    android:background="#FF0000"
    android:textColor="#FFFFFF"
    android:textSize="15sp"

    android:text="tv top"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"

    />
</LinearLayout>

主活动.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

FrameLayout holder;
LinearLayout tvTop,tvBottom;
GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View tv_top = getLayoutInflater().inflate(R.layout.tv_top, null);
    View tv_bottom = getLayoutInflater().inflate(R.layout.tv_bottom, null);

    tvTop = tv_top.findViewById(R.id.tv_top);
    tvBottom = tv_bottom.findViewById(R.id.tv_bottom);

    holder = findViewById(R.id.holder);
    holder.addView(tvBottom);
    holder.addView(tvTop);

    tvBottom.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            Toast.makeText(getApplicationContext(),"i am bottom",Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    tvTop.setOnTouchListener(new OnSwipeTouchListener(this) {
        public void onSwipeTop() {
            Toast.makeText(getApplicationContext(), "Swiped top", Toast.LENGTH_SHORT).show();
        }

        public void onSwipeRight() {
            Toast.makeText(getApplicationContext(), "Swiped right", Toast.LENGTH_SHORT).show();
        }

        public void onSwipeLeft() {
            Toast.makeText(getApplicationContext(), "Swiped left", Toast.LENGTH_SHORT).show();
        }

        public void onSwipeBottom() {
            Toast.makeText(getApplicationContext(), "Swiped bottom", Toast.LENGTH_SHORT).show();
        }

    });

}

class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context ctx) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 300;
        private static final int SWIPE_VELOCITY_THRESHOLD = 300;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.i("TAG", "onSingleTapConfirmed:");
            Toast.makeText(getApplicationContext(), "Single Tap Detected", Toast.LENGTH_SHORT).show();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.i("TAG", "onLongPress:");
            Toast.makeText(getApplicationContext(), "Long Press Detected", Toast.LENGTH_SHORT).show();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Toast.makeText(getApplicationContext(), "Double Tap Detected", Toast.LENGTH_SHORT).show();
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                        result = true;
                    }
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题