ScrollView(垂直滚动视图)的使用

一、概述

ScrollView是一个可以滚动的布局容器,可以向用户展示超出设备屏幕的内容。当拥有很多内容,屏幕显示不完时,就需要通过滚动显示完成内容。

注意事项:ListView不需要使用ScrollView ,因为ListView自身带有滚动功能。TextView同样带有滚动功能,不需要作为ScrollView的子元素,以实现滚动功能。

二、常用设置

 2.1滚动条隐藏设置

  •标签属性设置: android:scrollbars=“none”
  •代码设置:        setVerticalScrollBarEnabled(false);

    两种方式任选其一即可

 2.2子元素全屏设置 

  1.ScrollView设置 android:fillViewport=“true”
  2.子元素的大小设置为 match_parent

三、监听事件配置

  1.实现OnTouchListener接口的onTouch()方法,监听所有的touch事件;
  2.在onTouch()方法中判断执行的事件类型;
  3.如果为手指移动事件,获取ScrollView子元素的measuredHeight属性,ScrollView的scrollY和height属性。如果measuredHeight值小于等于scrollY与height的和,则已滑动到底部。
 
ScrollView视图布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:scrollbars="none"
    tools:context="com.ccshxt.firstproject.activity.ScrollViewActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:orientation="vertical">
    <TextView
        android:id="@+id/scroll_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ccshxt_desc"/>
     <ImageView
         android:id="@+id/scroll_iv"
         android:layout_width="300dp"
         android:layout_height="250dp"
         android:src="@drawable/ccshxt_logo"/>

    </LinearLayout>

</ScrollView>

Activity代码

public class ScrollViewActivity extends AppCompatActivity implements OnTouchListener{

    private String TAG = "ScrollViewActivity";
    private ScrollView scrollView;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_scroll_view);
        scrollView = (ScrollView) findViewById(R.id.scroll_view);
        scrollView.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int mHeight=0,scrollY=0,height=0;
        //获取执行的动作类型
        switch (event.getAction()){
            //手指移动事件
            case MotionEvent.ACTION_MOVE:
                mHeight = scrollView.getChildAt(0).getMeasuredHeight();
                scrollY = scrollView.getScrollY();
                height = scrollView.getHeight();
                Log.d(TAG, "mHeight: " + mHeight + " scrollY: " + scrollY + " height:" + height);
                if(mHeight <= (height + scrollY)){
                    Toast.makeText(context,"已经到达底部",Toast.LENGTH_SHORT).show();
                }
                break;
            //手指抬起事件
            case MotionEvent.ACTION_UP:
                break;
            //手指落下事件
            case MotionEvent.ACTION_DOWN:
                break;
        }
        return false;
    }
}
原文地址:https://www.cnblogs.com/ccshxt/p/5225582.html