Android中ScrollView使用详解

2023-05-16

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

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

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

scrollTo和scrollBy:控制ScrollView视图的位置

使用实例

1、布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 横向滚动条 -->
    <!-- <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" />
    </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:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <TextView
	        android:id="@+id/textView1"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:textSize="22sp" />
    </ScrollView>

</LinearLayout>
2、在Activity中实现滑动监听、滑动加载、位置跳转等功能

<pre name="code" class="java">package com.cx.scorollview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
	private Button button1;
	private Button button2;
	private TextView textView;
	private ScrollView scrollView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		textView = (TextView) findViewById(R.id.textView1);
		scrollView = (ScrollView) findViewById(R.id.scrollView1);
		
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		
		//这里是为textView赋值,内容在R.string.text中,测试时最好内容长一些,这里不再贴出。
		textView.setText(getResources().getString(R.string.text));
		scrollView.setOnTouchListener(new 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(MainActivity.this, "顶部", Toast.LENGTH_SHORT).show();
					}
					
					//顶部状态
					//TextView的总高度<=一屏幕的高度+滚动条的滚动距离(getChildAt(0):第0个子控件)
					if(scrollView.getChildAt(0).getMeasuredHeight()<= scrollView.getScrollY() + scrollView.getHeight()){
						Log.e(">>>>>>>>>>>>>>", "底部");
						Toast.makeText(MainActivity.this, "底部", 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;
		}
	}
}


  
源码下载

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android中ScrollView使用详解 的相关文章

随机推荐

  • 多线程并发笔记整理

    1 线程本质就是栈结构 2 进程从规模上要大于线程 xff0c 进程是包含线程的 xff0c 主方法就是主线程 3 线程在创立以后 xff0c 各自之间的执行是互不干扰的 4 创建线程之后 xff0c 如果没有start xff0c 线程是
  • 如何实现多线程减少上下文切换

    多线程处理 xff0c 虽然减少了CPU的浪费 xff0c 但是 xff0c 线程间的切换会导致开销增大 xff0c 如何减少线程的切换 1 无锁并发编程 xff1a 多线程竞争锁时 xff0c 会引起上下文切换 xff0c 所以多线程处理
  • 产生死锁和避免死锁

    如何造成死锁 xff1a 在下面的代码中 xff0c 线程t1对A进行加锁 xff0c 线程t2对B进行加锁 xff0c 但是t1想要B xff0c t2想要A xff0c 这样就会导致 xff0c 两个线程都在等待对方释放锁 xff0c
  • 多线程并发编程中的锁

    1 volatile xff0c 修饰的a在1线程执行完后 xff0c 写回内存刷新了 xff0c 此时缓存行中的线程2所用的a就被标记为无效了 xff0c 要再次从内存读取 xff0c 线程1刷新以后的a 但是他只能保证当前查看的时候是正
  • 消息队列解耦合

    队列 xff1a 传统的串行化服务的缺点是 1 耦合性太强 xff0c xff08 如果发生网络波荡 xff0c 就会导致都失败 xff09 2系统吞吐量不大 xff0c 耗时多 传统的串行化服务的优点是 xff1a 系统结构简单 xff0
  • java内存模型(JMM)

    1 并发编程的两大问题 xff1a 多线程之间如何通信 以及多线程之间如何同步 2 线程之间的通信机制包含 xff1a 共享内存和消息传递 3 保证现成的安全是在承接上次线程写完之后再读 4 xff08 面试点 xff09 java线程的通
  • JavaScript的传参问题,出现bug

    记录一次javascript传参遇到的问题 xff1a javascript是弱编辑语言 xff0c 所以传递参数的时候只需要 xff0c 将参数变量写入到方法中即可 xff0c 但是今天我在使用的时候却发现 xff0c 同一个数组数据中
  • 在测试VPN时候的惊天大坑,命令行查到的ip与百度搜索的ip不一致

    为什么百度查到的IP和ipconfig命令的结果不一样 详解公网IP 私网IP 网络分类 xff08 A B C xff09 蒲公英云 推荐 xff1a Java网络编程汇总 Java 原文地址 Link 1 96 IP 96 可以分为 9
  • Android进度条ProgressBar使用详解

    先介绍一下ProgressBar几种比较常用的属性 布局中设置 xff1a android max 61 34 100 34 最大显示进度 android progress 61 34 50 34 第一显示进度 android second
  • 如何让IDEA连接上你的GitHub---git协作开发的出发站

    1 在你的idea中 xff0c 打开设置 xff0c 然后去到VerSion Control 中找到GitHub xff0c 左上角有一个添加按钮 2 由于GitHub收网络影响不太稳定 xff0c 为了更好连接 xff0c 选择使用口令
  • 火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。

    在腾讯云上部署了一个服务器 xff0c 听过服务器ip访问项目的时候 xff0c 刚开始的时候 xff0c 启动tomcat xff0c 是可以启动的 xff0c 有进程的 xff0c 但是访问的时候 xff0c 会出现超时连接 后来想了想
  • 最右的一道面试算法题,--特殊基因

    题目描述 小右发现某种特殊基因片段可以使人类拥有某种超能力 xff0c 比如飞翔 xff0c 隐身 xff0c 时光倒流等等 他想找到拥有这些超能力的人 一个人要想拥有超能力当且仅当他的基因编码里包含至少一个特殊基因片段作为子串 请编写程序
  • 布隆过滤器原理和仿写

    1 作用 布隆过滤器是一个防止黑客恶意攻击的宝器 xff0c 布隆过滤器可以与redis结合使用 xff0c 能够有效地防止redis缓存穿透 先将全数据 xff0c 存放到过滤器中 当黑客访问时 xff0c 会携带访问数据 xff0c 去
  • 多线程测试力扣练习题

    1115 交替打印FooBar 给定一个类 class FooBar public void foo for int i 61 0 i lt n i 43 43 print 34 foo 34 public void bar for int
  • 2021-09-12

  • 2021-09-12

  • 2021-10-14

  • 【无标题】

    学生成绩管理
  • 【无标题】

    零售业管理
  • Android中ScrollView使用详解

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