导入依赖
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:latest.release'
主布局文件
SwipRefreshLayout嵌套ListView
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swip"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
ListView显示布局文件 就一个TextView
<?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:orientation="vertical">
<TextView
android:id="@+id/tv_c"
android:text="香蕉"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
package com.dzqc.card;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swip;
private ListView lv;
String[] fruits = {"香蕉", "苹果", "桔子", "西瓜", "橙子", "葡萄"};
List<Fruits> frutisLists = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swip = findViewById(R.id.swip);
lv = findViewById(R.id.lv);
//三种颜色切换
swip.setColorSchemeResources(R.color.blue, R.color.red, R.color.black);
//让刷新停止
swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//获取数据 停止刷新
refreshFruits();
}
});
/* //自动刷新
swip.measure(0,0);
swip.setRefreshing(true);*/
}
private void refreshFruits() {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
//更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
initFrutis();
//获取数据
lv.setAdapter(new MyAdapter());
//刷新消失
swip.setRefreshing(false);
}
});
}
}.start();
}
private void initFrutis() {
//加载数据
for (int i = 0; i < fruits.length; i++) {
Random random = new Random();
int index = random.nextInt(fruits.length);
Fruits f = new Fruits();
f.setName(fruits[index]);
frutisLists.add(f);
}
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return frutisLists.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v;
if (view == null) {
v = View.inflate(getApplicationContext(), R.layout.item, null);
} else {
v = view;
}
TextView tv_c = v.findViewById(R.id.tv_c);
Fruits fruits = frutisLists.get(i);
tv_c.setText(fruits.getName());
return v;
}
}
}
效果图