SwipeRefreshLayout的基本使用「建议收藏」
大家好,又見面了,我是你們的朋友風君子。
SwipeRefreshLayout的基本使用
簡介
SwipRefreshLayout是谷歌前一段時間推出的一款下拉刷新控件。
常用方法
| 方法 | 解釋 |
|---|---|
| setColorSchemeResources(int…colorReslds) | 設置下拉進度條的顏色主題,參數可變,并且是資源id,最多設置四種不同的顏色。 |
| setProgressBackgroundSchemeResource(int coloRes) | 設置下拉進度條的背景顏色,默認白色。 |
| isRefreshing() | 判斷當前的狀態是否是刷新狀態。 |
| setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) | 設置監聽,需要重寫onRefresh()方法,頂部下拉時會調用這個方法,在里面實現請求數據的邏輯,設置下拉進度條消失等等。 |
| setRefreshing(boolean refreshing) | 設置刷新狀態,true表示正在刷新,false表示取消刷新。 |
使用
1.首先在應用或模塊的 build.gradle 文件中添加所需工件的依賴項:
dependencies {
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
}
2.在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">
<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),可以改變下拉刷新時的顏色
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),設置下拉進度的背景顏色
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)設置刷新狀態,false代表停止執行
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.全部整理好后,再加上幾個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);
//創建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>
可能會遇到的錯誤
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
總結
以上是生活随笔為你收集整理的SwipeRefreshLayout的基本使用「建议收藏」的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyTorch Tutorial
- 下一篇: csgo开箱网站都有哪些_csgo官方承