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

Android SwipeRefreshLayout超詳細講解

 更新時間:2022年11月03日 11:56:26   作者:broadview_java  
在android開發(fā)中,使用最多的數(shù)據(jù)刷新方式就是下拉刷新了,而完成此功能我們使用最多的就是第三方的開源庫PullToRefresh?,F(xiàn)如今,google也忍不住推出了自己的下拉組件SwipeRefreshLayout,下面我們通過api文檔和源碼來分析學習如何使用SwipeRefreshLayout

1. 控件說明

SwipeRefreshLayout是google官方推薦使用的下拉刷新的控件,如果用戶想通過垂直滑動手勢刷新視圖的內(nèi)容,就可以使用它。

實例化此控件的Activity應添加一個 OnRefreshListener,以便在完成滑動刷新手勢時收到通知。 SwipeRefreshLayout 會在每次手勢再次完成時通知監(jiān)聽器,監(jiān)聽器負責確定何時實際啟動其內(nèi)容的刷新。如果監(jiān)聽器不需要有刷新動作,通過調(diào)用 setRefreshing(false) 來取消任何刷新的視覺指示。如果用戶要禁用手勢和進度動畫,可以通過調(diào)用 setEnabled(false)實現(xiàn)。

SwipeRefreshLayout 經(jīng)常與ListView RecyclerView CardView控件一起使用,用來刷新數(shù)據(jù)顯示。

SwipeRefreshLayout 在xml文件中角色:它作為手勢刷新視圖的父布局控件,并且只支持一個直接子控件。

2. API介紹

方法說明
setRefreshing(boolean refreshing)設置刷新狀態(tài),true表示正在刷新,false表示取消刷新。
isRefreshing()判斷當前的狀態(tài)是否是正處于刷新狀態(tài)。
setSize(int size)設置下拉刷新圖標的大小, 只支持兩種: DEFAULT  和 LARGE
setColorSchemeResources(int…colorReslds)設置進度View的組合顏色,在手指上下滑時使用第一個顏色,在刷新中,會一個個顏色進行切換
setColorSchemeResources(@ColorRes int... colorResIds)設置刷新圖標的顏色, 在手指下滑刷新時使用第一個顏色,和 setColorSchemeColors 傳遞的參數(shù)不一樣,這里是傳入int colorResIds
setProgressBackgroundColorSchemeResource(@ColorRes int colorRes)設置刷新圖標的背景顏色,默認為白色
setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)設置監(jiān)聽,需要重寫onRefresh()方法,頂部下拉時會調(diào)用這個方法,在里面實現(xiàn)請求數(shù)據(jù)的邏輯,設置下拉進度條消失等等。
setProgressViewOffset(boolean scale, int start, int end)設置動畫樣式下拉的起始點和結束點,scale設置是否需要放大或者縮小動畫, 第一個參數(shù):默認為false,設置為true,下拉過程中刷新圖標就會從小變大

第二個參數(shù):起始位置,刷新圖標距離頂部像素px

第三個參數(shù):結束位置,刷新圖標距離頂部像素px
setProgressViewEndTarget(boolean scale, int end)設置動畫樣式下拉的結束點 scale設置是否需要放大或者縮小動畫, 第二個參數(shù):結束位置,刷新圖標距離頂部像素px
setSlingshotDistance(@Px int slingshotDistance)設置可以將刷新指示器拉出其靜止位置的距離(以像素為單位)
setEnabled(boolean enabled)false:禁用手勢下拉刷新動畫

這些API 都是通過代碼測試后,做的解釋說明,還是比較準確的。

3. 使用方法

3.1 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipeLayout">
        <TextView
            android:id="@+id/text_test"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="手動下拉刷新"
            android:gravity="center"
            android:textSize="30dp"/>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>

SwipeRefreshLayout 作為父控件, 它僅僅只能有一個子控件,比如TextView ListView CardView 其他等...

3.2 界面代碼

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipeRefreshLayout = findViewById(R.id.swipeLayout);
        mTextView = findViewById(R.id.text_test);
        //設置下拉刷新圖標的大小 只支持兩種: DEFAULT  和 LARGE
        swipeRefreshLayout.setSize(CircularProgressDrawable.LARGE);
        //設置刷新圖標的顏色,在手指下滑刷新時使用第一個顏色,在刷新中,會一個個顏色進行切換 這里是傳入 int... colors
        swipeRefreshLayout.setColorSchemeColors(Color.BLACK, Color.GREEN, Color.RED, Color.YELLOW, Color.BLUE);
        //設置刷新圖標的顏色, 在手指下滑刷新時使用第一個顏色,和 setColorSchemeColors 傳遞的參數(shù)不一樣,這里是傳入int colorResIds
        swipeRefreshLayout.setColorSchemeResources(R.color.red, R.color.blue, R.color.green);
        //設置刷新圖標的背景顏色
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.teal_200);
        //設置動畫樣式下拉的起始點和結束點,scale設置是否需要放大或者縮小動畫
        // 第一個參數(shù):默認為false,設置為true,下拉過程中刷新圖標就會從小變大
        // 第二個參數(shù):起始位置,刷新圖標距離頂部像素px
        // 第三個參數(shù):結束位置,刷新圖標距離頂部像素px
        //swipeRefreshLayout.setProgressViewOffset(false, 100, 200);
        //設置動畫樣式下拉的結束點  scale設置是否需要放大或者縮小動畫
        // 第二個參數(shù):結束位置,刷新圖標距離頂部像素px
        //swipeRefreshLayout.setProgressViewEndTarget(false, 500);
        //設置可以將刷新指示器拉出其靜止位置的距離(以像素為單位)
        //swipeRefreshLayout.setSlingshotDistance(600);
        //false:禁用手勢下拉刷新動畫
        //swipeRefreshLayout.setEnabled(false);
        //設置監(jiān)聽器,需要重寫onRefresh()方法
        swipeRefreshLayout.setOnRefreshListener(this);
    }
    @Override
    public void onRefresh() {
        Log.e("test", "===是否==正在刷新中====" + swipeRefreshLayout.isRefreshing());
        mTextView.setText("正在刷新中......");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mTextView.setText("刷新完成");
                //注意事項:當完成數(shù)據(jù)更新后一定要調(diào)用 setRefreshing(false),不然刷新圖標會一直轉圈,不會消失
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 3000);
    }

代碼解讀:

當手勢下拉刷新界面打印log如下:

28100 28100 E test    : ===是否==正在刷新中====true

效果圖:

響應刷新手勢

(1)當用戶做出滑動手勢時,系統(tǒng)會顯示進度指示器并調(diào)用應用的回調(diào)方法。您的回調(diào)方法負責應用數(shù)據(jù)的實際更新。

(2)如需響應應用中的刷新手勢,請實現(xiàn) SwipeRefreshLayout.OnRefreshListener 接口及其 onRefresh() 方法。

(3)當用戶做出滑動手勢時,系統(tǒng)會調(diào)用 onRefresh() 方法。

實際更新操作的代碼放在單獨的方法中,并通過onRefresh() 實現(xiàn)調(diào)用該更新方法。

當完成數(shù)據(jù)更新后一定要調(diào)用setRefreshing(false), 調(diào)用此方法可指示SwipeRefreshLayout移除進度指示器并更新視圖內(nèi)容。

4. 注意事項

當完成數(shù)據(jù)更新后,記得一定要調(diào)用setRefreshing(false) 方法,不然刷新圖標會一直轉圈,不會消失。

到此這篇關于Android SwipeRefreshLayout超詳細講解的文章就介紹到這了,更多相關Android SwipeRefreshLayout內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論