Recycleviewscrollhelper--recyclerview auxiliary classes for sliding event detection

Source: Internet
Author: User

Catalog Overview

This is an RecycleView auxiliary class for sliding events that can detect RecycleView the state of sliding to the top or bottom.
Can be used to implement RecycleView load more or refresh (although the refresh can be used directly SwipeRefreshLayout ). Also used for some sliding related requirements, such as FloatingActionButton hiding and displaying.

About RecycleViewThe sliding monitoring

RecycleViewitself has provided a sliding listening interface, OnScrollListener This interface contains the following methods.

//当recycleView的滑动状态改变时回调publicvoidonScrollStateChangedint newState){}//当RecycleView滑动之后被回调publicvoidonScrolled(RecyclerView recyclerView,intint dy){}
RecycleViewThe sliding state
/** * The Recyclerview is not currently scrolling. * Current Recycleview does not slide (when sliding is stopped) */ Public Static Final intScroll_state_idle =0;/** * The Recyclerview is currently being dragged by outside input such as user touch input. * Current Recycleview is dragged and slid */< /c2> Public Static Final intScroll_state_dragging =1;/** * The Recyclerview is currently animating to a final position and not under * outside control. * Current Recycleview in The animation process that scrolls to a location, but is not scrolled by touch. Calling scrolltoposition (int) should trigger this state */ Public Static Final intScroll_state_settling =2;

From the above state we can judge the RecycleView current position or the rolling state according to different states.

Monitoring of the sliding position

What we need to determine is RecycleView whether you have slipped to the bottom or the top.
By the States mentioned above, we can determine that when the current slide to the RecycleView top or bottom, its scrolling state is static, then the state should be SCROLL_STATE_IDLE .
OK, here's what you need to determine is whether the current item is the top or the bottom item?
Regarding this question, actually RecycleView already has the related method to be possible to inquire (strictly should be RecycleView LayoutManager ), has already had many related blog descriptions on the net. Here is also a reference to some blog, here give an address, you can understand, the following will also mention how to detect, If you feel that the link content too much can be skipped.
Reference links

In particular, in order to avoid confusion
1. This uses itemView the view that represents each position in the adapter;
2. Refers to the location of the position data in the adapter
3. Use childView RecycleView A child view that represents a cache reuse

Detection of boundary itemView

Regarding itemView the location determination, you can LinearLayoutManager get to the currently displayed view corresponding to the position in the adapter.

LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;//查找最后一个可见的item的positionint lastItemPosition = linearManager.findLastVisibleItemPosition();//查找第一个可见的item的positionint firstItemPosition =linearManager.findFirstVisibleItemPosition();

The position found here is the position in adapter. So you can determine the current itemView top and bottom by position.

The complete Detection method ( onScrollStateChanged)
//onscrollstatechanged MethodRecyclerview.layoutmanager LayoutManager = Recyclerview.getlayoutmanager ();//Determine if the current LayoutManager is Linearlayoutmanager//Only Linearlayoutmanager to find the first and last visible view position methodif(LayoutManagerinstanceofLinearlayoutmanager) {Linearlayoutmanager Linearmanager = (linearlayoutmanager) LayoutManager;//Get the location of the last visible view    intLastitemposition = Linearmanager.findlastvisibleitemposition ();//Get the location of the first visible view    intFirstitemposition =linearmanager.findfirstvisibleitemposition ();if(NewState = = Recyclerview.scroll_state_idle && lastitemposition +1= = Adapter.getitemcount ()) {//The last Itemview position is the last data in adapter, indicating that the Itemview is the bottom view.    //need to note position starting at 0, Adapter.getitemcount () is the total amount of data}///If the detection is a top itemview, just determine if its position is 0    if(NewState = = Recyclerview.scroll_state_idle && Firstitemposition = =0) {}

The above is a simple top/bottom judging method.

Disadvantages of a simple way of judging

The above has been described how to determine how to RecycleView slide to the top and bottom of the way. But this way of judging is flawed. The problem is RecycleView the visible itemview on the lookup.

Visible issues with Itemview

RecycleViewThe Itemview is large and small, depending on our actual usage scenarios and business. When the content of Itemview is relatively long, it will occupy a significant portion RecycleView of the interface. So we tend to have this situation:
some itemview will show only part or half of the sliding process.
But in this case, the Itemview still belongs to a visible ( visible ).

At any time a itemview is visible whenever any part is displayed RecycleView on the Itemview

Back to our method of finding the boundary Itemview, the method to find the boundary is:

linearLayoutManager.findFirstVisibleItemPosition();

So it should be understood here. Only a little bit of the first Itemview is displayed in the RecycleView Itemview, which is also retained and found, and the position returned is indeed 0 (the first itemview position).

In most cases, there is no big problem with this situation. But in some cases, this situation can cause our logic or business to behave abnormally.

Some demand situations

There is one of the following:

    • Demand
      When sliding to the bottom or halfway, because the item may be a lot, re-sliding back is a very troublesome situation, so a button will be set 回到顶部 . When you swipe to the first item Itemview, the display of the 回到顶部 button is canceled.

    • caused by the problem
      Through the above instructions, it is possible to have the following situation, when sliding to the first item Itemview show a little bit of time 回到顶部 the button disappears.

    • Solution Solutions
      If we want this button to disappear as soon as the first item Itemview display is fully displayed or at least most of the display is gone, then you need to detect the fully displayed child view

    • Sample picture

      For the first time, no matter how you swipe, the button won't disappear unless you slide it completely to the top.
      After the toggle does not fully detect the displayed Childview, as long as the first item (child-0) appears in any part of the interface, the button disappears, which is the disadvantage of not detecting the full childview.

Detecting a fully displayed child view

In response to the above possible "manslaughter" touch events, we can further determine and optimize the detection conditions.
First review the conditions of the test:

    • Detects RecycleView the current sliding state
    • Detects whether the current is sliding to the top or bottom

The above two test conditions will not change, but the second detection condition can be further optimized. The following is an example of how to more accurately detect whether to slide to the first Itemview.
We have already taken the RecycleView first Itemview and have now determined that the first Itemview is already in the display, but maybe the itemview just shows a small part. We want the Itemview to be mostly visible or fully visible.

solve the key
In fact, as long as the itemview is visible, it shows that the itemview is at least partially drawn, and as long as it is drawn, it can get some coordinate properties related to the Itemview, thereby RecycleView A comparison can be made to determine whether the Itemview is fully displayed or partially displayed.

Implementation steps
    • First, we need to get the itemview. Because the Itemview is visible and is the first Itemview, it must be in RecycleView the first child view (that is, the first childview of the cache).
    • Second, we need to get the top coordinate of the first item Itemview, and when the Itemview is fully displayed RecycleView , the top coordinate must also be in RecycleView the top coordinate, otherwise the top coordinate will be less than RecycleView the top coordinate.
Watch out.

There is a need to note that the child view is dependent on the RecycleView drawing, so the top coordinate of the obtained child view is relative to the RecycleView coordinate, RecycleView the top coordinate is relative to itself, so RecycleView the top coordinate should be 0

//获取 RecycleView第一个子viewView childView=recycleView.getChildAt(0);//获取第一个子view的顶部坐标int top=childView.getTop();//获取 RecycleView的顶部坐标//正常来说RecycleView的顶部坐标应该是0,但是严格来考虑,当RecycleView设置了paddingTop时,所有子view的绘制将以paddingTop的位置为起始位置,所以实际的顶部应该是paddingTop的高度的数值.int topEdge=recycleView.getPaddingTop();if(top>=topEdge){    //子view完全显示}

The above is about whether the first Itemview is completely tested in a way that is similar to the last Itemview detection method, except that the top of the detected Itemview is replaced by bottom, because at this point the last item to be detected Itemview the bottom edge is less than RecycleView The bottom coordinate (actually RecycleView high), the last item is fully displayed.

When detecting full-screen data RecycleView

With the above instructions, we have been able to detect the top itemview and bottom Itemview completely displayed on the interface, you can avoid some abnormal phenomenon.
But this does not completely solve all the situations, and in some cases there may be some unexpected problems. The
assumes the following conditions:

    • Demand
      RecycleViewOnly one item, and the height of item is not enough to fill RecycleView the height (that is, there is also a blank space to display other item), in this case, as long as the RecycleView arbitrary sliding, the result will be what?

    • Reason
      In fact, from the above analysis, we can determine that in this case, regardless of how the slide will trigger the slide to the top or the bottom of the event. The reason is:
      at this point the top and bottom itemview are the same view and have been fully RecycleView displayed, and RecycleView there is a vacant space
      In this case, as long as the slide, no matter what type of slide will eventually successfully detect the top Itemview or the bottom of the Itemview

    • Solution Solutions
      If in order to avoid this situation, we need to take a step more in the detection, if the current RecycleView display of itemview dissatisfaction screen, in fact, there is no sliding saying ( 没有加载更多,因为根本数据还没有满屏显示 ), as for the drop-down refresh, or can, but generally will use SwipeRefreshLayout Implement the drop-down refresh. So the main consideration here is about sliding to the bottom to load more questions .

    • Sample picture

      RecycleViewthe item in the figure does not fully populate the interface, and when a toast appears, it slides.
      It can be seen that if there is no detection of full screen display, as long as there is a swipe will trigger the slide to the top or bottom of the event (here is the process of switching priority detection, note toast).
      When you select two events to detect a trigger at the same time, the text of the visible toast prompt is two times the

Implementation steps

The following is a case of sliding to the top, regardless of whether sliding to the bottom.

    • Refer to the full display of the detection of sub-view, for the bottom, we just make sure that RecycleView the last sub-view bottom coordinate is less than RecycleView the bottom coordinate to determine its full display.
    • What we need to make sure then is whether there is enough child view to fill the whole RecycleView . Similarly, if the top coordinate of the first child view is determined to be less than the RecycleView top coordinate, the first child view is partially invisible and the description RecycleView is fully populated.

Note that all of the following coordinates are based on RecycleView themselves, relative to RecycleView the coordinates, because all the child view is RecycleView drawn internally

//Get last ChildviewView Lastchildview = Recyclerview.getchildat (ChildCount-1);//Get the first ChildviewView Firstchildview = Recyclerview.getchildat (0);inttop = Firstchildview.gettop ();intBottom = Lastchildview.getbottom ();//recycleview Displays the bottom coordinate y of the active area of the ItemviewintBottomedge = Recyclerview.getheight ()-Recyclerview.getpaddingbottom ();//recycleview Displays the top coordinate y of the active area of the ItemviewintTopEdge = Recyclerview.getpaddingtop ();//The top of the first view is less than the top boundary value, indicating that the first view is partially or completely removed from the interface //The bottom of the last view is less than the bottom boundary value, indicating that the last view is fully displayed in the interface //If these two conditions are met, all child view is filled with Recycleview,recycleview can "really" slideif(Bottom <= bottomedge && top < TopEdge) {//Fullscreen's Recyceview}
Other

Sets the tolerance value when detecting sliding boundaries. When a fully displayed Childview is detected, it is detected when the Childview boundary is fully displayed in the interface. This leads to a possible scenario where It does not detect success when the difference is a little bit to the boundary. The sensitivity requirement is very high here.
The tolerance value is to solve this situation, when the need to detect the sensitivity does not need to be so high, allowing a certain range of boundaries to detect success, you can set the tolerance value.
The tolerance value makes it easier to judge. The tolerance value must be 0 or positive, and the default is 0.

//设置顶部检测的容差值publicvoidsetTopOffsetFaultTolerance(int offset);//设置底部检测的容差值publicvoidsetBottomOffsetFaultTolerance(int offset);

Example: When you set the tolerance value to half the height of item, the callback event can be triggered when the top or bottom item is more than half the slide out of the interface.

Fully detects full screen and slides to bottom (or top)

The above two detection fullscreen and the detection of sliding to the bottom of the method combination can be.
Finally, a more specific test method is attached:

    • You can set the first to detect the slide to the bottom or the top
    • You can set whether or not to continue to detect a situation if one is detected earlier (in some cases it may be necessary to detect whether sliding to the top and bottom)
    • You can set whether a full screen is detected (no sliding events are triggered when not full screen)
    • You can set the tolerance value to detect sliding to the top/bottom (that is, expand the detection range)
GitHub Address

Https://github.com/CrazyTaro/RecycleViewAdatper

Resources download

If you do not want to download the GitHub project, or do not use as just the class file, you can download the class file directly below:
http://download.csdn.net/detail/u011374875/9556686

Back to Catalog

Recycleviewscrollhelper--recyclerview auxiliary classes for sliding event detection

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.