A suspension effect was realized when I was working on the project, as shown in the picture (it may not be clear enough)





meibei.gif

Next comes the implementation, as shown





sticky.gif

Screenshot directly used by demo

The principle is very simple, RecyclerView addHeaderView to achieve the steps:

1. Add depend on the compile ‘com. Making. CymChad: BaseRecyclerViewAdapterHelper: 2.9.14’ compile ‘com. Android. Support: recyclerview – v7:25.1.0’ import BaseRecyclerViewAdapterHelper, used to add HeaderView recyclerview; 2. Layout, use the tag include to write the view that needs to be suspended;



; Then the layout headerView

3. Inside onScrollStateChanged(RecyclerView RecyclerView, int newState), inside inside hanged View (RecyclerView RecyclerView, int newState) int[] location = new int[2]; mImageView.getLocationOnScreen(location); int y = location[1]; imageY = y; In onScrolled(RecyclerView RecyclerView, INT DX, int DY), constantly obtain the displacement of the suspended label in headerView on the Y-axis of the screen, to judge the display or hide of the suspended view, core code: if (mHeaderView == null) return; int getTop = mHeaderView.getDistanceY(); if (getTop <= imageY) { mImageView.setVisibility(View.VISIBLE); } else { mImageView.setY(0); mImageView.setVisibility(View.GONE); }

Github Demo experience address