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


相关推荐

  • Flake8_quakemaker

    Flake8_quakemakerFlake8简介转载自:http://www.malike.net.cn/blog/2013/10/23/flake8-tutorial/| COMMENTSFlake8包装了下列工具:PyFlakes:静态检查Python代码逻辑错误的工具。pep8:静态检查PEP8编码风格的工具。NedBatchelder’sMcCabescript:静态

    2022年9月13日
    2
  • ❤️学习linux 两年多沉淀下来的linux 命令大全⭐建议收藏⭐[通俗易懂]

    ❤️学习linux 两年多沉淀下来的linux 命令大全⭐建议收藏⭐[通俗易懂]文章目录linux系统命令总结大全关于作者**作者介绍**0.IP地址相关命令0.1ifconfig命令—显示网络设备信息02.ip命令—显示与操作路由03.dhclient命令—动态获取或释放IP地址04.nmtui—界面修改网卡地址信息05.nmcli命令—设置ip地址1.文件目录命令1.1ls命令–显示目录下的内容1.2cd命令—切换目录命令1.3cp命令—复制文件或目录1.4mkdir—创建目录1.5rmdir

    2022年5月1日
    27
  • 苹果越狱安装ipa_苹果4越狱后怎么下载软件

    苹果越狱安装ipa_苹果4越狱后怎么下载软件条件Iphone已越狱IPA补丁已安装软件准备电脑上要有IFUNBOXIphone上要有,INSTALLOUS,可直接在cydia中下载安装。方法在网上

    2022年9月20日
    2
  • Request常用方法

    Request常用方法原文地址一、HttpServletRequest介绍  HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。二、Request常用方法2.1、获得客户机信息  getRequestURL方法返回客户端发出请求时的完整URL。   getRequestURI…

    2022年6月13日
    34
  • Hadoop基础_hadoop教程

    Hadoop基础_hadoop教程Hadoop基础

    2022年4月21日
    45
  • adminlte ajax,AdminLTE

    adminlte ajax,AdminLTEReminder!AdminLTEusesallofBootstrap3components.It’sagoodstarttoreviewtheBootstrapdocumentationtogetanideaofthevariouscomponentsthatthisdocumentationdoesnotcover.Tip!Ifyoug…

    2022年7月27日
    6

发表回复

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

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