提问者:小点点

findLastVisibleItemPotion-返回错误的值,如果在NestedScrollView中RecyclView


我在NestedScrollView中回收了一个视图

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

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

这是主活动的完整代码

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方法应该在延迟后工作,因为findLastVisibleItemPoition在将项目添加到列表之前返回值。结果它返回了错误的值。例如添加前10个项目后为-1。

我的问题:当NestedScrollView中的RecyclerView找到最后可见项目位置返回错误的值并且即使有足够的项目也无法停止数据加载时。当RecyclerView不在NestedScrollView中时,没有这样的问题。

我的问题:如何获得最后可见的项目位置从RecyclerView当它在NestedScrollView?


共3个答案

匿名用户

问题是,当recyclerView父项是像nestedScroll一样的可滚动ViewGroup时,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. 使用getChildAt()方法在NestedScrollingView中查找RecyclerView
  2. 从RecyclerView获取LayoutManager
  3. 查找lastVisiblePosition()

这是我为嵌套ScrollingView的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;
            }
        }
    }
}

祝你好运!

匿名用户

简单的解决方案:将下面的scrollChangeListener放在NestedScrollView上,并从Recyclestview中删除scrollListener。并在下面提到的条件下将Load more Pagination功能放在onScrollChange上。就是这样。

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

                  // Do Load more feature here
                }
            }
        });
    }

..为我工作..