欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android入門之ScrollView的使用教程

 更新時間:2022年11月10日 16:44:39   作者:TGITCIC  
我們經(jīng)??梢钥吹皆谑謾C里正在垂直加載一堆的數(shù)據(jù),然后過一會加載得內(nèi)容過多,到了手機的底部了,垂直方向就會出現(xiàn)一個“滾動條”。本文就來通過一些示例和大家介紹下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)視頻播放器

    Android UniversalVideoView實現(xiàn)視頻播放器

    這篇文章主要為大家詳細(xì)介紹了Android UniversalVideoView實現(xiàn)視頻播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • android讀寫中文如何避免亂碼詳解

    android讀寫中文如何避免亂碼詳解

    這篇文章主要介紹了android讀寫中文如何避免亂碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Android百度地圖poi范圍搜索

    Android百度地圖poi范圍搜索

    這篇文章主要介紹了Android百度地圖poi范圍搜索的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • android 實現(xiàn)側(cè)邊彈窗特效代碼

    android 實現(xiàn)側(cè)邊彈窗特效代碼

    側(cè)邊彈窗是在左邊,需要定位好位置,實現(xiàn)原理其實就是進出動效,用位移加透明度效果來控制,下面通過代碼給大家介紹android 實現(xiàn)側(cè)邊彈窗,需要的朋友參考下吧
    2021-06-06
  • Android ViewBinding的使用詳解

    Android ViewBinding的使用詳解

    這篇文章主要介紹了Android ViewBinding的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android ScrollView使用代碼示例

    Android ScrollView使用代碼示例

    這篇文章主要介紹了Android ScrollView使用代碼示例,本文直接給出示例代碼,需要的朋友可以參考下
    2015-05-05
  • Android獲取通話時間實例分析

    Android獲取通話時間實例分析

    我們知道安卓系統(tǒng)中通話時長應(yīng)該是歸Callog管,所以建議去查查ContactProvider,或者是TelephonyProvider,本文也總結(jié)了一些,需要的朋友可以參考下
    2012-12-12
  • JetpackCompose Scaffold組件使用教程

    JetpackCompose Scaffold組件使用教程

    在今年的Google/IO大會上,亮相了一個全新的 Android 原生 UI 開發(fā)框架-Jetpack Compose, 與蘋果的SwiftIUI一樣,Jetpack Compose是一個聲明式的UI框架
    2023-01-01
  • Android ListView滾動到底后自動加載數(shù)據(jù)

    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下拉刷新實例代碼

    這篇文章主要介紹了Android自定義控件開發(fā)實戰(zhàn)之實現(xiàn)ListView下拉刷新實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-04-04

最新評論