ScrollView

来源:互联网 发布:寂寞无人知 编辑:程序博客网 时间:2024/05/16 12:51

  今天,一起学习滚动视图ScrollView,这个视图在安卓应用中也是很常见的,它主要分为两种:横向滚动视图HorizontalScrollView;垂直滚动视图ScrollView。

  ScrollView有一条可以隐藏滚动条的属性,需要着重记一下,即scrollbas="none";

  然后,ScrollView也不会例外,也会有监听器,他的监听器是OnTouchListener,此外,一起其他需要注意的,贴出代码后,会放在注解当中。


MainActivity.java

package com.example.demo2;


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 MainActivity extends Activity implements OnClickListener {
private TextView text;
private ScrollView scroll;
private Button up_btn, down_btn;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
scroll = (ScrollView) findViewById(R.id.scroll);
text.setText(getResources().getString(R.string.hello_world));
scroll.setOnTouchListener(new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
/**
* getScrollY()----滚动条滑动的距离 getMeasuredHeight() getHeight()
*/
// 顶部状态
if (scroll.getScrollY() <= 0) {
Log.i("Main", "顶部");
}
// 底部状态
/**
* TextView的总高度=一屏幕的高度+滚动条滑动的距离
*/
if (scroll.getChildAt(0).getMeasuredHeight() <= scroll
.getHeight() + scroll.getScrollY()) {
Log.i("Main", "滑动到底部");
Log.i("Main",
"scroll.getChildAt(0).getMeasuredHeight()="
+ scroll.getChildAt(0)
.getMeasuredHeight()
+ "scroll.getHeight()="
+ scroll.getHeight()
+ "scroll.getScrollY()="
+ scroll.getScrollY());
text.append(getResources().getString(
R.string.hello_world));
}
break;
}
}
return false;
}
});
up_btn = (Button) findViewById(R.id.up);
down_btn = (Button) findViewById(R.id.down);
up_btn.setOnClickListener(this);
down_btn.setOnClickListener(this);
}


@Override
public void onClick(View v) {
/**
* scrollTo:以起始位置开始计算 scrollBy:相对前一次的位置,去滚动对应的距离
*/
switch (v.getId()) {
case R.id.up:
scroll.scrollBy(0, -30);


break;
case R.id.down:
scroll.scrollBy(0, +30);
break;
default:
break;
}
}
}


注解:1.getScrollY()----滚动条滑动的距离 getMeasuredHeight()  getHeight()

    2.scrollTo:以起始位置开始计算 scrollBy:相对前一次的位置,去滚动对应的距离


activity_main.xml

<RelativeLayout 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="${relativePackage}.${activityClass}" >


    <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:layout_toRightOf="@+id/up"
        android:text="DOWN" />


    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/down" >


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


</RelativeLayout>

0 0
原创粉丝点击