implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-5' //下拉刷新依赖
MainActivity的xml布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".OneFrgment.BlankFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recy3"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="@color/maincolor"
android:src="@drawable/ic_arrow_upward"
app:backgroundTint="@color/maincolor"
/>
</RelativeLayout>
</FrameLayout>
MainActivity的置顶效果主要代码 ,
private void setRecyclerScroll(RecyclerView mRecyclerView) {
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
//RecyclerView 滑动 事件 -1 表示 向上, 1 表示向下。
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (!recyclerView.canScrollVertically(-1)) {
} else if (!recyclerView.canScrollVertically(1)) {
} else if (dy < 0) {
onScrolledUp();
} else if (dy > 0) {
onScrolledDown();
}
super.onScrolled(recyclerView, dx, dy);
}
public void onScrolledUp() {
View linearLayout = getActivity().findViewById(R.id.tab_layout);
linearLayout.setVisibility(View.VISIBLE);
fab.setVisibility(View.GONE);
}
public void onScrolledDown() {
View view = getActivity().findViewById(R.id.tab_layout);
view.setVisibility(View.GONE);
fab.setVisibility(View.VISIBLE);
}
});
}
记得调用如上方法
setRecyclerScroll(recy3);
点击时置顶
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fab:
recy3.smoothScrollToPosition(0);
break;
}
}