ListView(2)最简单的上拉刷新、下拉刷新代码
效果
最简单的上拉刷新和下拉刷新,当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)最简单的上拉刷新、下拉刷新代码的更多相关文章
- Flutter学习笔记(25)--ListView实现上拉刷新下拉加载
如需转载,请注明出处:Flutter学习笔记(25)--ListView实现上拉刷新下拉加载 前面我们有写过ListView的使用:Flutter学习笔记(12)--列表组件,当列表的数据非常多时,需 ...
- ListView装上拉电阻下拉刷新
主要用到了这个几个文件.MainActivity是界面的Activity,MyAdapter是ListView的自己定义适配,MyListView是自己定义带头部LIistView,假设仅仅须要上拉载 ...
- Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载
随着 Google 推出了全新的设计语言 Material Design,还迎来了新的 Android 支持库 v7,其中就包含了 Material Design 设计语言中关于 Card 卡片概念的 ...
- 使用iScroll实现上拉或者下拉刷新
上拉或者下拉刷新的需求在移动端是非常常见的需求,大部分情况下,实现这个效果都使用网上现有的解决方案,例如有人使用swiper这个插件, 也有人使用iScroll这个滚动插件.本文的示例是利用iscro ...
- html5+css3实现上拉和下拉刷新
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- jQuery模拟原生态App上拉刷新下拉加载
jQuery模拟原生态App上拉刷新下拉加载效果代码,鼠标上拉时会显示loading字样,并且会模拟加载一条静态数据,支持触屏设备使用. <!doctype html> <html ...
- ios 上拉载入下拉刷新Dome
为练手写了一个小的上拉载入很多其它下拉刷新的小的Dome . 没有太多的技术含量,仅仅是作为新手的启示用.是上一篇下拉载入的扩展.先看一下那个再看这个就easy非常多. Dome下载:http://d ...
- 微信小程序 上拉刷新/下拉加载
小程序项目中上拉刷新下拉加载是比较常见的需求,官方文档也提供了相当友好的API,但是因为API隐藏的比较深,文档描述也比较模糊所以也折腾了一番(官方文档),在此记录一下使用方式 onPullDownR ...
- JQuery实现无刷新下拉加载图片
最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...
- Arduino 极速入门系列 - 光控灯(2) - 关于开关,上拉、下拉电阻那些事
接上篇,这次继续讲解光控灯的另外两个组成部分 - 开关和光敏电阻,光控灯里面将会有自锁开关按钮和光敏电阻.这此主要给新玩电子的朋友解释一下开关按钮的做法. 开关按钮的引脚电平读取问题 - 新手专用 我 ...
随机推荐
- Spring装配之——JAVA代码装配Bean
首先创建几个普通的JAVA对象,用于测试JAVA代码装配bean的功能. package soundsystemJava; //作为接口 定义了CD播放器对一盘CD所能进行的操作 public int ...
- awk 新手入门笔记
转自:http://www.habadog.com/2011/05/22/awk-freshman-handbook/ awk新手入门笔记 @作者 : habadog@邮箱 : habadog1203 ...
- c3p0 连接池配置
C3P0 可使用properties 配置文件方式,将c3p0.properties放在classpath目录下,如果为WEB应用,放在WEB-INF\classes下 c3p0.properties ...
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...
- Ubuntu 18.04 nvidia driver 390.48 安装 TensorFlow 1.12.0 和 PyTorch 1.0.0 详细教程
最近要在个人台式机上搭建TensorFlow和PyTorch运行环境,期间遇到了一些问题.这里就把解决的过程记录下来,同时也可以作为安装上述环境的过程记录. 如果没有遇到类似的问题,想直接从零安装上述 ...
- JDK5-8特性归纳
jdk5新特性1.自动装箱和拆箱2.枚举3.静态导入4.可变参数5.內省 是Java语言对Bean类属性.事件的一种缺省处理方法.例如类A中有属性那么,那我们可以通过getName,setName ...
- 远程连接Ubuntu的桌面
参考:http://www.linuxidc.com/Linux/2016-06/132442.htm http://teliute.org/linux/TeUbt/lesson52/lesson52 ...
- BZOJ 1602 牧场行走
直接写一波Lca就好了 #include<cstdio> #include<cmath> #include<algorithm> using namespace s ...
- Debug : array type has incomplete element type
array type has incomplete element type extern struct SoundReport SoundList[32]; ///// 多写了 st ...
- R语言 EFA(探索性因子分析)
EFA的目标是通过发掘隐藏在数据下的一组较少的.更为基本的无法观测的变量,来解释一组可观测变量的相关性.这些虚拟的.无法观测的变量称作因子.(每个因子被认为可解释多个观测变量间共有的方差,也叫作公共因 ...