首先是分割线,添加自定义类SpacesItemDecoration
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space=space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left=space;
outRect.right=space;
outRect.bottom=space;
outRect.top=space;
}
}
//设置item之间的间隔
SpacesItemDecoration decoration=new SpacesItemDecoration(10);
recyclerView.addItemDecoration(decoration);
返回顶部的实现
public abstract class RecyclerViewScrollDetector extends RecyclerView.OnScrollListener {
private int mScrollThreshold;
public abstract void onScrollUp();
public abstract void onScrollDown();
public abstract void onScrollTop();
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//设置移动3后才触发事件
setScrollThreshold(3);
boolean isSignificantDelta = Math.abs(dy) > mScrollThreshold;
if (isSignificantDelta) {
if (dy > 0) {
onScrollUp();
} else {
if(getScrolledDistance(recyclerView)>1000){
onScrollDown();
}else{
onScrollTop();
}
}
}
}
private int getScrolledDistance(RecyclerView recyclerView) {
LinearLayoutManager layoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
View firstVisibleItem = recyclerView.getChildAt(0);
int firstItemPosition = layoutManager.findFirstVisibleItemPosition();
int itemHeight = firstVisibleItem.getHeight();
int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibleItem);
return (firstItemPosition + 1) * itemHeight - firstItemBottom;
}
public void setScrollThreshold(int scrollThreshold) {
mScrollThreshold = scrollThreshold;
}
//imageview 就是返回顶部的图像 调用recyclerView.smoothScrollToPosition(0);
recyclerView.addOnScrollListener(new RecyclerViewScrollDetector() {
@Override
public void onScrollUp() {
if(imageView.getVisibility()==View.VISIBLE){
imageView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onScrollDown() {
if(imageView.getVisibility()!=View.VISIBLE){
imageView.setVisibility(View.VISIBLE);
}
}
//以滑进顶部识别区1000
@Override
public void onScrollTop() {
if(imageView.getVisibility()==View.VISIBLE){
imageView.setVisibility(View.INVISIBLE);
}
}
});