android scrollview 动画滚动,Android使用ScrollView实现滚动效果

本文实例为大家分享了ScrollView实现滚动效果的具体代码,供大家参考,具体内容如下

如果长文本的内容超过一屏幕 则只能显示一屏幕的内容

设置ScrollView 通过滚动浏览下面的内容

若将标签更改为则为水平滚动效果

xml文件:

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.lenovo.scrollview.MainActivity">

android:id="@+id/scroll"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scrollbars="none">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/content"

/>

MainActivity文件:

package com.example.lenovo.scrollview;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Button;

import android.widget.ScrollView;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;

private ScrollView scrollView;

@SuppressLint("ClickableViewAccessibility")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv=findViewById(R.id.content);

tv.setText(getResources().getString(R.string.content));

scrollView=findViewById(R.id.scroll);

//设置监听器

scrollView.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View view, MotionEvent motionEvent) {

//对motionEvent的参数作判断

switch (motionEvent.getAction()){

case MotionEvent.ACTION_UP:

{

break;

}

case MotionEvent.ACTION_DOWN:

{

break;

}

case MotionEvent.ACTION_MOVE:{

/*

* (1)getScrollY()--滚动条滑动的距离,从0开始计算

* (2)getMeasuredHeight()--全长

* (3)getHeight()--一屏幕的高度

* */

//顶部状态

if(scrollView.getScrollY()<=0){

Log.i("Main","滑动到顶部");

}

//底部状态

if(scrollView.getChildAt(0).getMeasuredHeight()<=scrollView.getHeight()+scrollView.getScrollY()){

Log.i("Main","滑动到底部");

tv.append(getResources().getString(R.string.content));//滑动到底部时再次追加本篇文字

}

break;

}

}

return false;

}

});

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值