为什么RecyclerView.OnScrollListener不是一个接口,但抽象类? [英] Why RecyclerView.OnScrollListener is not an interface but an abstract class?
问题描述
要实现无尽滚动模式到 RecyclerView
我想创建一个类,像
To implement Endless Scroll pattern in to the RecyclerView
I want to create a class like
public class EndlessScrollAdapter<VH extends ViewHolder>
extends RecyclerView.Adapter<VH> implements RecyclerView.OnScrollListener {
}
由于 EndlessScrollAdapter
应该负责这两个数据的滚动事件处理,这是实现它的最便捷的方式。
Because EndlessScrollAdapter
should be responsible for both of the data and scroll event handling, this is the most convenient way to implement it.
不过,由于 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){}
}
我不能让 EndlessScrollAdapter
实施 OnScrollListener
。
所以,正如标题所说,有没有什么好的理由 OnScrollListener
是一个类,而不是一个接口?因为我认为它应该是一个接口。
So as the title says, is there any good reason for OnScrollListener
to be a class rather than an interface? Because I think it should be an interface.
推荐答案
我有同样的问题,它肯定是设计,因为在这个错误报告回答:
I had the same question, and it's definitely as designed, as answered in this bug report:
<一个href="https://$c$c.google.com/p/android/issues/detail?id=79283">https://$c$c.google.com/p/android/issues/detail?id=79283
抽象类允许的框架,以增加新的方法不会破坏现有实现。
Abstract classes allow frameworks to add new methods without breaking existing implementations.
也介绍了它可以在这里找到的差异:
Also the diff that introduced it can be found here:
<一个href="https://android.googlesource.com/platform/frameworks/support/+/cef7b49%5E!/">https://android.googlesource.com/platform/frameworks/support/+/cef7b49%5E!/
这个变化增加了RecyclerView作为第一个参数滚动相关的回调。
This change adds RecyclerView as a first parameter to the scroll related callbacks.
这也fxies一个在那里滚动回调是被称为瓦特/预期滚动的bug 量,而不是真正的滚动量。
It also fxies a bug where scroll callback was being called w/ the intended scroll amount instead of the real scroll amount.
我也改变了它是一个抽象类,而不是一个接口,使未来 更改更加容易。
I also changed it to be an abstract class instead of an interface to make future changes easier.
不知道我同意了变化,个人,但亚去。
Not sure I agree with the change, personally, but there ya go.
这篇关于为什么RecyclerView.OnScrollListener不是一个接口,但抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!