Android使用ScrollView实现滚动效果
本文实例为大家分享了ScrollView实现滚动效果的具体代码,供大家参考,具体内容如下
如果长文本的内容超过一屏幕 则只能显示一屏幕的内容
设置ScrollView 通过滚动浏览下面的内容
若将标签更改为<HorizontalScrollView></HorizontalScrollView>则为水平滚动效果
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"><!--不显示右侧滚动条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content"
/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
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;
}
});
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android4.4+ 实现半透明状态栏(Translucent Bars)
这篇文章主要为大家详细介绍了Android4.4+ 实现半透明状态栏,对状态栏(Status Bar)和下方导航栏(Navigation Bar)进行半透明处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-09-09
详解Android平台JSON预览(JSON-handle)
这篇文章主要介绍了Android平台JSON预览(JSON-handle),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-09-09
Android利用CountDownTimer实现点击获取验证码倒计时效果
这篇文章主要为大家详细介绍了Android利用CountDownTimer实现点击获取验证码倒计时效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-03-03
Android user版通过adb_enable开启adb 调试 不提示对话框的流程分析
这篇文章主要介绍了Android user版通过adb_enable开启adb 调试 不提示对话框的流程分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-05-05
Android设置Padding和Margin(动态/静态)的方法实例
如何在java代码中设置margin,也就是组件与组件之间的间距,下面这篇文章主要给大家介绍了关于Android设置Padding和Margin(动态/静态)的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-11-11


最新评论