<ListView
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
注意:这一步中,SwipeRefreshLayout必须包裹一个元素,要不然会报如下错误:空指针异常
![](https://img-blog.csdnimg.cn/20210530202452233.png?x-oss
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxODg1Njcz,size_16,color_FFFFFF,t_70)
好了,到这一步你就可以看到下拉刷新的雏形了
API的使用
======
设置下拉刷新进度条颜色
package com.wust.myswiperefresh;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一步:找到 swipeRefresh 控件
SwipeRefreshLayout srl_my_refresh = findViewById(R.id.srl_my_refresh);
//第二步,设置 下拉刷新时的颜色
srl_my_refresh.setColorSchemeColors(Color.parseColor("#ff0000"),Color.parseColor("#00ff00"));
}
}
设置下拉刷新背景色
package com.wust.myswiperefresh;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一步:找到 swipeRefresh 控件
SwipeRefreshLayout srl_my_refresh = findViewById(R.id.srl_my_refresh);
//第二步,设置 下拉刷新时的颜色
srl_my_refresh.setColorSchemeColors(Color.parseColor("#ff0000"),Color.parseColor("#00ff00"));
srl_my_refresh.setProgressBackgroundColorSchemeColor(Color.parseColor("#0000ff"));
}
}
监听下拉刷新
package com.wust.myswiperefresh;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一步:找到 swipeRefresh 控件
SwipeRefreshLayout srl_my_refresh = findViewById(R.id.srl_my_refresh);
//第二步,设置 下拉刷新时的颜色
srl_my_refresh.setColorSchemeColors(Color.parseColor("#ff0000"),Color.parseColor("#00ff00"));
srl_my_refresh.setProgressBackgroundColorSchemeColor(Color.parseColor("#0000ff"));
//监听 刷新是回调
srl_my_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//判断是否在刷新
Toast.makeText(MainActivity.this,srl_my_refresh.isRefreshing()?“正在刷新”:“刷新完成”
,Toast.LENGTH_SHORT).show();
srl_my_refresh.postDelayed(new Runnable() {
@Override
public void run() {
//关闭刷新
srl_my_refresh.setRefreshing(false);
}
},3000);
}
});
}
}