Android使用ScrollView實(shí)現(xiàn)滾動效果
本文實(shí)例為大家分享了ScrollView實(shí)現(xiàn)滾動效果的具體代碼,供大家參考,具體內(nèi)容如下
如果長文本的內(nèi)容超過一屏幕 則只能顯示一屏幕的內(nèi)容
設(shè)置ScrollView 通過滾動瀏覽下面的內(nèi)容
若將標(biāo)簽更改為<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"><!--不顯示右側(cè)滾動條 -->
<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);
//設(shè)置監(jiān)聽器
scrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
//對motionEvent的參數(shù)作判斷
switch (motionEvent.getAction()){
case MotionEvent.ACTION_UP:
{
break;
}
case MotionEvent.ACTION_DOWN:
{
break;
}
case MotionEvent.ACTION_MOVE:{
/*
* (1)getScrollY()--滾動條滑動的距離,從0開始計算
* (2)getMeasuredHeight()--全長
* (3)getHeight()--一屏幕的高度
* */
//頂部狀態(tài)
if(scrollView.getScrollY()<=0){
Log.i("Main","滑動到頂部");
}
//底部狀態(tài)
if(scrollView.getChildAt(0).getMeasuredHeight()<=scrollView.getHeight()+scrollView.getScrollY()){
Log.i("Main","滑動到底部");
tv.append(getResources().getString(R.string.content));//滑動到底部時再次追加本篇文字
}
break;
}
}
return false;
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio 引用aar 進(jìn)行java開發(fā)的操作步驟
這篇文章主要介紹了Android studio 引用aar 進(jìn)行java開發(fā)的操作步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
Android日期時間格式國際化的實(shí)現(xiàn)代碼
本篇文章是對在Android中 日期時間格式國際化的實(shí)現(xiàn)代碼進(jìn)行了分析介紹。需要的朋友參考下2013-05-05
Android基于Sqlite實(shí)現(xiàn)注冊和登錄功能
這篇文章主要為大家詳細(xì)介紹了Android基于Sqlite實(shí)現(xiàn)注冊和登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法(一)
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android入門之IntentService的使用教程詳解
IntentService的生命周期中有一個非常好的方法-onHandleIntent方法,它是一個abstract方法,開發(fā)者在實(shí)現(xiàn)IntentService時可以覆蓋它來處理“長事務(wù)”。本文就來聊聊IntentService的使用,需要的可以參考一下2022-12-12
Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能,文中還給大家?guī)硭姆N方法,大家可以根據(jù)自己需要參考下2018-07-07

