Android之ScrollView详解
1.ScrollView使用介绍
首先来看google官方对他的介绍,翻译过来就是可以滚动的用户布局容器,如果手机显示不下子布局,那么可以使用scrollView,当然谷歌也说NestedscrollView
已经提供了更好的用户体验,这个我们以后再详细总结下。
谷歌官方已经提示,不要在scrollView中添加RecyclerView
或者是ListView
布局,这样会引起不好的体验因为会有滑动冲突的问题出现。
另外,ScrollView的直接子View只能有一个。也就是说如果你的视图结构比较复杂,你需要一个标准的容器,如LinearLayout
、RelativeLayout
等。ScrollView只支持竖直滑动,水平滑动使HorizontalScrollView
。
先看布局:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"><ScrollView
android:id="@+id/vertical_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /><include layout="@layout/scollview_item" /></LinearLayout>
</ScrollView></androidx.constraintlayout.widget.ConstraintLayout>
scrollviewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
><ImageView
android:layout_height="64dp"
android:layout_width="64dp"
android:src="@drawable/vector_drawable_right"
/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="ScollViewItem"
android:textSize="21dp"
android:gravity="left|center_vertical"
android:layout_gravity="center"
/></LinearLayout>
效果图:
我是在scrollView中添加了一个LinearLayout,默认是垂直。
scrollView的大小是撑满了父布局。
在这里说一下,ScrollView是一种特殊的布局。当ScrollView的内容大于他本身的size的时候,ScrollView会自动添加滚动条,并可以竖直滑动。如果想要在子View高度小于ScrollView高度时占满,必需在xml里为ScrollView加上Android:fillViewport="true"
,这样子View小于ScrollView高度时就会占满父View.
在说下面之前,再介绍下scollview的其他特点:
scollview在滑动的时候默认是有滑动条的
android:scrollbars="vertical"//垂直滚动条
android:fadeScrollbars="false"//不隐藏
- 如果只添加
android:scrollbars="vertical"
则只有在上下滑动页面的时候才会有垂直滚动条,停止滑动则滚动条消失。
如果不想显示滚动条:
android:scrollbars="none"
不想显示ScrollView拉到尽头(顶部、底部)出现的阴影效果,适用于2.3及以上的 否则不用设置.
android:overScrollMode="never"
2.Android ScrollView监听滑动到顶部和底部:
有需求才会有市场!
需求:典型的就是滚动到底部触发自动加载操作
整体思路:
1.自定义View
2.bottom_position - (getHeight() + getScrollY()) -getPaddingBottom
3.添加滑动到底部条件的回调监听
在这里简单说一下这几个方法:
getHeight():
获得当前view的高度
getScrollX()
:Return the scrolled left position of this view
意思是返回当前滑动View左边界的位置,其实获取的值就是这块幕布在窗口左边界时的x坐标,而幕布上面哪个点是原点(0,0)呢?就是初始化时内容显示的位置。具体的可以看下面的这张图。
getScrollY()
与这个类似,是获取窗口上边界时的y坐标,当然这两个值会随着滑动而变化。
这里如果在实践的时候log一下就会更清楚!
1.CustomScrollView.java
package com.example.myscollview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {OnBottomReachedListener mListener;
public CustomScrollView(Context context) {super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);
}
//重点:
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {View view = getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY())) - view.getPaddingBottom();
Log.e("Bottom", String.valueOf(view.getBottom()));
Log.e("Height", String.valueOf(getHeight()));
Log.e("ScrollY", String.valueOf(getScrollY()));
Log.e("PaddingBottom", String.valueOf(view.getPaddingBottom()));
if (diff <= 0 && mListener != null) {mListener.onBottomReached();
}
super.onScrollChanged(l, t, oldl, oldt);
}
// Getters & Setters
public OnBottomReachedListener getOnBottomReachedListener() {return mListener;
}
public void setOnBottomReachedListener(
OnBottomReachedListener onBottomReachedListener) {mListener = onBottomReachedListener;
}
//Event listener.
public interface OnBottomReachedListener {public void onBottomReached();
}
}
2.MainActivity.java
package com.example.myscollview;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private CustomScrollView scrollView;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = findViewById(R.id.vertical_scroll_view);scrollView.setOnBottomReachedListener(new CustomScrollView.OnBottomReachedListener() {@Override
public void onBottomReached() {Toast.makeText(MainActivity.this, "Has reached the bottom!", Toast.LENGTH_SHORT).show();
}
});
}
}
3.activity_main.xml
<com.example.myscollview.CustomScrollViewandroid:id="@+id/vertical_scroll_view"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="none"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><include layout="@layout/scollview_item" />此处省略。。。。。。。。。。。</LinearLayout></com.example.myscollview.CustomScrollView>
肯定要结果图呀!!
上面说了怎么监听,下面再来说说怎么在某条件下滑动到顶部和底部
3.ScrollView滑动到顶部和底部
需求比如说点击某个btn自动滑动到顶部/底部
Android ScrollView滑动到顶部和底部:
- Scrollview禁止滑动:
scrollView.setOnTouchListener(new View.OnTouchListener() {@Override
public boolean onTouch(View view, MotionEvent motionEvent) {return true;
}});
- 可滑动
scrollView.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {return false;}});
滑动到底部
scrollView.post(new Runnable() {@Overridepublic void run() {//滑动底部scrollView.fullScroll(ScrollView.FOCUS_DOWN);}});
滑动到顶部
scrollView.post(new Runnable() {@Overridepublic void run() {//滑动顶部scrollView.fullScroll(ScrollView.FOCUS_UP);}});
滑动到某个位置
scrollView.post(new Runnable() {@Overridepublic void run() {int offset = 100;//偏移值scrollView.smoothScrollTo(0, offset);}});
scrollview 遇到的坑:
android scrollview 滑不到最下面 ,在scrollview子view里面设置layout_margin之后就滑不到最下面,目前有效的解决办法不要在子view里面设置,在子view的子view里面设置。
参考:
ScrollView监听滑动到底部
Scrollview控制滑动到底部
最后还没完,来加个需求吧:ScrollView阻尼回弹效果!!下回再总结完善
Android之ScrollView详解相关推荐
- Android 动画框架详解,第 1 部分
2019独角兽企业重金招聘Python工程师标准>>> Android 平台提供了一套完整的动画框架,使得开发者可以用它来开发各种动画效果,本文将向读者阐述 Android 的动画框 ...
- Android 动画框架详解
Android 动画框架详解 基本原理 朱 韦伟, 软件工程师, IBM 李 浩, 软件工程师, 爱格码 简介: Android 平台提供了一套完整的动画框架,使得开发者可以用它来开发各种动画效果.A ...
- 《Android游戏开发详解》——第1章,第1.6节函数(在Java中称为“方法”更好)...
本节书摘来自异步社区<Android游戏开发详解>一书中的第1章,第1.6节函数(在Java中称为"方法"更好),作者 [美]Jonathan S. Harbour,更 ...
- JMessage Android 端开发详解
JMessage Android 端开发详解 目前越来越多的应用会需要集成即时通讯功能,这里就为大家详细讲一下如何通过集成 JMessage 来为你的 App 增加即时通讯功能. 首先,一个最基础的 ...
- 《Java和Android开发实战详解》——2.5节良好的Java程序代码编写风格
本节书摘来自异步社区<Java和Android开发实战详解>一书中的第2章,第2.5节良好的Java程序代码编写风格,作者 陈会安,更多章节内容可以访问云栖社区"异步社区&quo ...
- Android事件流程详解
Android事件流程详解 网络上有不少博客讲述了android的事件分发机制和处理流程机制,但是看过千遍,总还是觉得有些迷迷糊糊,因此特地抽出一天事件来亲测下,向像我一样的广大入门程序员详细讲述an ...
- Android Studio 插件开发详解二:工具类
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...
- 《Android游戏开发详解》一2.16 区分类和对象
本节书摘来异步社区<Android游戏开发详解>一书中的第2章,第2.16节,作者: [美]Jonathan S. Harbour 译者: 李强 责编: 陈冀康,更多章节内容可以访问云栖社 ...
- Android Framework系统服务详解
Android Framework系统服务详解 操作环境 系统:Linux (Ubuntu 12.04) 平台:高通 Android版本:5.1 PS: 符号...为省略N条代码 一.大致原理分析 A ...
最新文章
- 交互式多模型_DeepFaceLab824版 Avatar换头模型尝鲜!
- 最新|全球药企15强(附名单)
- 【跃迁之路】【731天】程序员高效学习方法论探索系列(实验阶段488-2019.2.21)...
- jsTree设置默认节点全部展开的方法
- linux crontab不运行,Linux运维知识之解决Linux中crontab不执行ntpdate问题
- [UE4]创建游戏、加入游戏
- 花里胡哨,不协调统一的界面,正如看到一个穿花里胡哨衣服的人,让人作呕,不想接近。...
- 布局管理器android,Android课程---布局管理器之相对布局(一)
- sencha touch调试时Please close other application using ADB: Monitor, DDMS, Eclipse
- MacOS安装app提示已损坏修复指令
- 如何制定个人理财计划_如何根据经济状况做个人投资理财计划?
- SM1、SM2 、SM3、 SM4算法
- 训练集与测试集数据分布不一致
- 苹果电脑上不错的svn客户端
- spring 注解方式动态代理
- 电脑硬件测试软件哪一个准一点,什么软件测试cpu温度准确_测试cpu温度准确软件汇总 - 系统家园...
- Linux内核中网络数据的传输
- 创建脚手架时,查看vue版本
- SpringCloud微服务实战——搭建企业级开发框架(三十一):自定义MybatisPlus代码生成器实现前后端代码自动生成
- Redis学习记录之Transaction简析(十九)