一、效果演示

ListView实现下拉刷新,是很常见的功能。下面是一个模拟的效果,如下图:

                

                

效果说明:当往下拉ListView的时候,顶部就会有一个“下拉刷新”的标识被拉 出来,再往下拉的时候,标识就会变成”松开刷新“,期间还伴随一个箭头的变化。此时松开手指,则会变成进度条提示正在刷新,刷新完成后,则加载进来刷新的数据。如此反复,就是下拉刷新的功能。

二、准备Demo

其实本质上,ListView实现下拉刷新和实现分页加载都是一样的,都是一个自定义的ListView而已。甚至可以说,原理基本相同,只不过下拉刷新头布局变化相对分页加载复杂一点。

因此,我们仍旧使用《listView实现分页加载》里面的Demo,即首先搭建一个正常下的ListView,准备点模拟的数据。可以点击下面的链接,查看Demo的编写:

http://www.cnblogs.com/fuly550871915/p/4866929.html

三、实现头布局

好了,模拟的东西都准备完成了。下面我们首先编写头布局。比较简单,就是一个箭头,进度条好文本而已。命名为header.xml。代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"> <ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
style="?android:attr/progressBarStyleSmall"/>
<ImageView
android:id="@+id/img_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/down_arrow"/>
<TextView
android:id="@+id/textinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textSize="20dp"
android:text="下拉刷新"/> </LinearLayout>

然后,我们就开始自定义ListView吧。新建类MyListView,继承自ListView。在这里加上头布局即可。代码如下:

 package com.fuly.load;

 import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView; public class MyListView extends ListView{ private View header;//头布局 //三个构造方法都要重写
public MyListView(Context context) {
super(context);
initView( context); }
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView( context); }
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView( context); } public void initView(Context context){ header = LayoutInflater.from(context).inflate(R.layout.header, null); //将头布局加进去
this.addHeaderView(header);
} }

自定义的ListView已经准备好了,下面就替换吧。修改activity_main.xml里的代码即可,如下:

 <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:background="#ccffff"> <com.fuly.load.MyListView
android:id= "@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp"
android:divider="#00cc00"></com.fuly.load.MyListView>
</LinearLayout>

至此,我们已经把带头布局的ListView做好了。运行一下程序,会发现就是效果图的第一张。

那么,怎么来隐藏头布局呢?这并不简单,是下一节要详细说的内容。

ListView实现下拉刷新(一)建立头布局的更多相关文章

  1. Android开发 - 下拉刷新和分段头悬停列表

    项目源码 本文所述项目已开源,源码地址 为什么做PullToRefresh-PinnedSection-ListView 前段时间因为项目需求,需要在Android中对ListView同时增加下拉刷新 ...

  2. ListView实现下拉刷新(三)实现下拉刷新

    该准备的东西都已经准备好了.在这篇文章里,我们就开始实现下拉刷新功能吧. 一.大体的逻辑分析 我们来简单分析一下需要做的逻辑吧.首先分析头布局有几种状态.不下拉时,为正常状态,此时头布局隐藏.下拉到一 ...

  3. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  4. android--------自定义控件ListView实现下拉刷新和上拉加载

    开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,为了方便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能. Android下拉刷新可以分为两种情况: 1.获取 ...

  5. ListView实现下拉刷新(二)隐藏头布局

    一.问题分析 在上一篇中,我们将头布局加到了ListView上.但是没有隐藏他.你可能会想,隐藏还不简单,直接给它设置为GONE属性不就可以了吗,在需要的时候再设定为可见.没错,这正是ListView ...

  6. ListView实现下拉刷新和上拉加载功能

    1 public class RefreshListView extends ListView implements OnScrollListener { private View mHeaderVi ...

  7. 自定义ListView实现下拉刷新,下拉加载的功能

    package com.loaderman.myrefreshlistviewdemo; import android.content.Context; import android.util.Att ...

  8. 【转载】 Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

    Android下拉刷新pullToRefreshListViewGridView 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/3 ...

  9. Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git ...

随机推荐

  1. 通过IntelliJ IDEA忽略掉不需要提交到github的文件

    现如今,开源项目越来越多,存储容器主要有github,国内的码云.开源贡献也是衡量一个开发者是否具有足够的包容能力.技术能力的重要标准. 有些开发者没注意到这些,好心提交开源项目,配置文件也提交上去, ...

  2. touch-slide-image

    用htmls5+css3实现的在android和ios,以及wekit新版浏览器上实现手指滑动切换图片. https://github.com/navyxie/touch-slide-image

  3. 记一次走心One 2 One沟通

    聊的比较零零碎碎,内容比较散,有些solution不错,记一些要点吧(1)要学会汇报,就是坐你身边的人,也未必知道你在干啥 三个人都在砌墙.当人们分别问他们在做什么,他们的答案却不一样:第一个人头也没 ...

  4. Truncated incorrect DOUBLE value: 'NO_REFUND'

    解决办法:Mysql中,如果一个字段是字符串,则一定要加单引号 问题原因: `item_refund_state` ) NOT NULL item_refund_state字段的类型是varchar但 ...

  5. 常用工具说明--强大的PostMan

    PsotMan介绍和使用 Postman介绍 Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件.其主要功能包括: 模拟各种HTTP ...

  6. Linux定时任务crontab使用指南

    crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查 ...

  7. spring-boot之简单定时任务

    首先是pom.xml依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  8. linux环境的基本搭建

    1.准备Linux环境(我的是centos系统) 如果你是hadoop用户在使用sudo之前需要配置一下:获取sudo权限 切换到root vi /etc/sudoersroot ALL=(ALL) ...

  9. java多线程之ReentrantLock

    前言 相信学过java的人都知道 synchronized 这个关键词,也知道它用于控制多线程对并发资源的安全访问,兴许,你还用过Lock相关的功能,但你可能从来没有想过java中的锁底层的机制是怎么 ...

  10. Algorithm——最长公共前缀

    一.问题 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...