Android入門之ScrollView的使用教程
介紹
ScrollView(滾動條),它有兩種“滾動條”:
- 豎直滾動條;
- 水平方向上的滾動條:HorizontalScrollView;
我們經(jīng)??梢钥吹皆谑謾C里正在垂直加載一堆的數(shù)據(jù),然后過一會加載得內(nèi)容過多,到了手機的底部了,垂直方向就會出現(xiàn)一個“滾動條”。
這個滾動條可以一下滑到底部、也可以一下滑到頂部。如下截圖所示。

高度需要注意的點
我們經(jīng)常為了用戶體驗,要求加載時或者點一下相應(yīng)的按鈕一下把滾動條定位到手機的底部。但是這邊會有一個“異步加載”的問題。
因為滾動條在加載,持續(xù)的出現(xiàn)垂直方向的滾動條,這已經(jīng)是一個主事件了。你要一下定位到底部,我們雖然可以調(diào)用ScrollView的FOCUS_DOWN事件。但此時會出現(xiàn)點擊無效即點了后滾動條依舊還在加載。
因此我們對于定位到滾動條的底部或者反之頂位到頂部,一定需要使用異步加載機制。這個異步加載事件它會等一會,等主事件結(jié)束-如:還正在加載數(shù)據(jù)完成后,才會調(diào)用另一個界面渲染主事件。
我們來看一個例子。
例子講解
需求如下:
- 使用ScrollView加載200個數(shù)據(jù),呈垂直出現(xiàn)滾動條;
- 在ScrollView的頂部放一個TO DOWN按鈕;
- 在ScrollView的底部放一個TO TOP按鈕;
UI設(shè)計上這邊要小心一下。我們最外層使用的是LinearLayout,然后內(nèi)嵌一個ScrollView,在ScrollView內(nèi)部會有TO DOWN按鈕+TextView+TO TOP按鈕,此時你一定要在ScrollView內(nèi)部再使用一個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;
}
}
}
}大家自己去動動手,試試看這個體驗吧。
到此這篇關(guān)于Android入門之ScrollView的使用教程的文章就介紹到這了,更多相關(guān)Android ScrollView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android UniversalVideoView實現(xiàn)視頻播放器
這篇文章主要為大家詳細(xì)介紹了Android UniversalVideoView實現(xiàn)視頻播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android ListView滾動到底后自動加載數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Android之ListView滾動到底后自動加載數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼
這篇文章主要介紹了Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04

