亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android入門(mén)之ScrollView的使用教程

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

介紹

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

  • 豎直滾動(dòng)條;
  • 水平方向上的滾動(dòng)條:HorizontalScrollView;

我們經(jīng)??梢钥吹皆谑謾C(jī)里正在垂直加載一堆的數(shù)據(jù),然后過(guò)一會(huì)加載得內(nèi)容過(guò)多,到了手機(jī)的底部了,垂直方向就會(huì)出現(xiàn)一個(gè)“滾動(dòng)條”。

這個(gè)滾動(dòng)條可以一下滑到底部、也可以一下滑到頂部。如下截圖所示。

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

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

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

因此我們對(duì)于定位到滾動(dòng)條的底部或者反之頂位到頂部,一定需要使用異步加載機(jī)制。這個(gè)異步加載事件它會(huì)等一會(huì),等主事件結(jié)束-如:還正在加載數(shù)據(jù)完成后,才會(huì)調(diào)用另一個(gè)界面渲染主事件。

我們來(lái)看一個(gè)例子。

例子講解

需求如下:

  • 使用ScrollView加載200個(gè)數(shù)據(jù),呈垂直出現(xiàn)滾動(dòng)條;
  • 在ScrollView的頂部放一個(gè)TO DOWN按鈕;
  • 在ScrollView的底部放一個(gè)TO TOP按鈕;

UI設(shè)計(jì)上這邊要小心一下。我們最外層使用的是LinearLayout,然后內(nèi)嵌一個(gè)ScrollView,在ScrollView內(nèi)部會(huì)有TO DOWN按鈕+TextView+TO TOP按鈕,此時(shí)你一定要在ScrollView內(nèi)部再使用一個(gè)LinearLayout把這3個(gè)組件歸在一起。在此例中我們使用縱向的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>

接著我們寫(xiě)我們后端的交互事件的代碼。

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;
            }
        }
    }
}

大家自己去動(dòng)動(dòng)手,試試看這個(gè)體驗(yàn)吧。

到此這篇關(guān)于Android入門(mén)之ScrollView的使用教程的文章就介紹到這了,更多相關(guān)Android ScrollView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論