如何检查滚动视图是否滚动到底部 android studio

How to check if scroll view is scroll to bottom android studio

我让 Scrollview 覆盖里面的很多元素。我想检查 scrollview 是否滚动到 scrool 的底部。如何实施。请任何人推荐我。谢谢

<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>

您可以通过比较 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
}

创建自定义滚动视图如下:

public class CustomScrollView extends 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();
    }
}

在你的主要activity:

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>

并不是说我的是完美的,但它很快,很脏而且很管用。它基于这样一个事实,即一旦你放慢速度,回收器状态将进入 'settling' 但这本身并不够好,因为它会在滚动结束时触发。因此,我们添加了 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()
            }
        }
    })