原理:就是动态改变ScrollView header的margin实现

主要的代码:

http://blog.csdn.net/swust_chenpeng/article/details/39289721

代码其实还是比较容易,但是但是,自己还是花了很多时间,脑袋瓜不够灵活呀...

下面是ScrollViewHeader的代码:

public class ScrollViewHeader extends RelativeLayout {  

    public final static int STATE_NORMAL = 0;
public final static int STATE_READY = 1;
public final static int STATE_REFRESHING = 2;
private final int ROTATE_ANIM_DURATION = 180;
private int topMargin = 0;
private int state = STATE_NORMAL;
private TextView refreshTv = null;
private TextView refreshTimeTv = null;
private ProgressBar refreshProgress = null;
private ImageView refreshArrow = null;
private Animation animationUp = null;
private Animation animationDown = null; public ScrollViewHeader(Context context) {
super(context);
// TODO Auto-generated constructor stub
if(!isInEditMode())
initView(context);
} public ScrollViewHeader(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
if(!isInEditMode())
initView(context);
} public ScrollViewHeader(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
if(!isInEditMode())
initView(context);
} /**
* 初始化相关的view
*/
public void initView(Context context) {
animationDown = new RotateAnimation(-180f, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationDown.setDuration(ROTATE_ANIM_DURATION);
animationDown.setFillAfter(true);
animationUp = new RotateAnimation(0, -180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationUp.setDuration(ROTATE_ANIM_DURATION);
animationUp.setFillAfter(true);
setPadding(10, 25, 10, 25);
View view = LayoutInflater.from(context).inflate(R.layout.scrollview_header, this, true);
refreshTv = (TextView) view.findViewById(R.id.refresh_text);
refreshTimeTv = (TextView) view.findViewById(R.id.refresh_time);
refreshProgress = (ProgressBar) view.findViewById(R.id.refresh_progress);
refreshArrow = (ImageView) view.findViewById(R.id.refresh_arrow);
} /**
* 设置scrollviewHeader的状态
* @param state
*/
public void setState(int state) {
if(this.state == state) {
return ;
}
switch (state) {
case STATE_NORMAL:
refreshTv.setText("下拉刷新");
refreshArrow.setVisibility(View.VISIBLE);
refreshProgress.setVisibility(View.INVISIBLE);
if(this.state == STATE_READY) {
refreshArrow.startAnimation(animationDown);
} else if(this.state == STATE_REFRESHING) {
refreshArrow.clearAnimation();
}
break;
case STATE_READY:
refreshTv.setText("松开刷新");
refreshArrow.setVisibility(View.VISIBLE);
refreshProgress.setVisibility(View.INVISIBLE);
refreshArrow.startAnimation(animationUp);
break;
case STATE_REFRESHING:
refreshTv.setText("正在加载...");
refreshProgress.setVisibility(View.VISIBLE);
refreshArrow.clearAnimation();
refreshArrow.setVisibility(View.INVISIBLE);
break;
default:
break;
}
this.state = state;
} /**
* 更新header的margin
* @param margin
*/
public void updateMargin(int margin) {
//这里用Linearlayout的原因是Headerview的父控件是scrollcontainer是一个linearlayout
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) this.getLayoutParams();
params.topMargin = margin;
topMargin = margin;
setLayoutParams(params);
} /**
* 获取header的margin
* @return
*/
public int getTopMargin() {
return topMargin;
}
}

header的布局文件,scrollview_header

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" > <LinearLayout
android:id="@+id/refresh_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/refresh_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新" /> <TextView
android:id="@+id/refresh_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5分钟前更新" />
</LinearLayout> <ProgressBar
android:id="@+id/refresh_progress"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="@id/refresh_des"
android:visibility="invisible" /> <ImageView
android:id="@+id/refresh_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:layout_toLeftOf="@id/refresh_des"
android:src="@drawable/arrow" /> </merge>

好了,相关的源码就只有3个文件...

分享一个可下拉刷新的ScrollView的更多相关文章

  1. [RN] React Native 使用 FlatList 和 ScrollView 的下拉刷新问题

    React Native 使用 FlatList 和 ScrollView 实现 下拉刷新时,RefreshControl 控件不起作用, 后来经查明,原来 RefreshControl 要加在 Sc ...

  2. 【微信小程序】scroll-view 的上拉加载和下拉刷新

    1.在微信小程序中,想到 下拉刷新 和 上拉加载,如果是整个页面都拖动的话,可以在页面配置中,配置 enablePullDownRefresh 和 onReachBottomDistance 然后在 ...

  3. scroll-view组件实现下拉刷新, 拉到底加载更多

    官方文档已声明,即使在page.json和app.json中开启下拉刷新,scroll-view组件也是不支持的.但我们可以通过曲线救国的方法来实现 实现代码 // wxml <scroll-v ...

  4. PullToRefresh使用详解(一)--构建下拉刷新的listView

    前言:前几天写了篇关于PullToRefresh控件的DEMO导入的博客,但由于当时没有用到,所以就没细往下讲,现在开始到了实战阶段,用到了PullToRefresh的listView样式,网上有讲的 ...

  5. UITableView:下拉刷新和上拉加载更多

    [转载请注明出处] 本文将说明让UIScrollView支持"下拉刷新"和"上拉加载更多"的实现机制,并实现一个可用的tableView子类,以下主要以&quo ...

  6. 指令汇B新闻客户端开发(三) 下拉刷新

    现在我们继续这个新闻客户端的开发,今天分享的是下拉刷新的实现,我们都知道下拉刷新是一个应用很常见也很实用的功能.我这个应用是通过拉ListView来实现刷新的,先看一张刷新的原理图 从图中可知,手指移 ...

  7. 安卓开发笔记——关于开源组件PullToRefresh实现下拉刷新和上拉加载(一分钟搞定,超级简单)

    前言 以前在实现ListView下拉刷新和上拉加载数据的时候都是去继承原生的ListView重写它的一些方法,实现起来非常繁杂,需要我们自己去给ListView定制下拉刷新和上拉加载的布局文件,然后添 ...

  8. iOS 下拉刷新-上拉加载原理

    前言 讲下拉刷新及上拉加载之前先给大家解释UIScrollView的几个属性 contentSize是UIScrollView可以滚动的区域. contentOfinset 苹果官方文档的解释是&qu ...

  9. Android下拉刷新底部操作栏的隐藏问题

    最近自己编写下拉刷新的时候,发现了一个问题,就是有一个需求是这样的:要求页面中是一个Tab切换界面,一个界面有底部操作栏,不可下拉刷新,另一个界面没有底部操作栏,但可以下拉刷新. 按照平常的做法,我在 ...

随机推荐

  1. JBPM学习第2篇:为Eclipse添加JBPM开发支持

    1.Eclipse添加JBoss支持插件 参考:Eclipse添加JBoss支持 若已安装,直接跳过! 2.Eclipse添加Drools插件 jbpm-installer-full解压后的文件夹中找 ...

  2. Scrapy爬虫requests

    requests 模块 模块的由来: 浏览器可以浏览网站, 是由于浏览器发送了requests , 各种请求.打开一个网站可能有几十到几百个请求. 从而服务器端会反馈各种因应不同请求生成的数据. 我们 ...

  3. Java设计模式—观察者模式

    观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/subscribe). 其定义如下: 定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都 ...

  4. 通过JTS源码分析Rtree(未完待续)

    前言 R树在数据库等领域做出的功绩是非常显著的.它很好的解决了在高维空间搜索等问题.它把B树的思想很好的扩展到了多维空间,采用了B树分割空间的思想,并在添加.删除操作时采用合并.分解结点的方法,保证树 ...

  5. 使用Charles抓包获取API

    在进行程序的开发之前,我们需要获得物流唐山APP的API,在这里我推荐大家使用Charles抓取数据包获得API.以下是Charles说明: Charles 是在 Mac 下常用的网络封包截取工具,在 ...

  6. 在Linux中安装redmine

    Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统. 如下即为安装步骤: (1)配置ruby环境,可用rvm进行安装匹配,参考http://ruby- ...

  7. 利用NVM在系统中维护多个版本的nodejs

      0. 背景 开发时可能同时进行多个项目,而这些项目所依赖的node版本又不是一样的.比如我现在的angular项目采用的node是8.9.3版本,而vue项目的vue-cli则依赖更高.由于ang ...

  8. webstorm javascript IDE调试

    webstorm是我见过的前端开发调试最好用的IDE工具了,它不仅具有强大的编辑,代码查阅引用功能,更有强大的js调试功能,这是任何通过firebug,chrome devtool,console.l ...

  9. 一张图看懂CSS cascade, specific, importance, inheritance

    http://www.w3.org/TR/CSS21/cascade.html#inheritance http://www.w3.org/TR/CSS21/cascade.html#cascade ...

  10. SQL点点滴滴_唯一索引设计指南-转载

    唯一索引能够保证索引键中不包含重复的值, 从而使表中的每一行从某种方式上具有唯一性, 只有当唯一性是数据本身的特征时, 指定唯一索引才有意义. 例如, 如果您希望确保 HumanResources.E ...