一、效果演示

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. Codeforces 494E. Sharti

    Description 有一个 \(n*n\) 的矩形,给出 \(m\) 个子矩形,这些矩形内部的点都是白色的,其余的点都是黑色,每一次你可以选择一个变长不超过 \(k\) 的正方形,满足这个正方形的 ...

  2. Java并发编程之volatile关键字解析

    一内存模型的相关概念 二并发编程中的三个概念 三Java内存模型 四深入剖析volatile关键字 五使用volatile关键字的场景 volatile这个关键字可能很多朋友都听说过,或许也都用过.在 ...

  3. WebStorm 用法集合

    1. 图片宽高提示.&lt;img src="https://pic4.zhimg.com/8345475b687c83a71e0564419b0ac733_b.jpg" ...

  4. WPF基础控件

    1.所有基础控件结束(英文) http://www.dotnetperls.com/tooltip-wpf 2.wpf- DataGrid 常用属性和事件 3.

  5. FTPS Firewall

    989 for the FTPS data channel implicit FTPS was expected to listen on the IANA Well Known Port 990/T ...

  6. group by 语句

    user E_book go 这样的程序会出错,因为play没有使用sum,所以要分组. group by play 有函数的和没有函数的表一起使用要用 GROUP BY .AVG 求平均值,只能与数 ...

  7. 建一个springboot项目

    1.打开 https://start.spring.io/ 新建一个maven的demo,这里选择的是1.5.18的版本 2.将自动生成的demo导入eclipse,直接选中File里的import, ...

  8. mac通过wine运行windows程序(将文件关联到windows程序notepad++)

    windows程序功能强大且已经习惯使用了,用mac总感觉不给力,例如记事本工具Notepad++就非常优秀.下面介绍如何在mac系统下通过wine来安装使用notepadd++程序. 1.安装win ...

  9. Effective C++ .14 智能指针的拷贝与deleter函数

    #include <iostream> #include <cstdlib> #include <memory> using namespace std; clas ...

  10. css-flex布局知识梳理

    一.传统的布局 布局的传统解决方案,基于盒装模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 二. Flex的 ...