Android使用ScrollView实现滚动效果

作者:「已注销」 时间:2023-05-01 13:25:16 

本文实例为大家分享了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;
     }
   });
 }

}

来源:https://blog.csdn.net/xushunag/article/details/79031371

标签:ScrollView,滚动
0
投稿

猜你喜欢

  • Java中数组在内存中存放原理的讲解

    2022-12-10 03:48:35
  • android采用FFmpeg实现音频混合与拼接剪切

    2023-10-04 06:51:24
  • Android实现复制Assets文件到SD卡

    2022-03-21 03:36:53
  • C#实现的字符串相似度对比类

    2023-08-08 20:35:10
  • C# 动态调用WebService的示例

    2023-07-04 05:40:20
  • Android通过代码控制ListView上下滚动的方法

    2022-06-29 03:07:57
  • android实现图片反转效果

    2022-09-24 20:48:11
  • Java通过XPath获取XML文件中符合特定条件的节点

    2023-01-19 07:42:34
  • SpringBoot返回多种格式的数据的实现示例

    2023-11-24 14:22:52
  • 一篇文章教你如何用多种迭代写法实现二叉树遍历

    2023-12-23 04:03:29
  • 基于C#实现屏幕取色器

    2023-02-16 04:17:53
  • Java中逆序遍历List集合的实现

    2022-04-03 23:48:13
  • Java DecimalFormat 保留小数位及四舍五入的陷阱介绍

    2023-11-09 04:49:33
  • 在Android app中实现九(n)宫格图片连续滑动效果

    2022-10-14 21:09:23
  • c#获取本机的IP地址的代码

    2021-08-21 08:37:07
  • Maven 主模块和子模块pom.xml依赖声明

    2022-09-12 21:20:10
  • android实现图片闪烁动画效果的两种实现方式(实用性高)

    2022-06-29 14:18:32
  • 一文探索Java文件读写更高效方式

    2021-12-20 08:31:22
  • java如何获取本地操作系统进程列表

    2022-12-05 13:35:20
  • RocketMQ消息过滤与查询的实现

    2023-06-26 10:04:25
  • asp之家 软件编程 m.aspxhome.com