簡體   English   中英

嵌套滾動視圖中的 recyclerview addOnScrollListener (endless scrolllistener)

[英]recyclerview inside nestedscrollview addOnScrollListener (endless scrolllistener)

我在嵌套滾動視圖中使用 recyclerview 如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/mainScrollView"
    android:layout_marginBottom="?attr/actionBarSize"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:overScrollMode="never"
        android:layout_height="250dp"
        android:focusableInTouchMode="true"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/VerticalRV"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

問題是,當我想為這個回收器視圖設置一個監聽器( onScrollListener )時,它無論如何都無法工作。 我也調試了這段代碼,但它甚至沒有捕捉到事件。 下面是java代碼:

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
MyAdapter adapter = new MyAdapter(verticalShownData, this.getActivity());
recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new HideShowScrollListener() {
     @Override
     public void onHide() {
          animateCallback.animateHide();
     }

     @Override
     public void onShow() {
          animateCallback.animateShow();
     }
});

我怎樣才能讓這個聽眾工作? 提前致謝。

我知道這可能有點晚了,但也許這會對某人有所幫助。 我還遇到了一個問題,即一旦數據插入 RecyclerView,我的 recyclerview 就會不斷調用 RecyclerView 的 onScrolled 方法。 經過一些調試后,我發現,我將 NestedScrollView 與 RecyclerView 一起使用。 所以我搜索了一些與它相關的東西,我在這里找到了一個很棒的medium.com教程。 它工作得很好。

從這里的鏈接添加一些代碼。

mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if(v.getChildAt(v.getChildCount() - 1) != null) {
            if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                    scrollY > oldScrollY) {

                int visibleItemCount = mLayoutManager.getChildCount();
                int totalItemCount = mLayoutManager.getItemCount();
                int pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (!isLoadingData()) {

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {

                         //Load Your Data
                    }
                }
            }
        }
    }
});

幾天后,我終於想出了一個解決方案。

解決方案是刪除NestedScrollView ,而是使用帶有標題的RecyclerView 所以RecyclerView的第一個元素是一個ViewPager ,其余的是其他對象。

我認為使用NestedScrollView作為RecyclerView父級並不是一個好習慣。 由於NestedScrollView強制其子項完全加載,這違反了RecyclerView的概念。

該解決方案的靈感來自@hister 在此線程上的回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM