1. 程式人生 > >Android-ScrollView滾動條的使用

Android-ScrollView滾動條的使用

ScrollView

1、概述

        滾動檢視是指當擁有很多內容,螢幕顯示不完時,需要通過滾動來顯示完整的檢視。

2、種類

       HorizontalScrollView:水平滾動檢視

       ScrollView:垂直滾動檢視

3、常用屬性

     (1android:scrollbars = “none” 設定隱藏滾動條

     (2setHorizontalScrollBarEnabled(false);  隱藏橫向ScrollView

               setVerticalScrollBarEnabled(false); 隱藏縱向ScrollView

               scrollTo(int ); 滾動到指定的位置

               scrollBy(int); 滾動指定的距離,負數代表向上滾動

4OnTouchListener的使用

      ScrollView.setOnTouchListener(new OnTouchListener(){

           @Override

           public boolean onTouch(View v, MotionEvent event) {

                  switch(event.getAction()){

                  case MotionEvent.ACTION_MOVE:{

                         /**

                         * 1getScrollY() 獲取滑出螢幕的距離

                         * 2getMeasuredHeight() 獲取內容高度

                         * 3getHeight() 獲取螢幕可見高度

                         */

                          break;

                   }

                   }

                 return false;

           }

      });

5、示例

(1)效果:


(2)主Activity

package com.sqb.demo3;

import com.sqb.app_im2.R;

import android.app.Activity;
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;

public class Activity3ScrollView extends Activity{
	private ScrollView scroll;
	private TextView text;
	private Button bt ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout3scrollview);
		text = (TextView) findViewById(R.id.text3_1);
		text.setText(getResources().getString(R.string.content));
		scroll = (ScrollView) findViewById(R.id.scroll3_1);
		scroll.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch(event.getAction()){
				case MotionEvent.ACTION_MOVE:{
					/**
					 * 1、getScrollY() 獲取滑出螢幕的距離
					 * 2、getMeasuredHeight() 獲取內容高度
					 * 3、getHeight() 獲取螢幕可見高度
					 */
					//頂部狀態
					if(scroll.getScrollY() <= 0){
					}
					//底部狀態,TextView的總高度<=螢幕的高度+滾動條的滾到距離
					if(scroll.getChildAt(0).getMeasuredHeight() <= (scroll.getHeight() +scroll.getScrollY())){
						//滑動到底部再次載入文字
						//text.append(getResources().getString(R.string.content));
					}
					break;
				}
				}
				return false;
			}		
		});
		bt = (Button) findViewById(R.id.bt3_1);
		bt.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				text.append(getResources().getString(R.string.content));
			}
		});
	}
}

(3)佈局檔案:

<?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="match_parent"
    android:orientation="vertical" >
    
	<ScrollView 
	    android:id="@+id/scroll3_1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content">
	    <LinearLayout 
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:orientation="vertical">
		    <TextView 
		        android:id="@+id/text3_1"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        />
		    <Button 
		        android:id="@+id/bt3_1"
		        android:layout_gravity="center_horizontal"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="載入更多"
		        />
	    </LinearLayout>
	</ScrollView>
</LinearLayout>