Android使用ScrollView實現(xiàn)滾動效果
本文實例為大家分享了ScrollView實現(xiàn)滾動效果的具體代碼,供大家參考,具體內(nèi)容如下
如果長文本的內(nèi)容超過一屏幕 則只能顯示一屏幕的內(nèi)容
設置ScrollView 通過滾動瀏覽下面的內(nèi)容
若將標簽更改為<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"><!--不顯示右側滾動條 --> <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); //設置監(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android studio 引用aar 進行java開發(fā)的操作步驟
這篇文章主要介紹了Android studio 引用aar 進行java開發(fā)的操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09Android基于Sqlite實現(xiàn)注冊和登錄功能
這篇文章主要為大家詳細介紹了Android基于Sqlite實現(xiàn)注冊和登錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法(一)
這篇文章主要為大家詳細介紹了Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android入門之IntentService的使用教程詳解
IntentService的生命周期中有一個非常好的方法-onHandleIntent方法,它是一個abstract方法,開發(fā)者在實現(xiàn)IntentService時可以覆蓋它來處理“長事務”。本文就來聊聊IntentService的使用,需要的可以參考一下2022-12-12Android自定義SeekBar實現(xiàn)視頻播放進度條
這篇文章主要為大家詳細介紹了Android自定義SeekBar實現(xiàn)視頻播放進度條的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android實現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能
這篇文章主要介紹了Android實現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能,文中還給大家?guī)硭姆N方法,大家可以根據(jù)自己需要參考下2018-07-07