-3

plz help getting error View cannot be converted to MotionEvent and MotionEvent cannot be converted to View ..

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnItemTouchListener;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;



   public class RecyclerTouchListener implements OnItemTouchListener {
    private ClickListener clickListener;
    private GestureDetector gestureDetector;

    public interface ClickListener {
        void onClick(View view, int i);

        void onLongClick(View view, int i);
    }

    public void onRequestDisallowInterceptTouchEvent(boolean z) {
    }

    public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
    }

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
        this.clickListener = clickListener;
        this.gestureDetector = new GestureDetector(context, new SimpleOnGestureListener() {
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return true;
            }

            public void onLongPress(MotionEvent motionEvent) {
                motionEvent = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
                if (motionEvent != null && clickListener != null) {
                    clickListener.onLongClick(motionEvent, recyclerView.getChildPosition(motionEvent));
                }
            }
        });
    }

    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        View findChildViewUnder = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
        if (!(findChildViewUnder == null || this.clickListener == null || this.gestureDetector.onTouchEvent(motionEvent) == null)) {
            this.clickListener.onClick(findChildViewUnder, recyclerView.getChildPosition(findChildViewUnder));
        }
        return null;
    }
}

error list in single file

1-incomparable types: boolean and <null>

2-incompatible types: cannot be converted to boolean

3-incompatible types: View cannot be converted to MotionEvent

4-incompatible types: MotionEvent cannot be converted to View

don't know how I can solve this problem.

Thank you in advance for your help

I hope to be clear, I'm sorry if there are mistakes

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Welcome to StackOverflow! *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a **specific problem or error** and the **shortest code necessary** to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.* For more information on how to ask a good question, consider [taking the tour](https://stackoverflow.com/tour/), or checking out the [help center](https://stackoverflow.com/help/how-to-ask). – ricky3350 Sep 21 '18 at 18:13

1 Answers1

0

On motionEvent = recyclerView.findChildViewUnder

findChildViewUnder method returns a View. A MotionEvent is a type of InputEvent not a View


this.gestureDetector.onTouchEvent(motionEvent) == null isn't a valid comparison because a boolean primitive can never be null, only true of false if the touch is intercepted.

Similarly, onInterceptTouchEvent cannot return null


You should be using setOnClickListener rather than using a GestureDetector and manually invoking onClick from the listener

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245