概述

  谷歌官方推出了SwipeRefreshLayout 来实现下拉刷新的效果。对比以前我们常用的 pull-to-refesh ,这个方案显得更加的简单方便。

关联项目引用(管理依赖)

  在你的 应用级别的 build.gradle  中添加如下:  

   compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:support-v4:23.0.0'

编写布局(Layout)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

在代码中  添加监听器处理事件

  public void setOnRefreshListener(OnRefreshListener listener)
  

完整代码

package demo.vir56k.swiperefreshlayoutdemo;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class MainActivity extends AppCompatActivity {
private static final int REFRESH_COMPLETE = 0X110;
private SwipeRefreshLayout mSwipeLayout;
private ListView mListView;
private ArrayAdapter<String> mAdapter;
private List<String> mDatas; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listview1);
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout1); mDatas = creatDataSource();
mSwipeLayout.setOnRefreshListener(mSwipeRefreshLayoutOnRefreshListener);
mSwipeLayout.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);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
mListView.setAdapter(mAdapter);
} private List<String> creatDataSource() {
mDatas = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
mDatas.add("item" + i);
}
return mDatas;
} SwipeRefreshLayout.OnRefreshListener mSwipeRefreshLayoutOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
}; private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case REFRESH_COMPLETE:
List<String> lst = new ArrayList<String>();
for (int i = mDatas.size() + 3; i > mDatas.size(); i--) {
lst.add("item -" + i);
}
mDatas.addAll(0, lst);
mAdapter.notifyDataSetChanged();
mSwipeLayout.setRefreshing(false);
break; }
} ;
};
}

至此完成。

补充:

其他情形,遇到"SwipeRefreshLayout 的子控件不 直接是个listview 等,是个普通 relativewlayout 怎么办?"

解决方法: 继承 SwipeRefreshLayout 重写 canChildScrollUp 方法

/*
* Copyright 2016, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package com.example.android.architecture.blueprints.todoapp.tasks; import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View; /**
* Extends {@link SwipeRefreshLayout} to support non-direct descendant scrolling views.
* <p>
* {@link SwipeRefreshLayout} works as expected when a scroll view is a direct child: it triggers
* the refresh only when the view is on top. This class adds a way (@link #setScrollUpChild} to
* define which view controls this behavior.
*/
public class ScrollChildSwipeRefreshLayout extends SwipeRefreshLayout { private View mScrollUpChild; public ScrollChildSwipeRefreshLayout(Context context) {
super(context);
} public ScrollChildSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean canChildScrollUp() {
if (mScrollUpChild != null) {
return ViewCompat.canScrollVertically(mScrollUpChild, -1);
}
return super.canChildScrollUp();
} public void setScrollUpChild(View view) {
mScrollUpChild = view;
}
}

  

  

  

android开发(49) Android 下拉刷新的实现。使用 SwipeRefreshLayout 代替 pull-to-refesh的更多相关文章

  1. Android仿苹果版QQ下拉刷新实现(二) ——贝塞尔曲线开发"鼻涕"下拉粘连效果

    前言 接着上一期Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件 的博客开始,同样,在开始前我们先来看一下目标效果: 下面上一下本章需要实现的效果图: 大家看到这个效果 ...

  2. Android学习之——ListView下拉刷新

    背景知识 ListView使用非常广泛,对于使用ListView的应用来说,下拉刷新是必不可少要实现的功能. 我们常用的微博.网易新闻,搜狐新闻都使用了这一功能,如下图所示.     微博 搜狐新闻 ...

  3. Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件

    前言: 忙完了结婚乐APP的开发,终于可以花一定的时间放在博客上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果:      因为此效果实现的步骤 ...

  4. Android—自定义控件实现ListView下拉刷新

    这篇博客为大家介绍一个android常见的功能——ListView下拉刷新(参考自他人博客,网址忘记了,阅读他的代码自己理解注释的,希望能帮助到大家): 首先下拉未松手时候手机显示这样的界面: 下面的 ...

  5. Android PullToRefresh (GridView 下拉刷新上拉加载)

    做这个需要自己去git hub上下载个pull-to-refresh 里面有个library为依赖包自己导到自己的项目中 (下载地址:https://github.com/chrisbanes/And ...

  6. Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)

    PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie ...

  7. android 引用 project以及下拉刷新开源类库Android-PullToRefresh 的使用

    Android-PullToRefresh 是一个github上的开源下拉刷新类库, GitHub  .此外,该作者还有另外一个实用度和关注量极高的项目–另一种Android ActionBar的实现 ...

  8. Android 之WebView实现下拉刷新和其他相关刷新功能

    最近项目中需要用到WebView下拉刷新的功能,经过查找资料终于完成了此功能,现在拿出来和大家分享一下.希望对大家有所帮助. 效果如下图:   代码: activity.xml <?xml ve ...

  9. Android如何定制一个下拉刷新,上滑加载更多的容器

    前言 下拉刷新和上滑加载更多,是一种比较常用的列表数据交互方式. android提供了原生的下拉刷新容器 SwipeRefreshLayout,可惜样式不能定制. 于是打算自己实现一个专用的.但是下拉 ...

随机推荐

  1. loj#2013. 「SCOI2016」幸运数字 点分治/线性基

    题目链接 loj#2013. 「SCOI2016」幸运数字 题解 和树上路径有管...点分治吧 把询问挂到点上 求出重心后,求出重心到每个点路径上的数的线性基 对于重心为lca的合并寻味,否则标记下传 ...

  2. BZOJ.2160.拉拉队排练(Manacher)

    题目链接 \(Description\) 求给定字符串中 最长的k个回文串长度的乘积(要求回文串长度为奇数):若奇数长度回文串不足k个则输出-1.(len<=10^6,k<=10^12) ...

  3. [JSOI2004]平衡点/[BZOJ3680]吊打XXX

    [JSOI2004]平衡点/[BZOJ3680]吊打XXX 题目大意: 有\(n(n\le10000)\)个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.假设绳子 ...

  4. Instruments

    链接: iOS性能优化:Instruments使用实战 iOS 使用Instruments的工具小结    

  5. EF6 简单增删改查示例代码

    示例一: private DbContext _dbContext; public DbContext CurrentContext { get { if (_dbContext == null) { ...

  6. 学习shader之前必须知道的东西之计算机图形学-渲染管线

    引言 shader到底是干什么用的?shader的工作原理是什么? 其实当我们对这个问题还很懵懂的时候,就已经开始急不可耐的要四处搜寻有关shader的资料,恨不得立刻上手写一个出来.但看了一些资料甚 ...

  7. .ipynb文件转.py文件

    到要转的demo.ipynb所在目录下,然后输入命令: jupyter nbconvert --to script demo.ipynb

  8. MDX 查询原型

    本篇文章记录 SBS 中 MDX 查询原型,可以根据这些查询原型来解决实际项目中的问题. 1. 查询在 2004年1月2日 - 2004年3月1日之间购买过 Bikes 产品的用户. SELECT ( ...

  9. C# Chart使用总结 2 ---------chart悬停选定数值操作

    1.用鼠标悬停事件处理 private void Form1_Load(object sender, EventArgs e) { //connStr = connPath1 + conn2; fil ...

  10. eclipse default handler IHandler interface “the chosen operation is not enabled”

    NOTE: These two methods: Tip: Subclass AbstractHandler rather than implementing IHandler. but you ca ...