android开发笔记之SwipeRefreshLayout

android开发笔记之SwipeRefreshLayoutSwipeRefreshLayout简介SwipeRefrshLayout是Google官方更新的一个控件,可以实现下拉刷新的效果,该控件集成自ViewGroup在support-v4兼容包下.在android源码中,主要是在联系人界面刷新联系人数据:packages/apps/Contacts/src/com/android/contacts/list/DefaultContactBrow…

大家好,又见面了,我是你们的朋友全栈君。

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.net/152915.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【系统架构设计师】第一章:操作系统(1.2.2) 信号量与pv操作

    【系统架构设计师】第一章:操作系统(1.2.2) 信号量与pv操作本篇帖子继续上篇。有兴趣可以点击链接进行查看以前写过的文章。【系统架构设计师】第一章:操作系统(1.2.2)参考教材:《系统架构设计师考试全程指导(第二版)》《系统架构设计师教程》1.2.2信号量与pv操作pv操作指的是两个:p操作和v操作。有时候我们的进程在工作的时候,需要同时配合来干多件事情。比如,我们规定一个进程用来写入数据,另一个进程用来读取数据。很显然,这连个进程是不能互相干扰的…

    2022年7月15日
    22
  • oracle存储过程捕捉异常后回滚_Oracle存储过程异常

    oracle存储过程捕捉异常后回滚_Oracle存储过程异常由三种方式抛出异常1.通过PL/SQL运行时引擎2.使用RAISE语句3.调用RAISE_APPLICATION_ERROR存储过程当数据库或PL/SQL在运行时发生错误时,一个异常被PL/SQL运行时引擎自动抛出。异常也可以通过RAISE语句抛出RAISEexception_name;显式抛出异常是程序员处理声明的异常的习惯用法,但RAISE不限于声明了的异常,它可以抛出任何任何异常。例…

    2022年7月17日
    15
  • 5G学习笔记:NSA和SA

    5G学习笔记:NSA和SA大家好,我是小枣君。第一个5G正式标准马上就要发布了,相信大家一定都在翘首企盼。之前我曾经和大家介绍过,去年12月份的时候,我们其实已经发布了“半个”5G标准。是的没错,那个时候是“非独立组网(NSA)”的5G标准。而我们现在正在等的,是“独立组网(SA)”的5G标准。关于非独立组网和独立组网,NSA和SA,虽然大家都听了很多次,但很少有人能真正搞懂它们到底是怎么…

    2025年7月31日
    3
  • linux查看并杀死进程_ubuntu查看进程命令

    linux查看并杀死进程_ubuntu查看进程命令今天在netbeans中关闭webrick时,发现没有关闭掉,打入localhost:3000依然显示页面,发现无法从nb中再次关闭只有进入ubuntu的进程下关闭查看进程:1,ps-e命令2,feng@feng:~$sudonetstat-antupActiveInternetconnections(serversandestablished)ProtoRecv-QSe…

    2022年9月21日
    2
  • linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法

    linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法linxu下的ftp命令是用来下载文件或者上传文件的,下面由学习啦小编为大家整理了linux的ftp下载文件命令的用法的相关知识,希望对大家有帮助!一、linux中的ftp下载文件命令的用法从远程ftp服务器下载文件的命令格式:get远程ftp服务器上当前目录下要下载的文件名[下载到本地机器上当前目录时的文件名],如:getnmap_file[nmap]意思是把远程ftp服务器下的文件nm…

    2022年8月24日
    10
  • [fastai]fastai Cookbook

    [fastai]fastai CookbookCourse url: http://www.fast.ai/ 1.Install fastai plugin in PyCharm IDE; 2.Import as: from fasta

    2025年9月15日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号