RecylerView在滚动时未调用“滚动添加”以刷新布局

时间:2018-10-11 05:31:32

标签: android android-recyclerview listener

我已将Recyclerview放入“滑动以刷新”布局中。滑动即可刷新代码。虽然为Endless recylerview添加了对recylerview的滚动侦听器。在滚动侦听器上添加从未被调用。

main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/WhiteOpacity">
      <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <android.support.v7.widget.RecyclerView
          android:id="@+id/rcv_home_screen"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/_5sdp"
          android:scrollbars="vertical"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" />
      </android.support.v4.widget.SwipeRefreshLayout>
    </android.support.constraint.ConstraintLayout>

Listener Add OnScroll

 recylerview.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                 Log.d("test", "working);
                }
            }
        });

2 个答案:

答案 0 :(得分:0)

使用以下代码。即使您的Go to you Project >> app >> libs Empty that folder and then Sync your project . It will download all dependencies again. 嵌套在RecyclerView

中,滚动侦听器仍然有效

第1步::将布局管理器设置为您的recyclerview。

SwipeRefreshLayout

步骤2:通过扩展LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recylerview.setLayoutManager(linearLayoutManager);

来实现自定义滚动侦听器
RecyclerView.OnScrollListener

步骤3:将滚动侦听器添加到您的public abstract class EndlessScrollListener extends RecyclerView.OnScrollListener{ private int visibleThreshold = 5; private int currentPage = 0; private int previousTotalItemCount = 0; private boolean loading = true; private int startingPageIndex = 0; RecyclerView.LayoutManager mLayoutManager; public EndlessScrollListener(LinearLayoutManager layoutManager) { this.mLayoutManager = layoutManager; } public EndlessScrollListener(GridLayoutManager layoutManager) { this.mLayoutManager = layoutManager; visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); } public EndlessScrollListener(StaggeredGridLayoutManager layoutManager) { this.mLayoutManager = layoutManager; visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); } public int getLastVisibleItem(int[] lastVisibleItemPositions) { int maxSize = 0; for (int i = 0; i < lastVisibleItemPositions.length; i++) { if (i == 0) { maxSize = lastVisibleItemPositions[i]; } else if (lastVisibleItemPositions[i] > maxSize) { maxSize = lastVisibleItemPositions[i]; } } return maxSize; } @Override public void onScrolled(RecyclerView view, int dx, int dy) { int lastVisibleItemPosition = 0; int totalItemCount = mLayoutManager.getItemCount(); if (mLayoutManager instanceof StaggeredGridLayoutManager) { int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null); lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions); } else if (mLayoutManager instanceof GridLayoutManager) { lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition(); } else if (mLayoutManager instanceof LinearLayoutManager) { lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition(); } if (totalItemCount < previousTotalItemCount) { this.currentPage = this.startingPageIndex; this.previousTotalItemCount = totalItemCount; if (totalItemCount == 0) { this.loading = true; } } if (loading && (totalItemCount > previousTotalItemCount)) { loading = false; previousTotalItemCount = totalItemCount; } if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) { currentPage++; onLoadMore(currentPage, totalItemCount, view); loading = true; } } public void resetState() { this.currentPage = this.startingPageIndex; this.previousTotalItemCount = 0; this.loading = true; } public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view); }

recyclerview

答案 1 :(得分:0)

您应使用特定于RecyclerView的addOnScrollListener