当列表中的项目小于屏幕大小时,无限滚动RecyclerView不起作用

A.s*_*ALI 6 android android-recyclerview

我已经使用Recycler视图成功实现了Endless Scrolling.我正在使用代码,这完全有效但除了以下情况

情况1: 当列表有2个项目比屏幕大小小时,因此它看起来像OnLoadMore再次调用,因为它发现自己到达列表的末尾

我在做什么: 我从1个片段中获取列表,然后将此列表发送到另一个片段,在该片段中有一个带有onScroll列表器的RecyclerView,如下所示:

recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // do something...

                currentVisiblePosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
                pageNumber = pageNumber + 1;
                CallingMyApi();

        }
    });
Run Code Online (Sandbox Code Playgroud)

困惑:

  • 我的RecyclerView在片段初始化时第一次运行滚动侦听器.没有滚动的地方.
  • 如果有较少的项目,请说2它始终在初始化的片段上运行,如果有许多项目OnLoadMore方法不运行.

所以我可以猜测这是问题,因为列表视图中的项目较少如何控制,请帮帮我?

更新1:我的片段代码

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mAdapter = new EnrollmentSearchAdapter(getActivity(), parentList);

    rvSearchEnrollments.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // do something...

            if(parentList!=null && parentList.size()>3) { // just checking my own that 3 items should not be minimum to screen size , where as here it should be more flexible solution 
                currentVisiblePosition = ((LinearLayoutManager) rvSearchEnrollments.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
                pageNumber = pageNumber + 1;
                SearchSundaySchoolParentsCouples searchSundaySchoolParentsCouples = new SearchSundaySchoolParentsCouples(keyword, 20, pageNumber, getActivity());
                searchSundaySchoolParentsCouples.execute();
            }
        }
    });

    rvSearchEnrollments.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));

    rvSearchEnrollments.setAdapter(mAdapter);


}
Run Code Online (Sandbox Code Playgroud)

Sak*_*boy 5

使用EndlessRecyclerViewScrollListener OnScrollListener子类(来自流行的Gist)。当列表中的项目最小且屏幕尺寸最小时,您需要更改一些内容才能使Scrolling RecyclerView无休止地工作。

这是您需要添加到EndlessRecyclerViewScrollListener类中的内容:

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
    super.onScrollStateChanged(recyclerView, newState);
    // Don't do any work until after the ScrollEvent has ended, so do the following check.
    if (newState == RecyclerView.SCROLL_STATE_IDLE)
    {
        // supply a positive number to recyclerView.canScrollVertically(int direction) to check if scrolling down.
        boolean canScrollDownMore = recyclerView.canScrollVertically(1);
        // If recyclerView.canScrollVertically(1) returns false it means you're at the end of the list.
        if (!canScrollDownMore)
        {
            //call the overridden onScrolled() method in our EndlessRecyclerViewScrollListener class.
            // supply any positive number to the third argument to indicate that we've scrolled downward.
            onScrolled(recyclerView, 0, 1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

说明:我们需要实现该onScrollStateChanged()方法,因为onScrolled()在列表末尾时不会调用该方法,或者至少在列表中的项目小于屏幕大小时才调用该方法。

这是整个EndlessRecyclerViewScrollListener班级,其中包含您需要的更新:

public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener
{
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// 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;

private RecyclerView.LayoutManager layoutManager;

public void setLayoutManager(RecyclerView.LayoutManager layoutManager)
{
    this.layoutManager = layoutManager;
}

public RecyclerView.LayoutManager getLayoutManager()
{
    return layoutManager;
}

public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager)
{
    this.layoutManager = layoutManager;
}

public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager, int visibleThreshold)
{
    this.layoutManager = layoutManager;
    this.visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}

public EndlessRecyclerViewScrollListener(
        StaggeredGridLayoutManager layoutManager,
        int visibleThreshold)
{
    this.layoutManager = layoutManager;
    this.visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}

public int getLastVisibleItem(int[] lastVisibleItemPositions)
{
    int maxSize = 0;
    for (int i = 0; i < lastVisibleItemPositions.length; i++)
    {
        if (i == 0)
        {
            maxSize = lastVisibleItemPositions[i];
        }
        else if (lastVisibleItemPositions[i] > maxSize)
        {
            maxSize = lastVisibleItemPositions[i];
        }
    }
    return maxSize;
}

// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy)
{
    if (dy < 1)
    {
        // don't do anything when scrolling up.
        return;
    }
    int lastVisibleItemPosition = 0;
    int totalItemCount = layoutManager.getItemCount();

    if (layoutManager instanceof StaggeredGridLayoutManager)
    {
        int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);
        // get maximum element within the list
        lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
    }
    else if (layoutManager instanceof GridLayoutManager)
    {
        lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
    }
    else if (layoutManager instanceof LinearLayoutManager)
    {
        lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
    }

    // 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 && (lastVisibleItemPosition + visibleThreshold) > totalItemCount)
    {
        currentPage++;
        onLoadMore(currentPage, totalItemCount, view);
        loading = true;
    }
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
    super.onScrollStateChanged(recyclerView, newState);
    if (newState == RecyclerView.SCROLL_STATE_IDLE)
    {
        // supply a positive number to recyclerView.canScrollVertically(int direction) to check if scrolling down.
       boolean canScrollDownMore = recyclerView.canScrollVertically(1);
       // If recyclerView.canScrollVertically(1) returns false it means you're at the end of the list.
       if (!canScrollDownMore)
       {
           onScrolled(recyclerView, 0, 1);
       }
    }
}

// Call this method whenever performing new searches on in onRefresh() if using SwipeRefresh
public void resetState()
{
    this.currentPage = this.startingPageIndex;
    this.previousTotalItemCount = 0;
    this.loading = true;
}

// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);

}
Run Code Online (Sandbox Code Playgroud)

使用的简短示例:此处了解详情

    public class MainActivity extends Activity {
    // Store a member variable for the listener
    private EndlessRecyclerViewScrollListener scrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // Configure the RecyclerView
       RecyclerView rvItems = (RecyclerView) findViewById(R.id.rvContacts);
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
       rvItems.setLayoutManager(linearLayoutManager);
       // Retain an instance so that you can call `resetState()` for fresh searches
       scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
           @Override
           public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
               // Triggered only when new data needs to be appended to the list
               // Add whatever code is needed to append new items to the bottom of the list
               loadNextDataFromApi(page);
           }
      };
      // Adds the scroll listener to RecyclerView
      rvItems.addOnScrollListener(scrollListener);
  }

  // Append the next page of data into the adapter
  // This method probably sends out a network request and appends new data items to your adapter. 
  public void loadNextDataFromApi(int offset) {
      // Send an API request to retrieve appropriate paginated data 
      //  --> Send the request including an offset value (i.e `page`) as a query parameter.
      //  --> Deserialize and construct new model objects from the API response
      //  --> Append the new data objects to the existing set of items inside the array of items
      //  --> Notify the adapter of the new items made with `notifyItemRangeInserted()`
  }
}
Run Code Online (Sandbox Code Playgroud)

您可能需要稍作修改才能使其在您的方案中正常工作,但是提供的代码是一个很好的起点。


Ana*_*Jha 1

如果列表最小然后屏幕大小,您可以在回收器视图中执行加载更多任务

@Override
 public void onBindViewHolder(DataObjectHolder holder, int position) {
            holder.label.setText(mDataset.get(position).getmText1());
            holder.dateTime.setText(mDataset.get(position).getmText2());

           if (position == this.getItemCount() - 1 &&  !loading){
                // do your load more task here
           }
  }
Run Code Online (Sandbox Code Playgroud)