【问题标题】:findLastVisibleItemPosition is returning wrong value if RecyclerView inside NestedScrollView如果 RecyclerView 在 NestedScrollView 内,findLastVisibleItemPosition 返回错误值
【发布时间】:2018-09-07 08:06:59
【问题描述】:

我在 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 获取最后一个可见项目的位置?

【问题讨论】:

  • 有你设置recyclerView.setNestedScrollingEnabled(false);
  • 你用过 findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true)
  • @NileshRathod 我尝试将nestedScrollingEnabled 设置为false,但没有帮助
  • this 的问题吗?
  • 我实际上会将这些“上方”视图直接放入RecyclerView 本身。这意味着适配器必须管理该数据,这可能会干扰关注点分离;但是它可以完成工作。现在您没有发布包含所有布局的整个活动布局,所以我不知道该方法是否合适。发布更多布局和代码,然后我可能会为您回答这个问题。列表模型不一定必须是同质的。考虑到RecyclerView.Adapter 具有getItemViewType() 方法,因此您可以根据项目位置膨胀不同的视图。

标签: android android-recyclerview android-nestedscrollview


【解决方案1】:

问题是当 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
        }
    });
}

对我来说很好用,如果不行请告诉我

【讨论】:

  • 太棒了,非常感谢。解决了我的问题!
【解决方案2】:

你可以试试这个方法:

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

祝你好运!

【讨论】:

    【解决方案3】:

    如果您的RecyclerViewNestedScrollView 中,则需要将recyclerView.setNestedScrollingEnabled(false) 添加到其中。

    还有另一个注意事项(与您的问题无关的是,您绝对应该保留对那些 RxJava 订阅的引用并将它们放在 Fragment/Adtivity 的 onStop 上,因为它们会导致内存泄漏问题。

    【讨论】:

    • setNestedScrollingEnabled 不能解决问题。 findLastVisibleItemPosition 每次仍然会返回最后一个位置
    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多