为什么RecyclerView.OnScrollListener不是一个接口而是一个抽象类?

Onu*_*nur 17 android endlessscroll android-recyclerview

要实现Endless Scroll模式,RecyclerView我想要创建一个类

public class EndlessScrollAdapter<VH extends ViewHolder> 
        extends RecyclerView.Adapter<VH> implements RecyclerView.OnScrollListener {
}
Run Code Online (Sandbox Code Playgroud)

因为EndlessScrollAdapter应该负责数据和滚动事件处理,这是实现它的最方便的方法.

但是,因为在recyclerview-v7-21.0.3,这样OnScrollListener宣布

/**
 * An OnScrollListener can be set on a RecyclerView to receive messages
 * when a scrolling event has occurred on that RecyclerView.
 *
 * @see RecyclerView#setOnScrollListener(OnScrollListener)
 */
abstract static public class OnScrollListener {
    /**
     * Callback method to be invoked when RecyclerView's scroll state changes.
     *
     * @param recyclerView The RecyclerView whose scroll state has changed.
     * @param newState     The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
     *                     {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
     */
    public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

    /**
     * Callback method to be invoked when the RecyclerView has been scrolled. This will be
     * called after the scroll has completed.
     *
     * @param recyclerView The RecyclerView which scrolled.
     * @param dx The amount of horizontal scroll.
     * @param dy The amount of vertical scroll.
     */
    public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
}
Run Code Online (Sandbox Code Playgroud)

我无法EndlessScrollAdapter实施OnScrollListener.

正如标题所说,是否有充分的理由OnScrollListener成为一个阶级而不是一个界面?因为我认为它应该是一个界面.

kco*_*ock 14

我有同样的问题,这绝对是设计的,正如这个错误报告所回答的那样:

https://code.google.com/p/android/issues/detail?id=79283

抽象类允许框架在不破坏现有实现的情况下添加新方法.

介绍它的差异也可以在这里找到:

https://android.googlesource.com/platform/frameworks/support/+/cef7b49%5E!/

此更改将RecyclerView添加为滚动相关回调的第一个参数.

它还会出现一个错误,在这个错误中,滚动回调被调用w /预定的滚动量而不是真正的滚动量.

我还将其更改为抽象类而不是接口,以使未来的更改更容易.

不确定我个人同意这个改变,但是你去了.


Bla*_*elt 6

我无法使EndlessScrollAdapter实现OnScrollListener.

这是真的,但你可以有一个专门的类extends RecyclerView.OnScrollListener(具体的实例 RecyclerView.OnScrollListener).例如

private class MyScrollListener extends RecyclerView.OnScrollListener {
   // abstract methods implemenations
}
Run Code Online (Sandbox Code Playgroud)

而你所需要的只是

mRecyclerView.addOnScrollListener(new MySCrollListener());
Run Code Online (Sandbox Code Playgroud)