滚动视图(ScrollView)是指当拥有很多内容,屏幕显示不完时,需要通过滚动来显示完整的视图。包括水平滚动视图(HorizontalScrollView)和垂直滚动视图(ScrollView)

基本操作:

setOnTouchListener的使用:判断ScrollView何时滑动到底部
1、getScorollY()——滚动条滑动的距离
2、getMeasuredHeight()——内容的整体高度,包括隐藏部分
3、getHeight()——显示高度。内容未布满屏幕,2=3;内容大于屏幕,3=屏幕高度,2>3。
4、getChildAt(int i)——获取ScorollView的第i个子控件

scrollTo(相对开始位置)和scrollBy(相对前一个位置):控制ScrollView视图的位置

隐藏滚动条
1、标签属性:android:scrollbars="none"
2、代码设置:
setHorizontalScrollBarEnabled(false);//隐藏横向ScorollView
setVerticalScrollBarEnabled(false);//隐藏纵向ScorollView

注意:scrollview内容只有一个子view。

1.ScrollView使内容可以滚动。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none"
> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="22sp"
android:text="shuiping scroll dfffffffffffffffffffffffffffffffffffffffffffffffffffffff"/>
</HorizontalScrollView>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="向上" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="向下" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollview"
android:scrollbars="vertical"
android:scrollbarTrackVertical="@color/colorAccent"
android:scrollbarThumbVertical="@color/colorPrimary"
android:scrollbarStyle="outsideInset">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="5000dp"
android:layout_height="500dp"
android:text="zcxzcxzcx"/>
<TextView
android:layout_width="5000dp"
android:layout_height="500dp"
android:text="zcxzcxzcx"/>
</LinearLayout> </ScrollView>
</LinearLayout>

2.代码 响应函数如下:

public class ScrollViewActi extends Activity implements View.OnClickListener{
private ScrollView scrollView;
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scrollview);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
scrollView = (ScrollView) findViewById(R.id.scrollview);
button1.setOnClickListener(this);
button2.setOnClickListener(this); //这里是为textView赋值,内容在R.string.text中,测试时最好内容长一些,这里不再贴出。
//textView.setText(getResources().getString(R.string.text));
scrollView.setOnTouchListener(new View.OnTouchListener() { @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
//手指抬起
case MotionEvent.ACTION_UP: break; //手指落下
case MotionEvent.ACTION_DOWN: break; //手指滑动
case MotionEvent.ACTION_MOVE:
/**
* 1、getScorollY()——滚动条滑动的距离
* 2、getMeasuredHeight()——内容的整体高度,包括隐藏部分
* 3、getHeight()——显示高度。内容未布满屏幕,2=3;内容大于屏幕,3=屏幕高度,2>3。
*/
//顶部状态
if(scrollView.getScrollY()<=0){
Log.e(">>>>>>>>>>>>>>", "顶部");
Toast.makeText(getApplicationContext(), "顶部", Toast.LENGTH_SHORT).show();
} //顶部状态
//TextView的总高度<=一屏幕的高度+滚动条的滚动距离(getChildAt(0):第0个子控件)
if(scrollView.getChildAt(0).getMeasuredHeight()<= scrollView.getScrollY() + scrollView.getHeight()){
Log.e(">>>>>>>>>>>>>>", "底部");
Toast.makeText(getApplicationContext(), "底部", Toast.LENGTH_SHORT).show(); //在文本中追加内容
//textView.append("111111111111111111111");
}
break;
}
return false;
}
});
} @Override
public void onClick(View v) {
//scrollTo:以滚动视图起始位置开始计算的
//scrollBy:相对前一次的位置,滚动相应的距离
switch (v.getId()) {
case R.id.button1:
scrollView.scrollTo(0, -30);
// scrollView.scrollBy(0, -30);
break; case R.id.button2:
scrollView.scrollTo(0, -30);
// scrollView.scrollBy(0, 30);
break;
}
}
}

3.效果

android-基础编程-ScrollView的更多相关文章

  1. Android基础控件ScrollView滚动条的使用

    1.简介 ScrollView是一个FrameLayout的容器,不过在他的基础上添加了滚动,允许显示的比实际多的内容!另外,只能够往里面放置一个子元素,可以是单一的组件,又或者一个布局包裹着的复杂的 ...

  2. android: 多线程编程基础

    9.1   服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...

  3. Android网络编程基础

    Android网络编程只TCP通信 TCP 服务器端工作的主要步骤如下.步骤1 调用ServerSocket(int port)创建一个ServerSocket,并绑定到指定端口上.步骤2 调用acc ...

  4. Qt on Android 核心编程

    Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著   ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...

  5. Android网络编程系列 一 TCP/IP协议族

    在学习和使用Android网路编程时,我们接触的仅仅是上层协议和接口如Apache的httpclient或者Android自带的httpURlconnection等等.对于这些接口的底层实现我们也有必 ...

  6. Android网络编程系列 一 Socket抽象层

     在<Android网络编程>系列文章中,前面已经将Java的通信底层大致的描述了,在我们了解了TCP/IP通信族架构及其原理,接下来我们就开始来了解基于tcp/ip协议层的Socket抽 ...

  7. Android基础总结(8)——服务

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  8. Android基础总结(7)——异步消息处理

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  9. 【Xamarin开发 Android 系列 4】 Android 基础知识

    原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android? Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Li ...

  10. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简介

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

随机推荐

  1. 502 Bad Gateway

    状态码解释: 502 Bad Gateway:作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应. 502 原因分析: 将请求提交给网关如php-fpm执行,但是由于某些原因没有 ...

  2. Java锁----Lock实现原理

    转载. https://blog.csdn.net/wl6965307/article/details/51249156 Lock完全用Java写成,在java这个层面是无关JVM实现的. 在java ...

  3. 表单:!!!常用JS: form 表单代码

    手机(文本框): <input type="text" name="" maxlength="11" placeholder=&quo ...

  4. 笔记:python (2015)

    [开发环境]: Python 3.3  http://rj.baidu.com/soft/detail/25283.html  大小:20.2M 版本:3.3.5150 位数:64 更新日期:2014 ...

  5. js跳转新窗口

    语法:window.open(url,[target]); eg: window.open("index.html"); window.open("index.html& ...

  6. Django Forms 表单

    环境 python 3.7 服务端  views.py from django import forms # 引入 froms 模块 from django.forms import widgets ...

  7. leetcode1029

    class Solution(object): def twoCitySchedCost(self, costs: 'List[List[int]]') -> int: costs = sort ...

  8. threading.local()源码分析

    前段时间写了个多线程的程序,了解到Python中有个与众不同的thread.local()方法,可以创建一个全局对象,各个线程可以用这个全局对象保存各自的局部变量,而在使用时不受其他线程的影响.于是抽 ...

  9. 思维导图-mysql log

  10. Halcon示例:print_quality 字符验证

    read_image (Image, 'fonts/arial_a1')get_image_size (Image, Width, Height)dev_close_window ()dev_open ...