Android点击按钮返回顶部实现代码
作者:DW的dory 时间:2023-04-10 19:06:40
点击按钮返回顶部,直接上代码吧
布局文件
<LinearLayout 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="#ffffff"
android:orientation="vertical">
<ScrollView
android:id="@+id/sv_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/eason"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/eason"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/eason"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginRight="10dp"
android:src="@mipmap/top"
app:backgroundTint="#ecefef"
app:elevation="10dp"
app:pressedTranslationZ="12dp"
app:rippleColor="@color/colorPrimary" />
</LinearLayout>
</ScrollView>
</LinearLayout>
按钮点击事件
topBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sc.post(new Runnable() {
@Override
public void run() {
sc.post(new Runnable() {
public void run() {
// 返回顶部
sc.fullScroll(ScrollView.FOCUS_UP);
}
});
}
});
}
});
附带一个跳到底部
bottomBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sc.post(new Runnable() {
@Override
public void run() {
sc.post(new Runnable() {
public void run() {
// 滚动到底部
sc.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
});
}
});
标签:Android,返回顶部
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
1秒钟实现Springboot 替换/写入 word文档里面的文字、图片功能
2022-05-08 18:35:48
![](https://img.aspxhome.com/file/2023/7/82437_0s.png)
Android编程之高效开发App的10个建议
2021-08-28 15:55:46
Java 实战项目之仓库管理系统的实现流程
2021-10-05 09:14:07
![](https://img.aspxhome.com/file/2023/1/125251_0s.png)
springboot自定义过滤器的方法
2021-07-29 05:36:53
Android仿百度外卖自定义下拉刷新效果
2022-05-24 06:32:45
![](https://img.aspxhome.com/file/2023/0/137110_0s.gif)
Android用户输入自动提示控件AutoCompleteTextView使用方法
2022-02-25 14:59:50
![](https://img.aspxhome.com/file/2023/5/137185_0s.jpg)
C#多线程及同步示例简析
2022-02-25 22:12:43
![](https://img.aspxhome.com/file/2023/2/92952_0s.png)
Java后端Cookie实现(时间戳)代码实例
2022-05-17 09:39:52
![](https://img.aspxhome.com/file/2023/8/69888_0s.jpg)
Spring Boot 整合mybatis 与 swagger2
2022-05-01 06:32:17
基于jQuery获取table数据发送到后端
2023-07-22 22:07:43
Java 逻辑控制详解分析
2023-08-13 20:08:11
![](https://img.aspxhome.com/file/2023/5/78945_0s.png)
Android Webview的postUrl与loadUrl加载页面实例
2021-06-26 19:39:04
解决Spring Security中AuthenticationEntryPoint不生效相关问题
2022-11-29 06:53:09
![](https://img.aspxhome.com/file/2023/1/110871_0s.png)
Android 中 退出多个activity的经典方法
2023-02-09 04:38:59
IntelliJ IDEA多屏后窗口不显示问题解决方案
2022-11-17 14:40:45
![](https://img.aspxhome.com/file/2023/3/129963_0s.png)
200行java代码实现2048小游戏
2023-11-29 12:03:57
![](https://img.aspxhome.com/file/2023/3/60693_0s.gif)
Android编程之ActionBar Tabs用法实例分析
2021-11-08 08:49:26
Java泛型T,E,K,V,N,?与Object区别和含义
2022-02-23 07:25:30
![](https://img.aspxhome.com/file/2023/7/64747_0s.jpg)
C#利用QrCode.Net生成二维码(Qr码)的方法
2023-11-30 17:37:38
![](https://img.aspxhome.com/file/2023/3/79093_0s.png)
基于C#的音乐播放器主Form实现代码
2022-07-13 01:21:36