如果NestedScrollView中的RecyclerView,则findLastVisibleItemPosition返回错误的值

时间:2018-09-07 08:06:59

标签: android android-recyclerview android-nestedscrollview

我在NestedScrollView中有RecyclerView

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

我在大型项目中遇到了这个问题,为了找到解决方案,我创建了没有其他视图的新项目。

这是MainActivity的完整代码

class MainActivity : AppCompatActivity() {

    var mItems = mutableListOf<String>()
    var mAdapter = MyAdapter(mItems)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = mAdapter

        delayedLoadDataIfPossible(100)

        recycler.viewTreeObserver.addOnScrollChangedListener {
            delayedLoadDataIfPossible(100)
        }

    }

    private fun delayedLoadDataIfPossible(delay: Long) {
        Observable.timer(delay, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    var scrollingReachedEnd = isScrollingReachedEnd()
                    if (scrollingReachedEnd) {
                        loadData()
                    }
                }
    }

    private fun isScrollingReachedEnd(): Boolean {
        val layoutManager = LinearLayoutManager::class.java.cast(recycler.layoutManager)
        val totalItemCount = layoutManager.itemCount
        val lastVisible = layoutManager.findLastVisibleItemPosition()
        return lastVisible + 5 >= totalItemCount
    }

    private fun loadData() {
        Observable.timer(5, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe { progress.visibility = View.VISIBLE }
                .doFinally { progress.visibility = View.GONE }
                .subscribe {
                    for (i in 1..10) {
                        mItems.add(i.toString())
                    }
                    mAdapter.notifyDataSetChanged()
                    delayedLoadDataIfPossible(100)
                }

    }

}

我正在使用 isScrollingReachedEnd 方法来确定滚动是否到达列表末尾。如果最后可见项目少于5个,则我正在尝试加载新数据。

loadData 模拟加载数据。它会将10个项目添加到列表中,并通知适配器有关更改。

delayedLoadDataIfPossible 方法应该在经过一些延迟后才能工作,因为findLastVisibleItemPosition在将项目添加到列表之前正在返回值。结果是它返回错误的值。例如,添加前10个项目后为-1。

我的问题:当NestedScrollView中的RecyclerView findLastVisibleItemPosition返回错误的值并且即使有足够的项目也无法停止数据加载时。当RecyclerView不在NestedScrollView内时,没有这种问题。

我的问题:当RecyclerView位于NestedScrollView内时,如何从RecyclerView获取最后可见的物品位置?

3 个答案:

答案 0 :(得分:2)

问题是,当recyclerView父级是可滚动的ViewGroup(如nestedScroll)时,即使其未显示,recyclerView中的所有项目也会被布局,因此 findLastVisibleItemPosition()将始终返回数组中的最后一项,即使它不可见,因此您必须使用父scrollView的滚动侦听器

package com.example.myApp;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;

public class MyNestedScroll extends NestedScrollView {
int SCREEN_HEIGHT ; 
private IsBottomOfList isBottomOfList ;
private LinearLayoutManager linearLayoutManager ;
private String TAG = "MyNestedScroll";

public MyNestedScroll(@NonNull Context context) {
    super(context);
init();
}

public MyNestedScroll(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
 init();
}

public MyNestedScroll(@NonNull Context context, @Nullable AttributeSet attrs, int 
defStyleAttr) {
    super(context, attrs, defStyleAttr);
init();
}
private void init(){
    SCREEN_HEIGHT = getContext().getResources().getDisplayMetrics().heightPixels;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (isBottomOfList != null){
        isBottomOfList.isBottomOfList(isVisible());
    }
}

public  boolean isVisible() {
    View view = null;
    int childCount ;
    if (linearLayoutManager != null) {
        childCount = linearLayoutManager.getChildCount();
        view = linearLayoutManager.getChildAt(childCount-1);
    }else {
        Log.v(TAG , "linearLayoutManager == null");

    }
    if (view == null) {
        Log.v(TAG , "view == null");
        return false;
    }
    if (!view.isShown()) {
        Log.v(TAG , "!view.isShown()");
        return false;
    }
    Rect actualPosition = new Rect();
    view.getGlobalVisibleRect(actualPosition);
    int height1 = view.getHeight();
    int height2 = actualPosition.bottom- actualPosition.top;
    Log.v(TAG , "actualPosition.bottom = "+actualPosition.bottom+"/ HomePage.SCREEN_HEIGHT ="+
            HomePage.SCREEN_HEIGHT+" / height1 = "+height1+"/ height2 = "+height2);
    return actualPosition.bottom<SCREEN_HEIGHT&&height1==height2;
}

public void setIsBottomOfList(IsBottomOfList isBottomOfList) {
    this.isBottomOfList = isBottomOfList;
}

public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
    this.linearLayoutManager = linearLayoutManager;
    Log.v(TAG , linearLayoutManager == null?"LM == NULL":"LM != NULL");
}

public interface IsBottomOfList {
    void isBottomOfList(boolean isBottom);
}
}

,并且在您的活动中仅使用以下行

// call this function after set layoutmanager to your recyclerView    
private void initScrollListener() {
nestedScroll.setLinearLayoutManager((LinearLayoutManager)bookingRecyclerView.getLayoutManager());

    nestedScroll.setIsBottomOfList(isBottom -> {
        if (isBottom){
           // here the last item of recyclerView is reached 
           // do some stuff to load more data
        }
    });
}

对我来说很好,否则请告诉我

答案 1 :(得分:0)

您可以尝试这种方法:

  1. 使用getChildAt()方法在NestedScrollingView中找到RecyclerView。
  2. 从RecyclerView获取LayoutManager。
  3. 找到lastVisiblePosition()。

这是我的NestedScrollingView的ScrollingListener代码:

@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

    int lastVisibleItemPosition = 0;
    int totalItemCount = layoutManager.getItemCount();

    if(v.getChildAt(v.getChildCount() - 1) != null) {
        if (scrollY >= (v.getChildAt(v.getChildCount()-1).getMeasuredHeight() - v.getMeasuredHeight())
                && scrollY > oldScrollY) {
            if (layoutManager instanceof LinearLayoutManager) {
                lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).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();
                loading = true;
            }
        }
    }
}

祝你好运!

答案 2 :(得分:0)

如果您的RecyclerView位于NestedScrollView内,则需要向其中添加recyclerView.setNestedScrollingEnabled(false)

另外要注意的一点(与您的问题无关的是,您绝对应该保留对这些RxJava订阅的引用,并将它们放置在Fragment / Adtivity的onStop上,因为它们可能导致内存泄漏问题。