原理:就是动态改变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. How to limit Dialog's max height?

    1. We can make it to play trick in code. At Dialog's show function, after app has set contentView, w ...

  2. How WindowLeaked exception occured?

    If a Activity performDestroy, and there is window not closed whose window token is the Activity's mW ...

  3. 使用JS完成注册表单的数据校验

    1.前台校验 防君子不防小人 JavaScript被设计用来向HTML页面添加交互行为 JavaScript是一种脚本语言(脚本语言是一种轻量级的编程语言) JavaScript由数行可执行计算机代码 ...

  4. Springboot基础知识

    1.@RestController注解 Spring4之后新加入的注解,@RestController是@ResponseBody和@Controller的组合注解.(返回json需要@Respons ...

  5. Ubuntu 中QT 用sogou拼音 安装

    1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...

  6. idea导入项目报错:文档中根元素前面的标记必须格式正确

    今天从git上面导入项目之后,由于是上周刚刚提交过的,本地也没有什么修改,于是就从gitlab上面直接下载下来了.可是项目启动时候,报错了... 文档中根元素前面的标记必须格式正确 想想 原来是上次提 ...

  7. shiro(java安全框架)

    shiro(java安全框架) 以下都是综合之前的人加上自己的一些小总结 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的A ...

  8. css outline属性

    outline属性在一个html element的周围画出一条线来,位于边框的外围,起到突出元素的作用.轮廓线不占据任何布局空间,也不一定非要是矩形. p { outline:#00FF00 dott ...

  9. IONIC调用原生的等待对话框

    ngCordova提供了progressIndicator插件,用以方便的显示等待对话框,但是目前版本仅支持安卓版本.为了让苹果版本也显示,可以到以下地址下载通用的插件,JS部分的代码无需改变: ht ...

  10. 【Leetcode】【Medium】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...