Android点击按钮返回顶部实现代码
更新时间:2017年02月17日 10:23:20 作者:DW的dory
这篇文章主要为大家详细介绍了Android返回顶部实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
点击按钮返回顶部,直接上代码吧
布局文件
<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 getSystemService用法实例总结
这篇文章主要介绍了Android getSystemService用法,结合实例形式总结分析了getSystemService获取系统Service的相关使用方法与注意事项,需要的朋友可以参考下2016-01-01
Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
本文主要介绍Fragment+FragmentTabHost组件实现常见主页面,这里整理了详细资料及简单示例代码,有兴趣的小伙伴可以参考下2016-09-09
AndroidSDK Support自带夜间、日间模式切换详解
这篇文章主要为大家详细介绍了AndroidSDK Support自带夜间、日间模式切换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-09-09


最新评论