今天就跟大家聊聊有关怎么在Android中利用ScrollView实现一个滚动效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
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中利用ScrollView实现一个滚动效果有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。