18

要在RecyclerView我想创建一个类似的类中实现 Endless Scroll 模式

public class EndlessScrollAdapter<VH extends ViewHolder> 
        extends RecyclerView.Adapter<VH> implements RecyclerView.OnScrollListener {
}

因为EndlessScrollAdapter应该同时负责数据和滚动事件的处理,这是最方便的实现方式。

但是,因为 in recyclerview-v7-21.0.3OnScrollListener这样声明

/**
 * 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){}
}

我无法EndlessScrollAdapter实现OnScrollListener

所以正如标题所说,有什么好的理由OnScrollListener成为一个类而不是一个接口?因为我认为它应该是一个接口。

4

4 回答 4

14

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

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

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

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

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

此更改将 RecyclerView 作为第一个参数添加到与滚动相关的回调中。

它还修复了一个错误,即使用预期的滚动量而不是实际的滚动量调用滚动回调。

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

不确定我个人是否同意这种改变,但你去吧。

于 2015-02-20T19:39:15.923 回答
6

我不能让 EndlessScrollAdapter 实现 OnScrollListener。

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

private class MyScrollListener extends RecyclerView.OnScrollListener {
   // abstract methods implemenations
}

而你需要的只是

mRecyclerView.addOnScrollListener(new MySCrollListener());
于 2015-02-20T19:32:06.930 回答
1
 yourRecyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    });
于 2016-05-30T07:21:58.063 回答
0

避免在你的方法中设置监听器的想法

...
recyclerView.addOnScrollListener(new ScrollListener());
...

并创建一个内部侦听器类

private class ScrollListener extends RecyclerView.OnScrollListener {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState){
        // your code there
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        // your code there
    }
}
于 2016-06-22T13:07:58.023 回答