栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android使用ScrollView实现滚动效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android使用ScrollView实现滚动效果

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

如果长文本的内容超过一屏幕 则只能显示一屏幕的内容
设置ScrollView 通过滚动浏览下面的内容

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

xml文件:



 
  
 
    
 
  
 

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:{
     
     //顶部状态
     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;
      }
    });
  }
 
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号