android开发笔记之SwipeRefreshLayout
2023-06-13 09:12:25 时间
大家好,又见面了,我是你们的朋友全栈君。
SwipeRefreshLayout简介
SwipeRefrshLayout是Google官方更新的一个控件,可以实现下拉刷新的效果,该控件集成自ViewGroup在support-v4兼容包下.
在android源码中,主要是在联系人界面刷新联系人数据:
packages/apps/Contacts/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
和文件夹应用的文件显示界面:
packages/apps/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java
Demo
主要实现下拉SwipeRefrshLayout控件,刷新listview控件的数据.
(1) 布局文件—activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="20dp"
android:text="下拉刷新控件样例"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
(2)实现逻辑
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private final static String TAG = "MainActivity";
private SwipeRefreshLayout swipeRefreshLayout;
private TextView textView;
private ListView listview;
private ArrayAdapter<String> adapter;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light, android.R.color.holo_orange_light,
android.R.color.holo_red_light);
textView = (TextView) findViewById(R.id.textView);
listview = (ListView) findViewById(R.id.listView);
data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
data.add("初始的item为:" + i);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);
listview.setAdapter(adapter);
}
public void showLoading() {
textView.setText("正在刷新,请等待");
}
public void hideLoading() {
textView.setText("刷新完毕!");
swipeRefreshLayout.setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(false); // close refresh animator
}
@Override
public void onRefresh() {
Log.i(TAG,"onRefresh()");
showLoading();
final Random random = new Random();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
hideLoading();
int min=1;
int max=20;
Random random = new Random();
int num = random.nextInt(max)%(max-min+1) + min;
data.clear();
for(int i=0;i < num;i++){
data.add("刷新后新增的item:"+i);
}
adapter.notifyDataSetChanged();
}
}, 5000);
}
}
参考资料
1.Android零基础入门|SwipeRefreshLayout下拉刷新 http://www.sohu.com/a/195607552_619467 2.SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新 https://www.cnblogs.com/liunanjava/p/5860024.html
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/152915.html原文链接:https://javaforall.cn
相关文章
- android中的加密算法,Android中加密算法[通俗易懂]
- 代码加密 android,Android 开发怎样做代码加密或混淆「建议收藏」
- android 的hook技术,Android Native Hook技术(一)
- android开发笔记之 Android代码混淆打包
- android deeplink流程,Android Deeplink探究[通俗易懂]
- android sdk manager安装,Android SDK Manager安装过程
- iphone4装android,iPhone4可安装Android实现双系统启动.pdf
- android telephony 原理解析与开发指南_Android逆向pdf
- android toast显示时间,Android Toast自定义显示时间「建议收藏」
- android toast 自定义时间,Android 自定义 Toast 显示时间「建议收藏」
- android触摸屏事件,Android Touch事件分析
- Android开发笔记(一百八十八)工作管理器WorkManager
- eclipse中android开发_Android开发教程
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api
- 【错误记录】Android Studio 编译报错 ( Could not find com.android.tools.build:gradle:4.2.1. )
- 【Android 逆向】Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
- 【Android UI】贝塞尔曲线 ④ ( 使用 android.graphics.Path 提供的 cubicTo 方法绘制三阶贝塞尔曲线示例 )
- Android Studio 导入项目 出现安装Error:Cause: failed to find target with hash string ‘android-23’ 等错误详解手机开发
- Android中的ProgressBar的android:indeterminate详解手机开发
- android自定义Android菜单背景的代码
- Android笔记之:App列表之下拉刷新的使用
- Android笔记之:CM9源码下载与编译的应用
- Android开发笔记之:对实践TDD的一些建议说明
- Android开发笔记之:深入理解多线程AsyncTask
- Android开发笔记之:Splash的实现详解
- Android开发笔记之:深入理解Cursor相关的性能问题
- Android开发笔记之:在ImageView上绘制圆环的实现方法
- Android学习笔记(二)App工程文件分析