当列表中的项目小于屏幕大小

时间:2016-08-22 10:30:58

标签: android android-recyclerview

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

案例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();

        }
    });

混淆:

  • 我的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);


}

3 个答案:

答案 0 :(得分:4)

使用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);
        }
    }
}

说明:我们需要实现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);

}

简要使用示例:more info here

    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()`
  }
}

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

答案 1 :(得分:1)

如果列表小于屏幕尺寸

,您可以在recyclerview中加载更多类似的任务
@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
           }
  }

答案 2 :(得分:-1)

private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
{
    if(dy > 0) //check for scroll down
    {
        visibleItemCount = mLayoutManager.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

        if (loading) 
        {
            if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
            {
                loading = false;
                Log.v("...", "Last Item Wow !");
                //Do pagination.. i.e. fetch new data
            }
        }
    }
}
});

//Don't forgot to implement this
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);