android开发(49) Android 下拉刷新的实现。使用 SwipeRefreshLayout 代替 pull-to-refesh
概述
谷歌官方推出了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的更多相关文章
- Android仿苹果版QQ下拉刷新实现(二) ——贝塞尔曲线开发"鼻涕"下拉粘连效果
前言 接着上一期Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件 的博客开始,同样,在开始前我们先来看一下目标效果: 下面上一下本章需要实现的效果图: 大家看到这个效果 ...
- Android学习之——ListView下拉刷新
背景知识 ListView使用非常广泛,对于使用ListView的应用来说,下拉刷新是必不可少要实现的功能. 我们常用的微博.网易新闻,搜狐新闻都使用了这一功能,如下图所示. 微博 搜狐新闻 ...
- Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件
前言: 忙完了结婚乐APP的开发,终于可以花一定的时间放在博客上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果: 因为此效果实现的步骤 ...
- Android—自定义控件实现ListView下拉刷新
这篇博客为大家介绍一个android常见的功能——ListView下拉刷新(参考自他人博客,网址忘记了,阅读他的代码自己理解注释的,希望能帮助到大家): 首先下拉未松手时候手机显示这样的界面: 下面的 ...
- Android PullToRefresh (GridView 下拉刷新上拉加载)
做这个需要自己去git hub上下载个pull-to-refresh 里面有个library为依赖包自己导到自己的项目中 (下载地址:https://github.com/chrisbanes/And ...
- Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)
PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie ...
- android 引用 project以及下拉刷新开源类库Android-PullToRefresh 的使用
Android-PullToRefresh 是一个github上的开源下拉刷新类库, GitHub .此外,该作者还有另外一个实用度和关注量极高的项目–另一种Android ActionBar的实现 ...
- Android 之WebView实现下拉刷新和其他相关刷新功能
最近项目中需要用到WebView下拉刷新的功能,经过查找资料终于完成了此功能,现在拿出来和大家分享一下.希望对大家有所帮助. 效果如下图: 代码: activity.xml <?xml ve ...
- Android如何定制一个下拉刷新,上滑加载更多的容器
前言 下拉刷新和上滑加载更多,是一种比较常用的列表数据交互方式. android提供了原生的下拉刷新容器 SwipeRefreshLayout,可惜样式不能定制. 于是打算自己实现一个专用的.但是下拉 ...
随机推荐
- Ubuntu urllib2.URLError:<urlopen error unknown url type:https>
描述: python中urllib2 下载网页时,出现错误urllib2.URLError:<urlopen error unknown url type:https> 解决方法: pyt ...
- golang中发送http请求的几种常见情况
整理一下golang中各种http的发送方式 方式一 使用http.Newrequest 先生成http.client -> 再生成 http.request -> 之后提交请求:clie ...
- Codeforces.788C.The Great Mixing(bitset DP / BFS)
题目链接 \(Description\) 有k种饮料,浓度Ai给出,求用最少的体积配成n/1000浓度的饮料. \(Solution\) 根据题意有方程 (A1x1+A2x2+...+Anxn)/[( ...
- Linux学习笔记12—磁盘管理
一.查看磁盘或目录的容量 1. df命令 作用:查看已挂载磁盘的总容量.使用容量.剩余容量等,可以不加任何参数,默认是按k为单位显示的 参数: -I : 查看inodes使用状况 -h: 使用合适的 ...
- cached-query 将缓存和查询数据库高速连接起来的轻类库
介绍 我们经常有这种需求:当我们把memcached增加到项目后我还还要写一个 cacheUtils 或者 cacheManager 之类的类来操作memcached. 而且一般的操作不外乎是这种操作 ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- LiteIDE 在 Windows 下为 Go 语言添加智能提示代码补全
本文以 Windows 7 64 位为环境,go1.4.2.windows-amd64 和 liteidex27.2.1.windows-qt5 为例. 成功搭建开发环境后,发现 LiteIDE 没有 ...
- 通过action传过来的值在option获取进行验证
通过action传过来的值在option获取进行验证的方法: for(var i=0;i<document.getElementById("ufacilityType").o ...
- SoapUI Pro Project Solution Collection-Test Step Object
Package com.eviware.soapui.model.testsuite used for access the current testsuite object, like test c ...
- orocos_kdl学习(一):坐标系变换
KDL中提供了点(point).坐标系(frame).刚体速度(twist),以及6维力/力矩(wrench)等基本几何元素,具体可以参考 Geometric primitives 文档. Creat ...