Android中ScrollView使用详解

来源:互联网 发布:网络支付异地消费短信 编辑:程序博客网 时间:2024/05/21 14:44
滚动视图(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;@Overrideprotected 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)@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (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;}});}@Overridepublic 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;}}}

源码下载

1 0