Android---ScrollView实现页面滚动效果

ScrollView实现页面滚动效果

直接上代码
layout布局中的代码:
<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"
    tools:context="com.example.scrollview.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="∨" />

        <Button
            android:id="@+id/up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="∧" />
    </LinearLayout>
    <!--
         android:scrollbars="none" 设置隐藏滚动条     
    	代码中设置隐藏滚动条:
    	setVerticalScrollBarEnabled(false);隐藏横向
    	setHorizontalScrollBarEnabled(false);隐藏纵向-->


    <!-- ScrollView:垂直滚动    HorizontalScrollView:横向滚动 -->

    <ScrollView
        android:id="@+id/scorll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >

        <!-- 
        1、android:lineSpacingExtra 
	设置行间距,如”8dp”。

	2、android:lineSpacingMultiplier 
	设置行间距的倍数,如”1.5″。
         -->
        <TextView
            android:layout_margin="10dp"
            android:lineSpacingMultiplier="1.5"
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp" />
    </ScrollView>

</LinearLayout>
Java文件中的代码:
package com.example.scrollview;

import android.annotation.SuppressLint;
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;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private TextView tv;
	private ScrollView scroll;

	private Button down,up;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
	down=(Button) findViewById(R.id.down);
	up=(Button) findViewById(R.id.up);
	down.setOnClickListener(this);
	up.setOnClickListener(this);

	tv = (TextView) findViewById(R.id.content);
	tv.setText(getResources().getString(R.string.content));

	scroll = (ScrollView) findViewById(R.id.scorll);
	scroll.setOnTouchListener(new OnTouchListener() {

	@Override
	public boolean onTouch(View v, MotionEvent event) {
	// TODO Auto-generated method stub
		switch (event.getAction()) {
		/*
		* 手指抬起
	 	*/
		case MotionEvent.ACTION_UP:

		break;
		/*
		 * 手指落下
		*/
		case MotionEvent.ACTION_DOWN:

		break;
		/*
	 	* 手指滑动
		*/
		case MotionEvent.ACTION_MOVE:
	/**
	 * 1.getScrollY()---滚动条滑动的距离
	 * 2.getMeasuredHeight()---显示视图的全部高度(手机显示+隐藏掉的)
	 * 3.getHeight()---手机显示视图的高度
	 */

	// 顶部状态
	if (scroll.getScaleY() <= 0) {
	Toast.makeText(MainActivity.this, "现在在顶部", Toast.LENGTH_SHORT).show();
	}

	/*
	* 底部状态: TextView的总高度 <= 一个屏幕的高度+滚动条的滚动距离
	 */
	if (scroll.getChildAt(0).getMeasuredHeight() <= scroll.getHeight() + scroll.getScaleY()) {
		Toast.makeText(MainActivity.this, "现在在底部", Toast.LENGTH_SHORT).show();
		Log.e("Main", "TextView的总高度(scroll.getChildAt(0).getMeasuredHeight()):"
		+ scroll.getChildAt(0).getMeasuredHeight() + "\n" + " 一个屏幕的高度(scroll.getHeight())"
		+ scroll.getHeight() + "\n" + "滚动条的滚动距离(scroll.getScaleY())" + scroll.getScaleY());
						
		tv.append(getResources().getString(R.string.content));//在底部重新追加文本
						
		/**
		 * 可以在这里设置刷新和重新加载页面的操作
		 */
						
		}
		break;
	default:
		break;
	}
		return false;
			     }
		});
	}

//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		// TODO Auto-generated method stub
//		return super.onTouchEvent(event);
//	}

	/**
	 * 按钮控制页面滑动
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		/**
		 * x:横坐标   y:纵坐标   
		 * ScrollView为纵向的,所以x轴可设置为0
		 * scrollTo(x, y):以滚动视图起始位置开始计算的,即(0,0)开始,以起始位置为参照点
		 * scrollBy(x, y):相对前一次的位置,去滚动对应的距离,以上一次滚动的位置为参照点
		 */
		switch (v.getId()) {
		
		case R.id.up:
			scroll.scrollBy(0, -30);
			//scroll.scrollTo(0, -30);
			break;
			
		case R.id.down:
			scroll.scrollBy(0, 30);
			//scroll.scrollTo(0, 30);
			break;
			
		default:
			break;
		}
	}
}




  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值