欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android使用ScrollView实现滚动效果

时间:2021-06-07 08:52:49|栏目:Android代码|点击:

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

上一篇:Android在类微信程序中实现蓝牙聊天功能的示例代码

栏    目:Android代码

下一篇:Android编程简单实现拨号器功能的方法

本文标题:Android使用ScrollView实现滚动效果

本文地址:http://www.codeinn.net/misctech/136797.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有