Android使用ScrollView實(shí)現(xiàn)滾動(dòng)效果
本文實(shí)例為大家分享了ScrollView實(shí)現(xiàn)滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
如果長文本的內(nèi)容超過一屏幕 則只能顯示一屏幕的內(nèi)容
設(shè)置ScrollView 通過滾動(dòng)瀏覽下面的內(nèi)容
若將標(biāo)簽更改為<HorizontalScrollView></HorizontalScrollView>則為水平滾動(dòng)效果
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è)滾動(dòng)條 -->
<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) {
//對(duì)motionEvent的參數(shù)作判斷
switch (motionEvent.getAction()){
case MotionEvent.ACTION_UP:
{
break;
}
case MotionEvent.ACTION_DOWN:
{
break;
}
case MotionEvent.ACTION_MOVE:{
/*
* (1)getScrollY()--滾動(dòng)條滑動(dòng)的距離,從0開始計(jì)算
* (2)getMeasuredHeight()--全長
* (3)getHeight()--一屏幕的高度
* */
//頂部狀態(tài)
if(scrollView.getScrollY()<=0){
Log.i("Main","滑動(dòng)到頂部");
}
//底部狀態(tài)
if(scrollView.getChildAt(0).getMeasuredHeight()<=scrollView.getHeight()+scrollView.getScrollY()){
Log.i("Main","滑動(dòng)到底部");
tv.append(getResources().getString(R.string.content));//滑動(dòng)到底部時(shí)再次追加本篇文字
}
break;
}
}
return false;
}
});
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter數(shù)字切換動(dòng)畫實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter數(shù)字切換動(dòng)畫實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android端權(quán)限隱私的合規(guī)化處理實(shí)戰(zhàn)記錄
大家應(yīng)該都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用市場(chǎng)都要求應(yīng)用上架需要用戶協(xié)議,這篇文章主要給大家介紹了關(guān)于Android端權(quán)限隱私合規(guī)化處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄(Translucent Bars)
這篇文章主要為大家詳細(xì)介紹了Android4.4+ 實(shí)現(xiàn)半透明狀態(tài)欄,對(duì)狀態(tài)欄(Status Bar)和下方導(dǎo)航欄(Navigation Bar)進(jìn)行半透明處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
詳解Android平臺(tái)JSON預(yù)覽(JSON-handle)
這篇文章主要介紹了Android平臺(tái)JSON預(yù)覽(JSON-handle),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Android編程之selector下設(shè)置背景屬性值的方法
這篇文章主要介紹了Android編程之selector下設(shè)置背景屬性值的方法,結(jié)合實(shí)例形式分析了Android的selector背景選擇器相關(guān)使用技巧,需要的朋友可以參考下2016-01-01
Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android實(shí)現(xiàn)打開本地pdf文件的兩種方式
在現(xiàn)代應(yīng)用中,PDF格式因其跨平臺(tái)、穩(wěn)定性好、展示內(nèi)容一致等特點(diǎn),在Android平臺(tái)上,如何高效地打開本地PDF文件,不僅關(guān)系到用戶體驗(yàn),也直接影響到應(yīng)用的功能豐富度,本文將給大家詳細(xì)介紹了Android打開本地pdf文件的兩種方式,需要的朋友可以參考下2025-04-04
Android user版通過adb_enable開啟adb 調(diào)試 不提示對(duì)話框的流程分析
這篇文章主要介紹了Android user版通過adb_enable開啟adb 調(diào)試 不提示對(duì)話框的流程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android設(shè)置Padding和Margin(動(dòng)態(tài)/靜態(tài))的方法實(shí)例
如何在java代碼中設(shè)置margin,也就是組件與組件之間的間距,下面這篇文章主要給大家介紹了關(guān)于Android設(shè)置Padding和Margin(動(dòng)態(tài)/靜態(tài))的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

