前言描述
下午突然有个同校的师弟发消息问我如何实现一键回到顶部功能。我想着能帮就帮吧。趁着没事,给他写个dmeo。写到一半发现自己好久没写文章了,顺带水一下吧。赶着下班前完成吧。
正文开始
首先,就我知识范围而言,目前就分别做过ScrollView跟RecyclerView的一键回到顶部。在这里一起说写下吧。
ScrollView
先写一下ScrollView的。我习惯自定义一个控件GoTopScrollView 继承ScrollView。方便以后全局用(这边看各位具体业务需求,一般也是用写自定义的)。废话不多说,上代码。
public class GoTopScrollView extends ScrollView implements View.OnClickListener {
// 回到顶部按钮
private ImageView mGoTopIv;
// 设置滑动高度显示回到顶部按钮
private int mScreenHeight;
public GoTopScrollView(Context context) {
super(context);
}
public GoTopScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GoTopScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public GoTopScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setScrollListener(ImageView goTopIv) {
this.mGoTopIv = goTopIv;
this.mGoTopIv.setOnClickListener(this);
}
public void setScreenHeight(int screenHeight) {
this.mScreenHeight = screenHeight;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
// 这边主要是判断显示跟隐藏的
if (t >= mScreenHeight && mGoTopIv.getVisibility() == VISIBLE) {
mGoTopIv.setVisibility(View.VISIBLE);
} else if (mGoTopIv.getVisibility() == GONE){
mGoTopIv.setVisibility(View.GONE);
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.go_top_iv) {
// 点击之后滚动流畅会顶端
this.smoothScrollTo(0, 0);
}
}
}
这是调用的fragment,我这里的BaseFragment 是我共有的fragment,不用管。findViewById建议直接用黄油刀。
public class GoTopFragment extends BaseFragment {
private GoTopScrollView mGoTopSv;
private ImageView mGoTopIv;
public static GoTopFragment getInstance() {
GoTopFragment fragment = new GoTopFragment();
return fragment;
}
@Override
public int getLayoutResId() {
return R.layout.fragment_go_top;
}
@Override
public void initView() {
mGoTopSv = contentView.findViewById(R.id.go_top_sv);
mGoTopIv = contentView.findViewById(R.id.go_top_iv);
// 450是我随便写的,具体看你们自己。如果需要一直显示可忽略
mGoTopSv.setScreenHeight(450);
}
@Override
public void initClick() {
mGoTopSv.setScrollListener(mGoTopIv);
}
@Override
public void initData() {
}
}
xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white_ffffff">
<com.byl.jdrefresh.customview.GoTopScrollView
android:id="@+id/go_top_sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:overScrollMode="never">
<TextView
android:id="@+id/content_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/string_to_content"
android:textSize="16sp"
android:lineHeight="20dp"
android:textColor="@color/black_333333"/>
</com.byl.jdrefresh.customview.GoTopScrollView>
<ImageView
android:id="@+id/go_top_iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/go_top"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
上个效果图吧
RecyclerView
RecyclerView就不过多说了,其实原理都差不多,用onScrollChanged就好了,具体回到回到顶部位置的方法如下:
mRecyclerView.scrollToPosition(0);
好了,收工下班。里面给大家附带了玄冥神掌,需要的滩友私,祝各位功法大成。 有什么错误还希望大佬们指教,谢谢。