我有Scrollview里面的很多元素。我想检查scrollview是否滚动到滚动的底部。如何实现。请有人推荐我。谢谢。
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:showDividers="end|middle" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="
text.......
" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="
text.......
" />
</LinearLayout>
</ScrollView>
创建自定义滚动视图如下:
公共类CustomScrollView扩展了ScrollView {OnBottomReachedListener mListener;
public CustomScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY())) - view.getPaddingBottom();
if (diff <= 0 && mListener != null) {
mListener.onBottomReached();
}
super.onScrollChanged(l, t, oldl, oldt);
}
// Getters & Setters
public OnBottomReachedListener getOnBottomReachedListener() {
return mListener;
}
public void setOnBottomReachedListener(
OnBottomReachedListener onBottomReachedListener) {
mListener = onBottomReachedListener;
}
//Event listener.
public interface OnBottomReachedListener {
public void onBottomReached();
}
}
在您的主要活动中:
CustomScrollView scrollView =(CustomScrollView)findViewById(R.id.scrollView);
scrollView.setOnBottomReachedListener(new CustomScrollView.OnBottomReachedListener(){
@Override
public void onBottomReached() {
// ScrollView Reached bottom
}
});
在您的xml文件中:
<CustomScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="wrap_content" >
</CustomScrollView>
你可以通过比较ScrollView
和你的LinearLayout
的高度来做到这一点:
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout);
if(linearLayout.getMeasuredHeight() <= scrollView.getScrollY() +
scrollView.getHeight()) {
//scroll view is at the very bottom
}
else {
//scroll view is in somewhere middle
}
不是说我的是完美的,但它很快,很脏,而且很有效。这是基于这样一个事实:一旦你减速,回收国的状态将会“稳定”,但这本身并不够好,因为任何时候滚动结束都会触发。因此,我们添加了500毫秒的检查,因为一旦你触到底部,'onScrolled'功能就会停止触发,因为你在技术上不会滚动并且会停止更新时间。
generic_list_frag_recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener(){
private var lastPositiveScroll: Long = SystemClock.uptimeMillis()
// This was implemented to take out the possibility of scrolling up and stopping at the top (negative dy value)
private var lastScrollWasPositive = false
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
Timber.d("Scrolled dy: $dy")
if(dy > 0){
lastPositiveScroll = SystemClock.uptimeMillis()
lastScrollWasPositive = true
}else{
lastScrollWasPositive = false
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if(lastScrollWasPositive && newState == RecyclerView.SCROLL_STATE_SETTLING && (SystemClock.uptimeMillis() - lastPositiveScroll > 500)){
Timber.d("User reached bottom of list!")
onUserScrollToBottom.invoke()
}
}
})