日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

SwipeRefreshLayout的基本使用「建议收藏」

發(fā)布時(shí)間:2023/12/15 综合教程 46 生活家
生活随笔 收集整理的這篇文章主要介紹了 SwipeRefreshLayout的基本使用「建议收藏」 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

大家好,又見面了,我是你們的朋友風(fēng)君子。

SwipeRefreshLayout的基本使用

簡介

SwipRefreshLayout是谷歌前一段時(shí)間推出的一款下拉刷新控件。

常用方法

方法 解釋
setColorSchemeResources(int…colorReslds) 設(shè)置下拉進(jìn)度條的顏色主題,參數(shù)可變,并且是資源id,最多設(shè)置四種不同的顏色。
setProgressBackgroundSchemeResource(int coloRes) 設(shè)置下拉進(jìn)度條的背景顏色,默認(rèn)白色。
isRefreshing() 判斷當(dāng)前的狀態(tài)是否是刷新狀態(tài)。
setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) 設(shè)置監(jiān)聽,需要重寫onRefresh()方法,頂部下拉時(shí)會(huì)調(diào)用這個(gè)方法,在里面實(shí)現(xiàn)請求數(shù)據(jù)的邏輯,設(shè)置下拉進(jìn)度條消失等等。
setRefreshing(boolean refreshing) 設(shè)置刷新狀態(tài),true表示正在刷新,false表示取消刷新。

使用

1.首先在應(yīng)用或模塊的 build.gradle 文件中添加所需工件的依賴項(xiàng):

dependencies {
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
   
}

2.在xml文件里面添加相關(guān)代碼

<?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">

    <android.support.v4.widget.SwipeRefreshLayout>
        
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

3.添加布局代碼

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/swipeLayout">
    <ListView android:id="@+id/aa" android:layout_width="match_parent" android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>


4.setColorSchemeResources(int…colorReslds),可以改變下拉刷新時(shí)的顏色

public class MainActivity extends AppCompatActivity { 
   
    private SwipeRefreshLayout swipeRefreshLayout;
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        SwipeRefreshLayout swip_refresh_layout=findViewById(R.id.swipeLayout);
        swip_refresh_layout.setColorSchemeResources(R.color.colorPrimary);
    }

5.setProgressBackgroundSchemeResource(int coloRes),設(shè)置下拉進(jìn)度的背景顏色

public class MainActivity extends AppCompatActivity { 
   
    private SwipeRefreshLayout swipeRefreshLayout;
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        SwipeRefreshLayout swip_refresh_layout=findViewById(R.id.swipeLayout);
        swip_refresh_layout.setColorSchemeResources(R.color.colorPrimary);      swip_refresh_layout.setProgressBackgroundColorSchemeColor(R.color.colorPrimaryDark);
    }

6.setRefreshing(boolean refreshing)設(shè)置刷新狀態(tài),false代表停止執(zhí)行

swip_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
   
    @Override
    public void onRefresh() { 
   
        new Handler().postDelayed(new Runnable() { 
   
            @Override
            public void run() { 
   
                swip_refresh_layout.setRefreshing(false);
            }
        },2000);
    }
});


7.全部整理好后,再加上幾個(gè)item,完整的代碼如下

package com.example.swiperefreshlayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity { 

private SwipeRefreshLayout swipeRefreshLayout;
private String[] names = new String[]
{ 
"Lion","Tiger","Monkey","Dog","Cat","Elephant"};
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
//創(chuàng)建list集合
ListView list = findViewById(R.id.aa);
List<Map<String,Object>> listItems =
new ArrayList<>();
for (int i=0;i<names.length;i++)
{ 

Map<String,Object> listItem =new HashMap<>();
listItem.put("names",names[i]);
listItems.add(listItem);
}
SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.item,
new String[]{ 
"names"}
,new int[]{ 
R.id.names});
list.setAdapter(simpleAdapter);
//SwipeRefreshLayout功能介紹
final SwipeRefreshLayout swip_refresh_layout=findViewById(R.id.swipeLayout);
swip_refresh_layout.setColorSchemeResources(R.color.colorPrimary);
swip_refresh_layout.setProgressBackgroundColorSchemeColor(R.color.colorPrimaryDark);
swip_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 

@Override
public void onRefresh() { 

new Handler().postDelayed(new Runnable() { 

@Override
public void run() { 

swip_refresh_layout.setRefreshing(false);
}
},2000);
}
});
}
}

item.xml

<?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">
<TextView android:id="@+id/names" android:layout_width="match_parent" android:layout_height="70dp" android:paddingLeft="10dp" android:layout_marginTop="5dp" android:textColor="@color/colorPrimaryDark" android:textSize="30dp" />
</LinearLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/swipeLayout">
<ListView android:id="@+id/aa" android:layout_width="match_parent" android:layout_height="74dp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

可能會(huì)遇到的錯(cuò)誤

1.Error inflating class android.support.v4.widget.SwipeRefreshLayout

解決:android.support.v4.widget.SwipeRefreshLayout改為androidx.swiperefreshlayout.widget.SwipeRefreshLayout

參考

參考一
參考二

作者:胡恒娟
原文鏈接:https://blog.csdn.net/hhj98/article/details/106679237

總結(jié)

以上是生活随笔為你收集整理的SwipeRefreshLayout的基本使用「建议收藏」的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。