設置向下拉動更新。

 package com.mycompany.Scroll_test;

 import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*; /**
* 刷新控制view
*
* @author Nono
*
*/
public class RefreshableView extends LinearLayout { private static final String TAG = "LILITH";
private Scroller scroller;
private View refreshView;
private ImageView refreshIndicatorView;
private int refreshTargetTop = -60;
private ProgressBar bar;
private TextView downTextView;
private TextView timeTextView; private RefreshListener refreshListener; private String downTextString;
private String releaseTextString; private Long refreshTime = null;
private int lastX;
private int lastY;
// 拉動標記
private boolean isDragging = false;
// 是否可刷新標記
private boolean isRefreshEnabled = true;
// 在刷新中標記
private boolean isRefreshing = false; private Context mContext;
public RefreshableView(Context context) {
super(context);
mContext = context; }
public RefreshableView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init(); }
private void init() {
// TODO Auto-generated method stub
//滑動對象,
scroller = new Scroller(mContext); //刷新視圖頂端的的view
refreshView = LayoutInflater.from(mContext).inflate(R.layout.refresh_top_item, null);
//指示器view
refreshIndicatorView = (ImageView) refreshView.findViewById(R.id.indicator);
//刷新bar
bar = (ProgressBar) refreshView.findViewById(R.id.progress);
//下拉顯示text
downTextView = (TextView) refreshView.findViewById(R.id.refresh_hint);
//下來顯示時間
timeTextView = (TextView) refreshView.findViewById(R.id.refresh_time); LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, -refreshTargetTop);
lp.topMargin = refreshTargetTop;
lp.gravity = Gravity.CENTER;
addView(refreshView, lp);
downTextString = mContext.getResources().getString(R.string.refresh_down_text);
releaseTextString = mContext.getResources().getString(R.string.refresh_release_text);
} /**
* 刷新
* @param time
*/
private void setRefreshText(String time) {
// TODO Auto-generated method stub
//timeTextView.setText(time);
} @Override
public boolean onTouchEvent(MotionEvent event) { int y= (int) event.getRawY(); switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//記錄下y坐標
lastY = y;
break; case MotionEvent.ACTION_MOVE:
Log.i(TAG, "ACTION_MOVE");
//y移動坐標
int m = y - lastY;
if(((m < 6) && (m > -1)) || (!isDragging )){
doMovement(m);
}
//記錄下此刻y坐標
this.lastY = y;
break; case MotionEvent.ACTION_UP:
Log.i(TAG, "ACTION_UP"); fling(); break;
}
return true;
} /**
* up事件處理
*/
private void fling() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LayoutParams) refreshView.getLayoutParams();
Log.i(TAG, "fling()" + lp.topMargin);
if(lp.topMargin > 0){//拉到了觸發可刷新事件
refresh();
}else{
returnInitState();
}
} private void returnInitState() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int i = lp.topMargin;
scroller.startScroll(0, i, 0, refreshTargetTop);
invalidate();
}
private void refresh() {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int i = lp.topMargin;
refreshIndicatorView.setVisibility(View.GONE);
bar.setVisibility(View.VISIBLE);
timeTextView.setVisibility(View.GONE);
downTextView.setVisibility(View.GONE);
scroller.startScroll(0, i, 0, 0-i);
invalidate();
if(refreshListener !=null){
refreshListener.onRefresh(this);
isRefreshing = true;
}
} /**
*
*/
@Override
public void computeScroll() {
// TODO Auto-generated method stub
if(scroller.computeScrollOffset()){
int i = this.scroller.getCurrY();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int k = Math.max(i, refreshTargetTop);
lp.topMargin = k;
this.refreshView.setLayoutParams(lp);
this.refreshView.invalidate();
invalidate();
}
}
/**
* 下拉move事件處理
* @param moveY
*/
private void doMovement(int moveY) {
// TODO Auto-generated method stub
LinearLayout.LayoutParams lp = (LayoutParams) refreshView.getLayoutParams();
if(moveY > 0){
//獲取view的上邊距
float f1 =lp.topMargin;
float f2 = moveY * 0.3F;
int i = (int)(f1+f2);
//修改上邊距
lp.topMargin = i;
//修改後刷新
refreshView.setLayoutParams(lp);
refreshView.invalidate();
invalidate();
}
timeTextView.setVisibility(View.VISIBLE);
if(refreshTime!= null){
setRefreshTime(refreshTime);
}
downTextView.setVisibility(View.VISIBLE); refreshIndicatorView.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
if(lp.topMargin > 0){
downTextView.setText(R.string.refresh_release_text);
refreshIndicatorView.setImageResource(R.drawable.refresh_arrow_up);
}else{
downTextView.setText(R.string.refresh_down_text);
refreshIndicatorView.setImageResource(R.drawable.refresh_arrow_down);
} } public void setRefreshEnabled(boolean b) {
this.isRefreshEnabled = b;
} public void setRefreshListener(RefreshListener listener) {
this.refreshListener = listener;
} /**
* 刷新時間
* @param refreshTime2
*/
private void setRefreshTime(Long time) {
// TODO Auto-generated method stub } /**
* 結束刷新事件
*/
public void finishRefresh(){
Log.i(TAG, "執行了=====finishRefresh");
LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams)this.refreshView.getLayoutParams();
int i = lp.topMargin;
refreshIndicatorView.setVisibility(View.VISIBLE);
timeTextView.setVisibility(View.VISIBLE);
scroller.startScroll(0, i, 0, refreshTargetTop);
invalidate();
isRefreshing = false;
} /*該方法一般和ontouchEvent 一起用
* (non-Javadoc)
* @see android.view.ViewGroup#onInterceptTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
// TODO Auto-generated method stub
int action = e.getAction();
int y= (int) e.getRawY();
switch (action) {
case MotionEvent.ACTION_DOWN:
lastY = y;
break; case MotionEvent.ACTION_MOVE:
//y移動坐標
int m = y - lastY; //記錄下此刻y坐標
this.lastY = y;
if(m > 6 && canScroll()){
return true;
}
break;
case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_CANCEL: break;
}
return false;
}
private boolean canScroll() {
// TODO Auto-generated method stub
View childView;
if(getChildCount()>1){
childView = this.getChildAt(1);
if(childView instanceof ListView){
int top =((ListView)childView).getChildAt(0).getTop();
int pad =((ListView)childView).getListPaddingTop();
if((Math.abs(top-pad)) < 3&&
((ListView) childView).getFirstVisiblePosition() == 0){
return true;
}else{
return false;
}
}else if(childView instanceof ScrollView){
if(((ScrollView)childView).getScrollY() == 0){
return true;
}else{
return false;
}
} }
return false;
}
/**
* 刷新監聽接口
* @author Nono
*
*/
public interface RefreshListener{
public void onRefresh(RefreshableView view);
}}

安卓中級教程(5):ScrollView與refreshable之間的設置的更多相关文章

  1. 安卓中級教程(3):ScrollView

    以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...

  2. 安卓中級教程(9):pathbutton中的animation.java研究(2)

    src/geniuz/myPathbutton/composerLayout.java package geniuz.myPathbutton; import com.nineoldandroids. ...

  3. 安卓中級教程(1):@InjectView

    package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...

  4. 安卓中級教程(4):ScrollView與ListView之間的高度問題

    在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns: ...

  5. 安卓中級教程(10):@InjectView

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  6. 安卓中級教程(8):pathbutton中的animation.java研究(1)

    src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...

  7. 安卓中級教程(6):annotation的基本用法

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  8. 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  9. 安卓中級教程(7):annotation中的 public @interface的用法

    package com.example.ele_me.util; import java.lang.annotation.Retention; import java.lang.annotation. ...

随机推荐

  1. js简单弹出层、遮罩层

    <html> <head> <title>js简单弹出层</title> <style> /*阴影边框效果*/ .box-shadow-1 ...

  2. .net 根据银行卡获取银行信息

    using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...

  3. Spring Data JPA @EnableJpaRepositories配置详解

    @EnableJpaRepositories注解用于Srping JPA的代码配置,用于取代xml形式的配置文件,@EnableJpaRepositories支持的配置形式丰富多用,本篇文章详细讲解. ...

  4. .NET LINQ Set 运算

    Set 运算      LINQ 中的 Set 操作是指根据相同或不同集合(或集)中是否存在等效元素来生成结果集的查询操作. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表 ...

  5. .NET LINQ 限定符操作

    限定符操作      限定符运算返回一个 Boolean 值,该值指示序列中是否有一些元素满足条件或是否所有元素都满足条件. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表 ...

  6. hdfs的读写数据流

    hdfs的读:      首先客户端通过调用fileSystem对象中的open()函数读取他需要的的数据,fileSystem是DistributedFileSystem的一个实例, Distrib ...

  7. Linux下安装JDK多种方式

    一.环境说明: 操作系统:Linux xx- -.el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Linux jdk版本:java-- ...

  8. jquery简单开始

    老师讲好少,我也没办法. &(function(){ 执行完所有代码之后再执行这里的代码 }) 选择器: &('#id');      获取id &('.class');   ...

  9. one_person年轻的程序员

    回顾大学三年,通过良师的教导和自身的刻苦学习,我已初步掌握如何运用计算机编程,也养成了认真对待学习和工作的好习惯! 在思想品德上,本人有良好道德修养,并有坚定的政治方向.我热爱祖国,热爱人民,遵纪守法 ...

  10. 关于php的一些小知识!

      浏览目录: 一.PHP的背景和优势: 二.PHP原理简介: 三.PHP运行环境配置: 四.编写简单的PHP代码以及测试. 一.PHP的背景和优势 1.1   什么是PHP? PHP是能让你生成动态 ...