当 Recyclerview 在 NestedScrollview 中时,如何避免绑定所有项目?
Posted
技术标签:
【中文标题】当 Recyclerview 在 NestedScrollview 中时,如何避免绑定所有项目?【英文标题】:How to avoid binding of all items in Recyclerview, when it is inside NestedScrollview? 【发布时间】:2018-08-02 06:45:27 【问题描述】:我在 NestedScrollview 中有一个 Recyclerview。 Recyclerview 有不同的项目类型需要动态加载,但是当它在嵌套滚动中时,recyclerview 中的所有项目都绑定在一起。我怎样才能避免这种情况?我不需要任何分页。我只想最小化视图渲染。
【问题讨论】:
Pagination not work for the RecyclerView within NestedScrollView的可能重复 可能重复***.com/questions/31000081/… 您的问题解决了吗?你能分享你的解决方案吗:) 【参考方案1】:试试这个代码:
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public abstract class RecyclerViewByNestedScrollListener implements NestedScrollView.OnScrollChangeListener
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
// The minimum amount of pixels to have below your current scroll position
// before loading more.
private int visibleThresholdDistance = 500;
RecyclerView.LayoutManager mLayoutManager;
public RecyclerViewByNestedScrollListener(RecyclerView.LayoutManager layoutManager)
this.mLayoutManager = layoutManager;
@Override
public void onScrollChange(NestedScrollView scrollView, int x, int y, int oldx, int oldy)
// We take the last son in the scrollview
View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
int distanceToEnd = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));
int totalItemCount = mLayoutManager.getItemCount();
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount)
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0)
this.loading = true;
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount))
loading = false;
previousTotalItemCount = totalItemCount;
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && distanceToEnd <= visibleThresholdDistance)
currentPage++;
onLoadMore(currentPage, totalItemCount);
loading = true;
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
然后使用 NestedScrollView 作为你的 recyclerView 的父级。然后在代码中这样做:
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
NestedScrollView mScrNested=(NestedScrollView)findViewById(R.id.nestedScrollView);
mScrNested.setOnScrollChangeListener(new RecyclerViewByNestedScrollListener(layoutManager)
@Override
public void onLoadMore(int page, int totalItemsCount)
//load next page of your recyclerView
);
正如您在上面看到的,您必须使用nestedScorllView 设置加载更多的recyclerview。否则您的数据会立即加载并且您的视图已经滞后。 所有这些问题都是因为 recyclerView 在 ScrollView 中无法正常工作。
** 更新答案 **
尝试使用下面的处理程序来延迟将每个项目添加到您的回收站视图:
handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run()
yourList.add(newItem);
adapter.notifyDataSetChanged();
,random);//set your delay time here like 2000 for two seconds delay
【讨论】:
@Masound 我不想设置更多负载,我只想绑定可见的项目。我该怎么做 我有 12 个项目,并且都有不同的项目类型和布局 在您的 recyclerview 适配器中,所有项目一个接一个地分别设置。这意味着不设置在一起。即使您的 recyclerView 在 NestedScrollView 中。 请按照我的回答告诉我你的结果。谢谢。以上是关于当 Recyclerview 在 NestedScrollview 中时,如何避免绑定所有项目?的主要内容,如果未能解决你的问题,请参考以下文章
当 onBindViewHolder 调用播放视频时,如果没有在 recyclerview 中停止视频
当我们在 recyclerview 中使用 searchview 时,我们如何将搜索到的字符着色?
Espresso,当 NestedScrollView 或 RecyclerView 在 CoordinatorLayout 中时滚动不起作用