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


相关推荐

  • Python量化交易学习笔记(50)——程序化交易1

    Python量化交易学习笔记(50)——程序化交易1easytrader安装pipinstalleasytrader下载安装e海通财PC独立交易版

    2022年10月8日
    0
  • JavaScript onmousemove 事件

    JavaScript onmousemove 事件DefinitionandUsage定义与用法Theonmousemoveeventoccurswhenthemousepointerismoved.当鼠标移动时触发onmou

    2022年7月2日
    19
  • 网络抓包工具 wireshark 入门教程[通俗易懂]

    网络抓包工具 wireshark 入门教程[通俗易懂]Wireshark(前称Ethereal)是一个网络数据包分析软件。网络数据包分析软件的功能是截取网络数据包,并尽可能显示出最为详细的网络数据包数据。Wireshark使用WinPCAP作为接口,直接

    2022年8月1日
    2
  • VSCode 断点调试项目「建议收藏」

    VSCode 断点调试项目「建议收藏」1.安装必须程序首页下载VSCode,打开一个项目扩展安装DebuggerforChrome插件用于Chrome调试;点击扩展搜索DebuggerforChrome然后点击安装2.VScode项目配置打开项目界面按F5出现界面选择Chrome添加成功后点击调试,添加配置,自动生成launch.json,添加配置的url为iis配置地址3…

    2022年5月21日
    38
  • OHEM(Online Hard Example Mining )算法

    OHEM(Online Hard Example Mining )算法OHEM算法提出于论文TrainingRegion-basedObjectDetectorswithOnlineHardExampleMining,链接:https://arxiv.org/abs/1604.03540。在hardexample(损失较大的样本)反向传播时,可以减少运算量。OHEM主要思想是,根据输入样本的损失进行筛选,筛选出hardexample,表示对…

    2022年5月29日
    35
  • Debian网卡配置_服务器光纤网卡配置

    Debian网卡配置_服务器光纤网卡配置Debian不同于centos系统,网卡配置不是在/etc/sysconfig/network-scrip里面,而是在/etc/network/interfaces里面1.修改vi/etc/network/interfacesautoeth0#开机自动启动ifaceeth0inetstatic#静态IP设置address192.168.0.10#本机IPnetmask255.255.255.0#子网掩码gateway192.168.0.1#网关ifaceeth0i

    2022年10月9日
    0

发表回复

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

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