效果

最简单的上拉刷新和下拉刷新,当listview滚动到底部时向上拉刷新数据。当listview滚动到最顶部时下拉刷新。

     

    图1,上拉刷新                  图2,下拉刷新

1.设置lisview

加载header refresh,footer refresh,同时可选择关闭滚动到底部,顶部的动画。

         listview = (ListView) tbhost.findViewById(R.id.tab_weixin_list);
adapter = new TabWeixinListAdapter(context,listview); headerRefresh = tabHost.findViewById(R.id.ViewStub_header_refresh);
footerRefresh = tabHost.findViewById(R.id.ViewStub_footer_refresh); listview.setAdapter(adapter); if(android.os.Build.VERSION.SDK_INT >=){//关闭滚动到顶部,底部动画。
listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
}
listview.setOnTouchListener(touchListener);

2.在onTouchEvent中处理事件

   float startY;
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = event.getY();;
break;
case MotionEvent.ACTION_MOVE:
float y = event.getY();
if (y - startY < ){//上拉
if (!listview.canScrollVertically()){
footerRefresh.setVisibility(View.VISIBLE);
}
}else{//下拉
View cld = listview.getChildAt();
int bottom = cld.getBottom();
int het = cld.getHeight();
if (listview.getFirstVisiblePosition() == && bottom >= het ){
headerRefresh.setVisibility(View.VISIBLE);
}
}
break;
case MotionEvent.ACTION_UP:
headerRefresh.setVisibility(View.GONE);
footerRefresh.setVisibility(View.GONE);
if (listview.getFirstVisiblePosition() == || !listview.canScrollVertically()){
addSomeTableWeixinItem();
}
break;
default:
System.out.println("default ");
break;
}
return false;
}
};

listview.canScrollVertically() 这个函数可以用来判断当前view还可以向上拉多少,如果向上拉0都不可以,就说明在view底部。此函数对下拉不好用。

当 listview.getFirstVisiblePosition() == 0 时 listview滚动到了顶部。

3.footer_refresh_layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_fresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1D1D1"
android:gravity="center_horizontal"
android:orientation="horizontal" > <ProgressBar
android:id="@+id/footer_refresh_prgs"
style="@style/CustomProgressStyle"
android:layout_width="wrap_content"
android:indeterminateDuration="700"
android:layout_height="match_parent" /> <TextView
android:id="@+id/footer_refresh_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="刷新"
android:textSize="20sp" /> </LinearLayout>

4.header_refresh_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_fresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="#D1D1D1"
android:orientation="horizontal" > <ProgressBar
android:id="@+id/header_refresh_prgs"
style="@style/CustomProgressStyle"
android:layout_width="wrap_content"
android:indeterminateDuration="700"
android:layout_height="match_parent" /> <TextView
android:id="@+id/header_refresh_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="刷新"
android:textSize="20sp" /> </LinearLayout>

5.listview的layout

 <?xml version="1.0" encoding="utf-8"?>
<!-- <merge xmlns:android="http://schemas.android.com/apk/res/android" > --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_weixin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ViewStub
android:id="@+id/ViewStub_header_refresh"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#BFBFBF"
android:inflatedId="@+id/tab_weixin_refresh"
android:layout="@layout/header_refresh_layout" > </ViewStub> <TextView android:id="@+id/footer_fresh_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#BFBFBF"
android:gravity="center_horizontal|center_vertical"
android:textSize="20sp"
android:text="@string/find_qq_friends" /> <!-- android:overScrollMode=”never” -->
<ListView
android:id="@+id/tab_weixin_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView> <ViewStub
android:id="@+id/ViewStub_footer_refresh"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#BFBFBF"
android:inflatedId="@+id/tab_weixin_footer_refresh"
android:layout="@layout/footer_refresh_layout" /> </LinearLayout> <!-- </merge> -->

ListView(2)最简单的上拉刷新,下拉刷新的更多相关文章

  1. Flutter学习笔记(25)--ListView实现上拉刷新下拉加载

    如需转载,请注明出处:Flutter学习笔记(25)--ListView实现上拉刷新下拉加载 前面我们有写过ListView的使用:Flutter学习笔记(12)--列表组件,当列表的数据非常多时,需 ...

  2. ListView装上拉电阻下拉刷新

    主要用到了这个几个文件.MainActivity是界面的Activity,MyAdapter是ListView的自己定义适配,MyListView是自己定义带头部LIistView,假设仅仅须要上拉载 ...

  3. Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载

    随着 Google 推出了全新的设计语言 Material Design,还迎来了新的 Android 支持库 v7,其中就包含了 Material Design 设计语言中关于 Card 卡片概念的 ...

  4. 使用iScroll实现上拉或者下拉刷新

    上拉或者下拉刷新的需求在移动端是非常常见的需求,大部分情况下,实现这个效果都使用网上现有的解决方案,例如有人使用swiper这个插件, 也有人使用iScroll这个滚动插件.本文的示例是利用iscro ...

  5. html5+css3实现上拉和下拉刷新

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  6. jQuery模拟原生态App上拉刷新下拉加载

    jQuery模拟原生态App上拉刷新下拉加载效果代码,鼠标上拉时会显示loading字样,并且会模拟加载一条静态数据,支持触屏设备使用. <!doctype html> <html ...

  7. ios 上拉载入下拉刷新Dome

    为练手写了一个小的上拉载入很多其它下拉刷新的小的Dome . 没有太多的技术含量,仅仅是作为新手的启示用.是上一篇下拉载入的扩展.先看一下那个再看这个就easy非常多. Dome下载:http://d ...

  8. 微信小程序 上拉刷新/下拉加载

    小程序项目中上拉刷新下拉加载是比较常见的需求,官方文档也提供了相当友好的API,但是因为API隐藏的比较深,文档描述也比较模糊所以也折腾了一番(官方文档),在此记录一下使用方式 onPullDownR ...

  9. JQuery实现无刷新下拉加载图片

          最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...

  10. Arduino 极速入门系列 - 光控灯(2) - 关于开关,上拉、下拉电阻那些事

    接上篇,这次继续讲解光控灯的另外两个组成部分 - 开关和光敏电阻,光控灯里面将会有自锁开关按钮和光敏电阻.这此主要给新玩电子的朋友解释一下开关按钮的做法. 开关按钮的引脚电平读取问题 - 新手专用 我 ...

随机推荐

  1. java 接口(基础思想一)

    我想,对于各位使用面向对象编程语言的程序员来说,“接口”这个名词一定不陌生,但是不知各位有没有这样的疑惑:接口有什么用途?它和抽象类有什么区别?能不能用抽象类代替接口呢?而且,作为程序员,一定经常听到 ...

  2. snmptrap使用

    SNMP简单网络管理协议,其中其支持的一个命令snmptrap命令,用于模拟向管理机发送trap消息.   启动陷阱方法: snmptrapd -C -c /etc/snmp/snmptrapd.co ...

  3. Install mcrypt for php on Mac OSX 10.10 Yosemite for a Development Server

    mcrypt is a file encryption method using secure techniques to exchange data. It is required for some ...

  4. PropertyDrawer 自定义属性绘图

    public class PlayerAttributeExample : MonoBehaviour { //无滑块的属性 ; //特性限定,有滑块 [Range(, )] ; } Range特性的 ...

  5. python学习小结5:封装、继承、多态

    面向对象程序设计中的类有三大特性: 继承,封装,多态 继承:以普通的类为基础建立专门的类对象 封装:对外部世界隐藏对象的工作细节 多态:可对不同类的对象使用同样的操作 在Python中类的继承定义基本 ...

  6. How to use Android Activity's finish(), onDestory() and System.exit(0) methods

    Activity.finish() Calling this method will let the system know that the programmer wants the current ...

  7. Matlab中@与函数调用

    function m f=@(x) x^2; y(f,3); function y(f,x) disp(num2str(f(x))); end end 函数调用另一个函数的时候,把另一个函数名作为参数 ...

  8. function复习

    #include <iostream> #include <functional> using namespace std; int fun(int a) { std::cou ...

  9. MATLAB——PLOT绘图

    MATLAB——PLOT绘图 格式化绘图: 1.color: b g  r c m y k w blue green red  cyan magenta yellow black white 2.ty ...

  10. ext3grep 恢复删除

    Linux ext2/ext3 文件删除恢复工具ext3grep安装使用 2010-08-23 18:03:10|  分类: 默认分类|举报|字号 订阅     一.           安装前系统环 ...