最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android入門之ScrollView的使用教程

 更新時間:2022年11月10日 16:44:39   作者:TGITCIC  
我們經(jīng)常可以看到在手機(jī)里正在垂直加載一堆的數(shù)據(jù),然后過一會加載得內(nèi)容過多,到了手機(jī)的底部了,垂直方向就會出現(xiàn)一個“滾動條”。本文就來通過一些示例和大家介紹下ScrollView(滾動條)的使用,感興趣的可以了解一下

介紹

ScrollView(滾動條),它有兩種“滾動條”:

  • 豎直滾動條;
  • 水平方向上的滾動條:HorizontalScrollView;

我們經(jīng)常可以看到在手機(jī)里正在垂直加載一堆的數(shù)據(jù),然后過一會加載得內(nèi)容過多,到了手機(jī)的底部了,垂直方向就會出現(xiàn)一個“滾動條”。

這個滾動條可以一下滑到底部、也可以一下滑到頂部。如下截圖所示。

高度需要注意的點(diǎn)

我們經(jīng)常為了用戶體驗,要求加載時或者點(diǎn)一下相應(yīng)的按鈕一下把滾動條定位到手機(jī)的底部。但是這邊會有一個“異步加載”的問題。

因為滾動條在加載,持續(xù)的出現(xiàn)垂直方向的滾動條,這已經(jīng)是一個主事件了。你要一下定位到底部,我們雖然可以調(diào)用ScrollView的FOCUS_DOWN事件。但此時會出現(xiàn)點(diǎn)擊無效即點(diǎn)了后滾動條依舊還在加載。

因此我們對于定位到滾動條的底部或者反之頂位到頂部,一定需要使用異步加載機(jī)制。這個異步加載事件它會等一會,等主事件結(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異步回調(diào)中的UI同步性問題分析

    Android異步回調(diào)中的UI同步性問題分析

    這篇文章主要為大家詳細(xì)分析了Android異步回調(diào)中的UI同步性問題,感興趣的小伙伴們可以參考一下
    2016-06-06
  • android基礎(chǔ)教程之開機(jī)啟動示例

    android基礎(chǔ)教程之開機(jī)啟動示例

    這篇文章主要介紹了android開機(jī)啟動示例,開機(jī)自動啟動程序后開機(jī)啟動廣播功能實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • Android超詳細(xì)講解組件LinearLayout的使用

    Android超詳細(xì)講解組件LinearLayout的使用

    LinearLayout又稱作線性布局,是一種非常常用的布局。正如它的名字所描述的一樣,這個布局會將它所包含的控件在線性方向上依次排列。既然是線性排列,肯定就不僅只有一個方向,這里一般只有兩個方向:水平方向和垂直方向
    2022-03-03
  • Android?Flutter繪制扇形圖詳解

    Android?Flutter繪制扇形圖詳解

    在開發(fā)過程中通常會遇到一些不規(guī)則的UI,比如不規(guī)則的線條,多邊形,統(tǒng)計圖表等等,用那些通用組件通過組合的方式無法進(jìn)行實(shí)現(xiàn),這就需要我們自己進(jìn)行繪制。本文將利用Flutter繪制扇形圖,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • Android--SQLite(增,刪,改,查)操作實(shí)例代碼

    Android--SQLite(增,刪,改,查)操作實(shí)例代碼

    Android--SQLite(增,刪,改,查)操作實(shí)例代碼,需要的朋友可以參考一下
    2013-02-02
  • flutter 路由機(jī)制的實(shí)現(xiàn)

    flutter 路由機(jī)制的實(shí)現(xiàn)

    本文主要介紹 flutter 中的路由實(shí)現(xiàn)原理,包括初始化時的頁面加載、切換頁面的底層機(jī)制等。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Android 仿京東商城底部布局的選擇效果(Selector 選擇器的實(shí)現(xiàn))

    Android 仿京東商城底部布局的選擇效果(Selector 選擇器的實(shí)現(xiàn))

    這篇文章主要介紹了Android 仿京東商城底部布局的選擇效果(Selector 選擇器的實(shí)現(xiàn)),需要的朋友可以參考下
    2017-04-04
  • Android Studio之Debug運(yùn)行期代碼植入的方法

    Android Studio之Debug運(yùn)行期代碼植入的方法

    這篇文章主要介紹了Android Studio之Debug運(yùn)行期代碼植入的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • kotlin中的數(shù)據(jù)轉(zhuǎn)換方法(示例詳解)

    kotlin中的數(shù)據(jù)轉(zhuǎn)換方法(示例詳解)

    這篇文章介紹了Kotlin中將數(shù)字轉(zhuǎn)換為字符串和字符串轉(zhuǎn)換為數(shù)字的多種方法,包括使用`toString()`、字符串模板、格式化字符串、處理可空類型等,同時,也詳細(xì)講解了如何安全地進(jìn)行字符串到數(shù)字的轉(zhuǎn)換,并處理了不同進(jìn)制和本地化格式的字符串轉(zhuǎn)換,感興趣的朋友一起看看吧
    2025-03-03
  • Android View 事件防抖的兩種方案

    Android View 事件防抖的兩種方案

    這篇文章主要介紹了Android View 事件防抖的兩種方案,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04

最新評論

安塞县| 曲阳县| 大洼县| 多伦县| 颍上县| 嘉善县| 周至县| 扬中市| 信丰县| 沁阳市| 绥化市| 连州市| 建湖县| 枣强县| 犍为县| 延安市| 山阳县| 大港区| 兴宁市| 金华市| 兴义市| 容城县| 泊头市| 沂南县| 淅川县| 开封县| 扬中市| 孙吴县| 汾阳市| 阜城县| 定襄县| 南投县| 栾川县| 绩溪县| 图木舒克市| 庆阳市| 阿坝县| 克拉玛依市| 丹江口市| 旺苍县| 漳州市|