ScrollView的簡單實用並實現下拉加載

我們在一個activity中如果文本過多,一屏幕顯示不下的時候,那麼超出的文字就看不到了。

但是我們在瀏覽器瀏覽數據時候一屏幕容納不下的話我們有下拉條可以下拉,那樣就能看到全部內容了。


當然,在android也可以做到。

我們看看代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--<HorizontalScrollView-->
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/content"
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
    <!--</HorizontalScrollView>-->
</LinearLayout>

以上的代碼中我們可以發現,在TextView外面包含了一層ScrollView,就是這一層ScrollView就能實現我們開篇說的顯示問題,當然,ScrollView是縱向的,橫向的話就是我註釋掉的HorizontalScrollView.

這裏我沒有添加文本內容,我實在Actitvity中動態設置文本內容的。




下面我們再來看看如何實現下拉到底進行類似於ListView滑到底部進行動態加載的呢?

package com.example.lolli.scrollview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private ScrollView scrollView;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件
        textView = (TextView) findViewById(R.id.content);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        // 設置文本(獲取String.xml中的資源)
        textView.setText(getResources().getString(R.string.content));
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_MOVE:{
                        // 當Y軸滑動的像素<=0,即當前在頂部
                        if (scrollView.getScrollY()<=0){
                            Log.i("state", "滑動到了頂部");
                        }
                        /**
                         * scrollView.getScrollY()   獲取Y軸滑動的距離
                         * scrollView.getHeight()   獲取屏幕的高度
                         * scrollView.getChildAt(0).getMeasuredHeight()  獲取scrollView的第一個子元素(即TextView)的內容高度
                         */
                        if (scrollView.getScrollY() + scrollView.getHeight() >= scrollView.getChildAt(0).getMeasuredHeight()){
                            Log.i("state", "滑動到了底部");
                            textView.append(getResources().getString(R.string.content));
                        }
                        break;
                    }
                }
                return false;
            }
        });
    }
}

這裏我們監聽了一個事件  onTouchLitenner,可以監控我們在屏幕上的滑動,其實當我手指滑動的距離+屏幕的高度 = 內容的高度,那麼久意味着滑到底部了。

這裏我們這是動態設置文本內容:

 // 設置文本(獲取String.xml中的資源)
        textView.setText(getResources().getString(R.string.content));

取的是string.xml中定義好的內容

還有一句代碼:

scrollView.getChildAt(0).getMeasuredHeight()
這個只的是獲取scrollView第一個子元素中的文本高度,我們從最開始的佈局中就可以看出來,第一個子元素就是TextView。



這樣,我們就實現了利用ScrollView進行下拉到底就進行加載的功能了。/




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章