Class must either be declared abstract or?

2019-06-07 01:27发布

I am new to android. I have always used snippets to create my app. So I copied material style drawer from this here.

Now I am facing a problem in this part of the code:

static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private ClickListener clickListener;

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

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }
}

In this snippet it gives error . I can give image of error

I tried to make the class abstract, but then it interferes with oncreate bundle code. Any help would be appreciated

2条回答
姐就是有狂的资本
2楼-- · 2019-06-07 01:50

This message tells you, that the RecyclerView.OnItemTouchListener (which is an interface) defines a method called onRequestDisallowInterceptTouchEvent(boolean). This method needs to be implemented by every class which implement the OnItemTouchListener interface.

So just add this method with an empty body...

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}

... and have a look on interfaces in the Java language: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

查看更多
Rolldiameter
3楼-- · 2019-06-07 02:04

the interface you are implementing has three methods. You are implementing already two of them. You need to add but not

 onRequestDisallowInterceptTouchEvent(boolean disallowIntercept)

which is called when a child of RecyclerView does not want RecyclerView and its ancestors to intercept touch events with. You can simply add

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}

to your class

查看更多
登录 后发表回答