前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用android自带的SwipeRefreshLayout实现下拉刷新

使用android自带的SwipeRefreshLayout实现下拉刷新

作者头像
103style
发布2022-12-19 12:22:09
4440
发布2022-12-19 12:22:09
举报
文章被收录于专栏:Android开发经验分享

效果图:

代码语言:javascript
复制
MainActivity.java<span style="font-family:Arial;">package com.hnpolice.luoxiaoke.swiperefreshlayout;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {


    @InjectView(R.id.list_view)
    ListView listView;
    @InjectView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    //下拉刷新状态
    private static final int REFRESH_COMPLETE = 110;

    private List<String> datas = new ArrayList<>();

    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        // Set the listener to be notified when a refresh is triggered via the swipe gesture.
        refreshLayout.setOnRefreshListener(this);
        // Set the colors used in the progress animation.
        refreshLayout.setColorSchemeResources(R.color.color_blue, R.color.color_green, R.color.color_red);

        for (int i = 0; i < 5; i++) {
            datas.add("item" + i);
        }

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);

    }

    @Override
    public void onRefresh() {
        //2000  : 2s后执行
        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    datas.add("added");
                    adapter.notifyDataSetChanged();
                    refreshLayout.setRefreshing(false);
                    break;

            }
        }
    };
}</span><span style="font-family:Consolas;">
</span>activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>


colors.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="color_red">#ff0000</color>
    <color name="color_green">#00ff00</color>
    <color name="color_blue">#0000ff</color>
</resources>

工程下载地址:http://download.csdn.net/detail/lxk_1993/9375711
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档