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,可惜样式不能定制. 于是打算自己实现一个专用的.但是下拉 ...
随机推荐
- git 详细教程和常用操作指令
git 内部工作原理图 如上图,git 一般可以分为三个区:工作区.暂存区.版本库,通常类似 git add等命令都是与index 暂存区的交互,git commit指令则是 index 与版本库的交 ...
- 构造函数,super()
一.构造函数的作用 创建类的对象,只有对象才能调用一个类中的方法和属性 二.在有继承关系的类中,创建子类对象,初始化的顺序 先父类构造 —>后子类构造 为什么父类构造会被调用?因为有继承关系,子 ...
- 阻止默认事件preventDefault与returnValue
通过阻止默认事件,禁止鼠标右键的使用 document.getElementById('dd').oncontextmenu = function(ev){ event = ev || window. ...
- Javascript中快速退出多重循环的技巧
在双重循环或多重循环中判断条件,条件符合时跳出整个嵌套循环体是常见的程序逻辑.在Javascript中有哪些跳出的方法呢?楼主简单整理了一下. 一. 使用多个break语句跳出 var breaked ...
- Synchronized、lock、volatile、ThreadLocal、原子性总结、Condition
http://blog.csdn.net/sinat_29621543/article/details/78065062
- android:碎片的使用方式
介绍了这么多抽象的东西,也是时候应该学习一下碎片的具体用法了.你已经知道,碎 片通常都是在平板开发中才会使用的,因此我们首先要做的就是新建一个平板电脑的模拟 器.由于 4.0 系统的平板模拟器好像存在 ...
- ArcGIS教程:曲率
摘要 计算栅格表面的曲率,包括剖面曲率和平面曲率. 用法 · 主要输出结果为每个像元的表面曲率,该值通过将该像元与八个相邻像元拟合而得.曲率是表面的二阶导数,或者可称之为坡度的坡度.可供选择的输出曲率 ...
- FXAA,FSAA与MSAA有什么区别?效果和性能上哪个好
而MSAA基本上只对画面中物体的边缘进行放大.混合的抗锯操作,因为边缘是锯齿最明显的地方(注意不是所有的边缘).提取边缘,主要是结合深度技术.MSAA是种硬件AA.我们一般说的4x.8x,就是放大倍数 ...
- ASP.NET CORE 之 在IIS上部署MVC项目
与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器(Kestrel)运行,IIS则是作为反向代理的角色转发请求到Kestrel ...
- Gson解析第三方提供Json数据(天气预报,新闻等)
之前都是自己写后台,自己的server提供数据给client. 近期在看第三方的数据接口,訪问其它站点提供的信息.比方.我们可能自己收集的数据相当有限.可是网上提供了非常多关于天气预报.新闻.星座运势 ...