您的位置:首页 > 移动开发 > Android开发

Android 垂直与水平滚动条:Scrollview与HorizontalScrollView

2015-09-08 08:43 507 查看

一、ScrollView介绍

  滚动视图ScrollView由FrameLayout派生而出,它的内部最多只能包含一个组件,而它的作用是为该组件添加垂直滚动条,使它可以滑动。那么我们为什么要学习SrcollView呢?我们可以联想到我们平时用的手机阅读器在内容过多时我们可以上下进行滑动来查看全部内容,这就是SrcollView所能做的事情。

  


二、使用SrcollView的监听器

  在上面的图片演示中,我们可以看ScrollView的旁边有垂直的滚动条,我们通过在ScrollView布局中加上
android:scrollbars="none"
语句隐藏灰色滚动条。

  下面我们使用ScrollView监听器来看下何时进行的滚动。

  


布局

[code]<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="horizontal"
    tools:context=".MainActivity" >
    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:scrollbars="none">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/content" />
    </ScrollView>

</LinearLayout>


MainActivity

[code]package com.example.myscrollview;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ScrollView scrollview;
    private TextView mtv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mtv=(TextView) findViewById(R.id.tv);
        scrollview=(ScrollView) findViewById(R.id.scrollview);
        //现在我们在原布局textview内容的后面再添加点内容
        mtv.append(getResources().getString(R.string.content));
        //监听器
        scrollview.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:

                    /*
                     * (1)getscrollY————滚动条滚动距离
                     * (2)getMeasuredHeight()—— 一屏幕高度
                     * (3)getHeight()——textview高度
                     */
                    //顶部状态
                    if(scrollview.getScrollY()<=0){
                    //  Toast.makeText(getApplicationContext(), "滑动到顶部", Toast.LENGTH_SHORT).show();
                Log.d("滑动", "顶部");
                    }
                    //底部状态
                    //textview总高度=一屏幕高度+滚动条滚动距离
                    if(scrollview.getChildAt(0).getMeasuredHeight()<=scrollview.getHeight()+scrollview.getScrollY()){
                    //  Toast.makeText(getApplicationContext(), "滑动到底部", Toast.LENGTH_SHORT).show();
                        Log.d("滑动", "底部");
                    }   
                    break;
                default:
                    break;
                }
                return false;
            }
        });
  }
}


三、如何控制视图位置

  这里我们通过scrollTo()与scrollBy()来获取视图的位置,并进行滚动。

scrollview.scrollTo();

scrollTo():以视图的开始位置进行滚动



[code]@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_up:
            scrollview.scrollTo(0, -30);
            break;
        case R.id.bt_down:
            scrollview.scrollTo(0, 30);
            break;
        default:
            break;
        }

    }


scrollview.scrollBy()

scrollBy():以视图的前一次的位置去滚动相应的距离(也就是我们设置的30)



[code]    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_up:
            scrollview.scrollBy(0, -30);
            break;
        case R.id.bt_down:
            scrollview.scrollBy(0, 30);
            break;
        default:
            break;
        }

    }


四、学习点

1、布局中隐藏滚动条

2、text后面添加内容

3、使用监视器查看滚动位置,获取滑动手势,区别getHeight()与getMeasuredHeight()

4、控制视图位置,区别scrollTo()与scrollBy().

五、HorizontalScrollView

HorizontalScrollView与ScrollView类似不再赘述。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: