xml:

<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"
tools:context="com.example.android_scollview.MainActivity" >
<Button
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="up"
/>
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="down"
/> <ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scroll1"
android:scrollbars="none">
<!--
ScrollView 的种类:
ScrollView:垂直滚动视图
HorizontalScrollView:水平滚动视图
隐藏ScrollView
(1)标签属性:scrollbars 设置不显示纵向滚动条
(2)代码设置:
setVerticalScrollBarEnabled(false);隐藏纵向ScrollView
setHorizontalSrollBarEnabled(false);隐藏横向ScrollView -->
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text" />
</ScrollView>
</LinearLayout>

main.java

package com.example.android_scollview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
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;
/**
* 应用案例:判断ScrollView何时滑动到底部
* 利用setOnTouchListener
*
*/
public class MainActivity extends Activity implements OnClickListener{ private TextView tv;
private ScrollView scroll;
private Button up;
private Button down; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up = (Button) findViewById(R.id.up);
down = (Button) findViewById(R.id.down); up.setOnClickListener(this);
down.setOnClickListener(this);
tv = (TextView) findViewById(R.id.text);
scroll = (ScrollView) findViewById(R.id.scroll1);
scroll.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:{
/**
* (1)getScrollY()--滚动条滑动的距离
* (2)getMeasuredHeight--显示的高度加上隐藏的高度
* (3)getHeight
*/
//顶部状态
if(scroll.getScaleY()<=0){
Log.i("Main", "顶部状态");
}
//底部状态
//TextView的总高度<=一屏幕的高度+滚动条滚动的距离
if(scroll.getChildAt(0).getMeasuredHeight()<=scroll.getHeight()+scroll.getY()){
Log.i("Main", "底部状态");
}
break;
} } return false;
}
});
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
/**
* scroll的两种滑动方式:
* scrollTo:以滚动视图起始位置开始计算
* scrollBy:相对前一次的位置,去滚动对应的距离
*/
case R.id.up:{
scroll.scrollBy(0, -30);
break;
}
case R.id.down:{
scroll.scrollBy(0, 30);
break;
}
}
}
}

Android_scrollview的更多相关文章

随机推荐

  1. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  2. linux内核中与进程相关的数据结构(基于linux3.16-rc4)

    1.进程描述符 struct task_struct { volatile long state; ....... struct list_head tasks; ....... struct mm_ ...

  3. 【原创】lua的module的一些点

    lua的module好像是5.1开始有的 在xx.lua的开头写上 module('my_module') 这行等价于如下几行 local name = 'my_module' local M = { ...

  4. ubuntukylin提取root权限及mongoDB部署

    UbuntuKylin下安装Mongodb (参照UbuntuKylin下安装Mongodb一文安装成功后的心得) 1.官网下载安装包  http://www.mongodb.org/dr/fastd ...

  5. 转载 在.net中使用GAC

    转载出处 https://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的d ...

  6. SpringMVC(三)

    今天是学习Spring的第四天,今天终于又把Spring+SpringMVC+Mybatis(SSM)高了一遍,这次运行的代码和配置和昨天的不一样,今天运行的很成功. 主要学习的一点就是我今天使用的是 ...

  7. 【WIN32进阶之路】:内存映射文件

    第一章:源起  遇到一个问题,如果一个客户数据文件有2g大,客户要通过界面查询文件中的数据并用列表控件显示数据,要怎么处理这个文件才能让应用程序不会长时间无响应,客户感觉不到程序的卡顿? 第二章:解决 ...

  8. Android开发之有效获取状态栏(StatusBar)高度

    获取状态栏高度 一.传统方式:有时获取为0,解决方法看  二 1 2 3 4 代码 Rect frame = new Rect(); getWindow().getDecorView().getWin ...

  9. How to add “Maven Managed Dependencies” library in build path eclipse

    If you have m2e installed and the project already is a maven project but the maven dependencies are ...

  10. SliderSkin

    <?xml version="1.0" encoding="utf-8"?> <s:Skin xmlns:fx="http://ns ...