Android开发中SwipeRefreshLayout实现下拉刷新
程序员文章站
2022-03-03 22:10:55
SwipeRefreshLayout是用于实现下拉刷新的核心类,使用很简单只需要使用SwipeRefreshLayout将Recyclerview包裹一下就行。使用:xml:
SwipeRefreshLayout是用于实现下拉刷新的核心类,使用很简单只需要使用SwipeRefreshLayout将Recyclerview包裹一下就行。
使用:
xml:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
activity:
srl = findViewById(R.id.srl);
srl.setColorSchemeResources(R.color.colorPrimary);
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshFruits();
}
});
完整代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toobar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.AppBarLayout>
<!-- <!– scroll:RV 向上滚动的时候ToolBar会跟着一起向上滚动并实现隐藏–>-->
<!-- <!– enterAlways:当RV像下滚动的时候 toolbar会跟着一起向下滑动–>-->
<!-- <!– snap:当还没有完全显示、隐藏的时候,会根据当前滚动的距离,自动显示隐藏–>-->
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity:
package com.example.myswiperefreshlayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.os.Bundle;
import com.example.myswiperefreshlayout.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private com.example.myswiperefreshlayout.Fruit[] fruits = {new com.example.myswiperefreshlayout.Fruit("Apple", R.drawable.apple), new com.example.myswiperefreshlayout.Fruit("Banana", R.drawable.banana),
new com.example.myswiperefreshlayout.Fruit("Orange", R.drawable.orange), new com.example.myswiperefreshlayout.Fruit("Watermelon", R.drawable.watermelon),
new com.example.myswiperefreshlayout.Fruit("Pear", R.drawable.pear), new com.example.myswiperefreshlayout.Fruit("Grape", R.drawable.grape),
new com.example.myswiperefreshlayout.Fruit("Pineapple", R.drawable.pineapple), new com.example.myswiperefreshlayout.Fruit("Strawberry", R.drawable.strawberry),
new com.example.myswiperefreshlayout.Fruit("Cherry", R.drawable.cherry), new com.example.myswiperefreshlayout.Fruit("Mango", R.drawable.mango)};
private List<com.example.myswiperefreshlayout.Fruit> fruitList = new ArrayList<>();
private SwipeRefreshLayout srl;
private FruitAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView rl = findViewById(R.id.rl);
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
rl.setLayoutManager(layoutManager);
adapter = new FruitAdapter(fruitList);
rl.setAdapter(adapter);
srl = findViewById(R.id.srl);
srl.setColorSchemeResources(R.color.colorPrimary);
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshFruits();
}
});
}
private void initFruits() {
fruitList.clear();
for (int i = 0; i < 50; i++) {
Random random = new Random();
int index = random.nextInt(fruits.length);
fruitList.add(fruits[index]);
}
}
private void refreshFruits() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
initFruits();
adapter.notifyDataSetChanged();
srl.setRefreshing(false);//刷新结束隐藏进度条
}
});
}
}).start();
}
}
转发标明出处:https://blog.csdn.net/qq_35698774/article/details/108167612
感谢:郭霖的《第一行代码 第二版》
上一篇: 安装ubuntu之后直接进入windows系统的一种原因
下一篇: Android腾讯应用宝应用认领
推荐阅读
-
Android SwipereFreshLayout下拉刷新
-
iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
-
Android开发重写Animation实现下拉图片后弹射回去效果示例
-
Android编程实现在Activity中操作刷新另外一个Activity数据列表的方法
-
android下拉刷新ListView的介绍和实现代码
-
解析Android开发中多点触摸的实现方法
-
android开发中数据加密实现方法
-
Android开发中如何使用BottomTabBar实现底部导航页
-
MUI进行APP混合开发实现下拉刷新和上拉加载 原创
-
Android自定义控件实现下拉刷新效果