Android使用ScrollView實(shí)現(xiàn)滾動(dòng)效果
本文實(shí)例為大家分享了ScrollView實(shí)現(xiàn)滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
如果長(zhǎng)文本的內(nèi)容超過(guò)一屏幕 則只能顯示一屏幕的內(nèi)容
設(shè)置ScrollView 通過(guò)滾動(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()--全長(zhǎng) * (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; } }); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio 引用aar 進(jìn)行java開發(fā)的操作步驟
這篇文章主要介紹了Android studio 引用aar 進(jìn)行java開發(fā)的操作步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Android日期時(shí)間格式國(guó)際化的實(shí)現(xiàn)代碼
本篇文章是對(duì)在Android中 日期時(shí)間格式國(guó)際化的實(shí)現(xiàn)代碼進(jìn)行了分析介紹。需要的朋友參考下2013-05-05Android基于Sqlite實(shí)現(xiàn)注冊(cè)和登錄功能
這篇文章主要為大家詳細(xì)介紹了Android基于Sqlite實(shí)現(xiàn)注冊(cè)和登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(一)
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android入門之IntentService的使用教程詳解
IntentService的生命周期中有一個(gè)非常好的方法-onHandleIntent方法,它是一個(gè)abstract方法,開發(fā)者在實(shí)現(xiàn)IntentService時(shí)可以覆蓋它來(lái)處理“長(zhǎng)事務(wù)”。本文就來(lái)聊聊IntentService的使用,需要的可以參考一下2022-12-12Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能,文中還給大家?guī)?lái)四種方法,大家可以根據(jù)自己需要參考下2018-07-07