【问题标题】:How to check if scroll view is scroll to bottom android studio如何检查滚动视图是否滚动到底部android studio
【发布时间】:2016-12-03 07:27:22
【问题描述】:

我有 Scrollview 覆盖里面的许多元素。我想检查scrollview是否滚动到scool的底部。如何实现。请任何人推荐我。谢谢。

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

【问题讨论】:

  • 我会建议使用 ListView 或 Recycler 视图进行滚动
  • @NitinKarande 如果我使用 Recyclerview 或 listview 我需要自定义页眉页脚....对我来说很难。
  • 当我们滚动底部并且您在最后一个项目中时您想要。显示页眉和页脚我是对的吗?
  • @NitinKarande 我想要的是我有过滤按钮,当我滚动到按钮时,排序按钮在顶部,它将加载更多数据。
  • 你确定你需要ScrollView,而不是ListViewRecycleView

标签: android scrollview onscrolllistener


【解决方案1】:

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

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();
    }
}

在您的主要活动中:

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>

【讨论】:

  • 错误活动 ComponentInfo{kh.com.iknow.scrolllistener/kh.com.iknow.scrolllistener.MainActivity}:android.view.InflateException:二进制 XML 文件第 13 行:膨胀类 CustomScrollView
  • 在您的 xml 中,您必须放置 CustomScrollView 的完整路径。例如;我已将我的 CustomScrollView 放入 com.mysample.customviews 包中,然后我必须编写
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      相关资源
      最近更新 更多