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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Ubuntu18.04的网络配置

    Ubuntu18.04的网络配置1、网络配置1首先打开虚拟网络编辑器进行配置,会加载几秒钟2点击更改设置1.3选择添加网络,添加一个Vment0,并设置为桥接模式,桥接到处设置为自动即可,如下图所示注意:这里有可能会出现问题:无法将网络更改为桥接状态,没有未桥接的主机网络适配器,解决办法请查看我的另外一章博客:https://blog.csdn.net/weixin_42108484/arti…

    2022年6月4日
  • 【leetcode】23. Merge k Sorted Lists

    【leetcode】23. Merge k Sorted Lists

    2021年6月10日
  • TLSF算法分析

    TLSF算法分析注:本文的大部分内容摘录自论文《TLSF:aNewDynamicMemoryAllocatorforReal-TimeSystems》,可以通过“科学上网”访问如下链接阅读原文:http://www.gii.upv.es/tlsf/files/ecrts04_tlsf.pdf。什么是TLSFTLSF是TwoLevelSegregatedFitmemoryal

    2022年6月30日
  • outsystems

    outsystemsoutsystems从入门到精通-朱家俊的文章-知乎https://zhuanlan.zhihu.com/p/322582052outsystemsoutsystemsoutsystemsoutsystemsoutsystemsoutsystems

    2022年10月22日
  • Netty权威指南V2.0版_javascript权威指南第七版

    Netty权威指南V2.0版_javascript权威指南第七版作者:李林锋著作出版发行:北京:电子工业出版社,2015.04ISBN号:978-7-121-25801-5页数:554开本:16开主题词:JAVA语言-程序设计-指南中图法分类号:TP312-62(工业技术->自动化技术、计算机技术->计算技术、计算机技术->计算机软件)内容提要:《Netty权威指南(第2版)…

    2022年10月2日
  • springboot讲解(终章怎么解释)

    转载请标明出处:https://blog.csdn.net/forezp/article/details/70341818本文出自方志朋的博客SpringBoot非官方教程|终章:文章汇总springboot非官方教程,可能最接近于官方的一个教程,大多数案例都来自于官方文档,为了更好的理解,加入了个人的改造。码云下载:https://git.oschina…

    2022年4月14日

发表回复

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

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