android 下拉刷新——swipeRefresh 的使用
dependencies {
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
}
第二步:编写布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
注意:这一步中,SwipeRefreshLayout 必须包裹一个元素,要不然会报如下错误:空指针异常
好了,到这一步你就可以看到下拉刷新的雏形了
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.g
raphics.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 控件
评论