The RecyclerView is loaded by pulling more and scrolling to the bottom. The recyclerview is loaded.

Source: Internet
Author: User

The RecyclerView is loaded by pulling more and scrolling to the bottom. The recyclerview is loaded.

1. Determine whether to reach the bottom based on item
This method is the most common and generally implemented as follows:

Public static boolean isVisBottom (RecyclerView recyclerView) {LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView. getLayoutManager (); // The position int lastVisibleItemPosition = layoutManager of the last visible sub-item on the screen. findLastVisibleItemPosition (); // Number of subitems displayed on the current screen int visibleItemCount = layoutManager. getChildCount (); // Number of all subitems of the current RecyclerView int totalItemCount = layoutManager. getItemCount (); // int state = RecyclerView. getScrollState (); if (visibleItemCount> 0 & lastVisibleItemPosition = totalItemCount-1 & state = recyclerView. SCROLL_STATE_IDLE) {return true;} else {return false ;}}

When this method is used to determine whether to scroll to the bottom, as long as the last item shows a point, more loading will be triggered, at this time, the user does not see loading more words at the FooterView (which is inconsistent with the need to drag and drop the display to load more). In addition, when the RecyclerView item is too small to fill the entire RecyclerView, it will also trigger loading more; therefore, this method does not meet our requirements.

2. Use canScrollVertically (int direction) to determine whether it has reached the bottom.

RecyclerView. the value of canScrollVertically (1) indicates whether to scroll up. The value of false indicates that the RecyclerView has been rolled to the bottom. the value of canScrollVertically (-1) indicates whether to scroll down, and false indicates that it has been rolled to the top.

This method seems simple, but there are also some traps. When there are too few items in the RecyclerView to fill the entire RecyclerView, more loading will be triggered regardless of whether the RecyclerView is pulled up or pulled down. In addition, direction not only requires 1 and-1, you only need to ensure that the positive and negative values can achieve the same effect.

// View # canScrollVertically (int direction) source code public boolean canScrollVertically (int direction) {final int offset = computeverticalscroloffset (); final int range = computeVerticalScrollRange (); if (range = 0) return false; if (direction <0) {return offset> 0;} else {return offset <range-1 ;}}

3. Perform a series of calculations through LinearLayoutManager
This method is not recommended and complicated, but it is helpful for understanding the layout of the View. This method consists of four steps ):

Calculate the height of a subitem

Public static int getItemHeight (RecyclerView recyclerView ){
Int itemHeight = 0;
View child = null;
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView. getLayoutManager ();
Int firstPos = layoutManager. findFirstCompletelyVisibleItemPosition ();
Int lastPos = layoutManager. findLastCompletelyVisibleItemPosition ();
Child = layoutManager. findViewByPosition (lastPos );
If (child! = Null ){
RecyclerView. LayoutParams params = (RecyclerView. LayoutParams) child. getLayoutParams ();
ItemHeight = child. getHeight () + params. topMargin + params. bottomMargin;
}
Return itemHeight;
}

Calculates the total distance of the slide subitem.

Public static int getLinearScrollY (RecyclerView recyclerView ){
Int scrollY = 0;
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView. getLayoutManager ();
Int headerCildHeight = getHeaderHeight (recyclerView );
Int firstPos = layoutManager. findFirstVisibleItemPosition ();
View child = layoutManager. findViewByPosition (firstPos );
Int itemHeight = getItemHeight (recyclerView );
If (child! = Null ){
Int firstItemBottom = layoutManager. getDecoratedBottom (child );
ScrollY = headerCildHeight + itemHeight * firstPos-firstItemBottom;
If (scrollY <0 ){
ScrollY = 0;
}
}
Return scrollY;
}

Calculate the total height of all subitems

Public static int getLinearTotalHeight (RecyclerView recyclerView) {int totalHeight = 0;
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView. getLayoutManager ();
View child = layoutManager. findViewByPosition (layoutManager. findFirstVisibleItemPosition ());
Int headerCildHeight = getHeaderHeight (recyclerView );
If (child! = Null ){
Int itemHeight = getItemHeight (recyclerView );
Int childCount = layoutManager. getItemCount ();
TotalHeight = headerCildHeight + (childCount-1) * itemHeight;
}
Return totalHeight;
}

Height comparison

Public static boolean isLinearBottom (RecyclerView recyclerView ){
Boolean isBottom = true;
Int scrollY = getLinearScrollY (recyclerView );
Int totalHeight = getLinearTotalHeight (recyclerView );
Int height = recyclerView. getHeight ();
// Log. e ("height", "scrollY" + scrollY + "totalHeight" + totalHeight + "recyclerHeight" + height );
If (scrollY + height <totalHeight ){
IsBottom = false;
}
Return isBottom;
}

Judge RecyclerView dragging

This step is relatively simple. You can directly listen for scrolling.

RecyclerView. addOnScrollListener (new OnScrollListener () {@ Override public void onScrollStateChanged (RecyclerView recyclerView, int newState) {super. onScrollStateChanged (recyclerView, newState); if (newState = SCROLL_STATE_DRAGGING) {// drag status, in actual use, you also need to determine whether to load more already displayed.} @ Override public void onScrolled (RecyclerView recyclerView, int dx, int dy) {super. onScrolled (recyclerView, dx, dy );}});
Recommended Methods

This method is based on View # canScrollVertically (int direction). It is optimized for the scenario of pulling up and dragging and the items is not filled with the entire RecyclerView. The Code is as follows:

RecyclerView. addOnScrollListener (new OnScrollListener () {@ Override public void onScrollStateChanged (RecyclerView recyclerView, int newState) {super. onScrollStateChanged (recyclerView, newState); if (newState = SCROLL_STATE_DRAGGING & do not trigger loading more) {if (RecyclerView. computeverticalscroloffset ()> 0) {// There is a rolling distance, indicating that more can be loaded. It solves the problem that items cannot be full of RecyclerView and the moving direction problem boolean isBottom = false; isBottom = Recy ClerView. computeVerticalScrollExtent () + RecyclerView. computeverticalscroloffset () = RecyclerView. computeVerticalScrollRange (); // method 2 // isBottom =! RecyclerView. canScrollVertically (1); if (isBottom) {// The description is scrolled to the bottom to trigger loading more ...}}}} @ Override public void onScrolled (RecyclerView recyclerView, int dx, int dy) {super. onScrolled (recyclerView, dx, dy );}});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.