網站首頁 編程語言 正文
介紹
ScrollView(滾動條),它有兩種“滾動條”:
- 豎直滾動條;
- 水平方向上的滾動條:HorizontalScrollView;
我們經常可以看到在手機里正在垂直加載一堆的數據,然后過一會加載得內容過多,到了手機的底部了,垂直方向就會出現一個“滾動條”。
這個滾動條可以一下滑到底部、也可以一下滑到頂部。如下截圖所示。
高度需要注意的點
我們經常為了用戶體驗,要求加載時或者點一下相應的按鈕一下把滾動條定位到手機的底部。但是這邊會有一個“異步加載”的問題。
因為滾動條在加載,持續的出現垂直方向的滾動條,這已經是一個主事件了。你要一下定位到底部,我們雖然可以調用ScrollView的FOCUS_DOWN事件。但此時會出現點擊無效即點了后滾動條依舊還在加載。
因此我們對于定位到滾動條的底部或者反之頂位到頂部,一定需要使用異步加載機制。這個異步加載事件它會等一會,等主事件結束-如:還正在加載數據完成后,才會調用另一個界面渲染主事件。
我們來看一個例子。
例子講解
需求如下:
- 使用ScrollView加載200個數據,呈垂直出現滾動條;
- 在ScrollView的頂部放一個TO DOWN按鈕;
- 在ScrollView的底部放一個TO TOP按鈕;
UI設計上這邊要小心一下。我們最外層使用的是LinearLayout,然后內嵌一個ScrollView,在ScrollView內部會有TO DOWN按鈕+TextView+TO TOP按鈕,此時你一定要在ScrollView內部再使用一個LinearLayout把這3個組件歸在一起。在此例中我們使用縱向的LinearLayout。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <Button android:id="@+id/buttonToDown" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="To Down" /> <TextView android:id="@+id/textShow" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> <Button android:id="@+id/buttonToTop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="To Top" /> </LinearLayout> </ScrollView> </LinearLayout>
接著我們寫我們后端的交互事件的代碼。
MainActivity.java
package org.mk.android.demo.demoscrollview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btnDown;
private Button btnUp;
private ScrollView scrollView;
private TextView txtShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
}
private void bindViews() {
btnDown = (Button) findViewById(R.id.buttonToDown);
btnUp = (Button) findViewById(R.id.buttonToTop);
scrollView = (ScrollView) findViewById(R.id.scrollView);
txtShow = (TextView) findViewById(R.id.textShow);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 200; i++) {
sb.append("呵呵 * " + i + "\n");
}
txtShow.setText(sb.toString());
btnDown.setOnClickListener(new OnClickListener());
btnUp.setOnClickListener(new OnClickListener());
}
private class OnClickListener implements View.OnClickListener{
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.buttonToDown:
//scrollView.fullScroll(ScrollView.FOCUS_DOWN);
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
break;
case R.id.buttonToTop:
//scrollView.fullScroll(ScrollView.FOCUS_UP);
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_UP);
}
});
break;
}
}
}
}
大家自己去動動手,試試看這個體驗吧。
原文鏈接:https://blog.csdn.net/lifetragedy/article/details/127790550
相關推薦
- 2022-12-21 C++?STL容器與函數謂詞示例分析講解_C 語言
- 2022-04-19 盤點分析C語言中少見卻強大的字符串函數_C 語言
- 2022-09-22 GoFrame garray使用實踐
- 2022-08-16 python+pytest自動化測試函數測試類測試方法的封裝_python
- 2022-08-26 Pandas?DataFrame.drop()刪除數據的方法實例_python
- 2023-01-12 React?useCallback鉤子的作用方法demo_React
- 2022-08-21 caffe的python接口deploy生成caffemodel分類新的圖片_python
- 2022-06-07 Python利用capstone實現反匯編_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支