我在NestedScrollview中有一个Recyclerview。 Recyclerview具有需要动态加载的不同项类型,但是当它在嵌套滚动内时,recyclerview中的所有项都绑定在一起。我怎么能避免这个?我不需要任何分页。我只想最小化视图渲染。
答案 0 :(得分:0)
试试这段代码:
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加载。否则,您的数据会立即加载,并且您的视图会滞后。 所有这些问题都是因为recyclelerView在ScrollView中无法正常工作。
** 更新回答 **
尝试使用下面的Handler来延迟将每个项目添加到您的recyclerview:
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