欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习教程之下拉刷新实现代码(11)

程序员文章站 2024-02-29 11:08:16
本文实例为大家分享了android下拉刷新的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package siso.refres...

本文实例为大家分享了android下拉刷新的具体代码,供大家参考,具体内容如下

mainactivity.java代码:

package siso.refreshablev;

import android.app.activity;
import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.widget.textview;

import refreshableview.refreshableview;
import refreshableview.refreshablehelper.refreshablehelper;

public class mainactivity extends activity implements view.onclicklistener {
 private static final string tag = mainactivity.class.getsimplename();

 private refreshableview refreshableview;

 /**
 * called when the activity is first created.
 */
 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 findviewbyid(r.id.main_tv).setonclicklistener(this);
 refreshableview = (refreshableview) findviewbyid(r.id.main_refresh_view);
 refreshableview.setrefreshablehelper(new refreshablehelper() {

  @override
  public view oninitrefreshheaderview() {
  return layoutinflater.from(mainactivity.this).inflate(r.layout.refresh_head, null);
  }

  @override
  public boolean oninitrefreshheight(int originrefreshheight) {
  refreshableview.setrefreshnormalheight(refreshableview.getoriginrefreshheight() / 3);
  refreshableview.setrefreshingheight(refreshableview.getoriginrefreshheight());
  refreshableview.setrefresharrivedstateheight(refreshableview.getoriginrefreshheight());
  return false;
  }

  @override
  public void onrefreshstatechanged(view refreshview, int refreshstate) {
  textview tv = (textview) refreshview.findviewbyid(r.id.refresh_head_tv);
  switch (refreshstate) {
   case refreshableview.state_refresh_normal:
   tv.settext("正常状态");
   break;
   case refreshableview.state_refresh_not_arrived:
   tv.settext("往下拉可以刷新");
   break;
   case refreshableview.state_refresh_arrived:
   tv.settext("放手可以刷新");
   break;
   case refreshableview.state_refreshing:
   tv.settext("正在刷新");
   new thread(
    new runnable() {
     @override
     public void run() {
     try {
      thread.sleep(1000l);
      runonuithread(new runnable() {
      @override
      public void run() {
       refreshableview.oncompleterefresh();
      }
      });
     } catch (interruptedexception e) {
      log.e(tag, "_", e);
     }
     }
    }
   ).start();
   break;

  }
  }
 });
 }

 @override
 public void onclick(view v) {
 switch (v.getid()) {
  case r.id.main_tv:
  log.d(tag, "content clicked");
  startactivity(new intent(this, refreshablelistactivity.class));
  break;
 }
 }
}

refreshablelistactivity.java代码:

package siso.refreshablev;

import android.app.activity;
import android.os.bundle;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.widget.arrayadapter;
import android.widget.textview;


import java.util.arraylist;
import java.util.list;

import refreshableview.refreshablelistview;
import refreshableview.refreshableview;
import refreshableview.refreshablehelper.refreshablehelper;


public class refreshablelistactivity extends activity {
 private static final string tag = refreshablelistactivity.class.getsimplename();
 refreshablelistview lv;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.refreshable_list);
 lv = (refreshablelistview) findviewbyid(r.id.refreshable_lv);
 lv.setrefreshablehelper(new refreshablehelper() {

  @override
  public view oninitrefreshheaderview() {
  return layoutinflater.from(refreshablelistactivity.this).inflate(r.layout.refresh_head, null);
  }

  @override
  public boolean oninitrefreshheight(int originrefreshheight) {
  lv.setrefreshnormalheight(lv.getoriginrefreshheight() / 3);
  lv.setrefreshingheight(lv.getoriginrefreshheight());
  lv.setrefresharrivedstateheight(lv.getoriginrefreshheight());
  return false;
  }

  @override
  public void onrefreshstatechanged(view refreshview, int refreshstate) {
  textview tv = (textview) refreshview.findviewbyid(r.id.refresh_head_tv);
  switch (refreshstate) {
   case refreshableview.state_refresh_normal:
   tv.settext("正常状态");
   break;
   case refreshableview.state_refresh_not_arrived:
   tv.settext("往下拉可以刷新");
   break;
   case refreshableview.state_refresh_arrived:
   tv.settext("放手可以刷新");
   break;
   case refreshableview.state_refreshing:
   tv.settext("正在刷新");
   new thread(
    new runnable() {
     @override
     public void run() {
     try {
      thread.sleep(1000l);
      runonuithread(new runnable() {
      @override
      public void run() {
       lv.oncompleterefresh();
      }
      });
     } catch (interruptedexception e) {
      log.e(tag, "_", e);
     }
     }
    }
   ).start();
   break;

  }
  }
 });

 list<string> data = new arraylist<string>();
 for (int i = 0; i < 30; i++) {
  data.add("item_" + i);
 }
 arrayadapter adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, data);
 lv.setadapter(adapter);
 adapter.notifydatasetchanged();

 }


}

nestscrollview.java代码:

package refreshableview;

import android.content.context;
import android.util.attributeset;
import android.util.log;
import android.view.motionevent;
import android.view.viewparent;
import android.widget.scrollview;


public class nestscrollview extends scrollview {
 private static final string tag = nestscrollview.class.getsimplename();

 public nestscrollview(context context) {
 super(context);
 }

 public nestscrollview(context context, attributeset attrs) {
 super(context, attrs);
 }

 public nestscrollview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 }

 @override
 public boolean dispatchtouchevent(motionevent ev) {
 log.d(tag, "___[dispatchtouchevent]ev action: " + ev.getaction());
 return super.dispatchtouchevent(ev);
 }

 @override
 public boolean onintercepttouchevent(motionevent ev) {
 super.onintercepttouchevent(ev);
 log.d(tag, "___[onintercepttouchevent]ev action: " + ev.getaction());
 if (motionevent.action_move == ev.getaction()) {
  return true;
 }
 return false;
 }

 float lastdowny;

 @override
 public boolean ontouchevent(motionevent event) {
 super.ontouchevent(event);
 switch (event.getaction()) {
  case motionevent.action_down:
  lastdowny = event.gety();
  parentrequestdisallowintercepttouchevent(true); // 保证事件可往下传递
  log.d(tag, "___down");
  return true;
//  break;
  case motionevent.action_move:
  log.d(tag, "___move, this.getscrolly(): " + this.getscrolly());
  boolean istop = event.gety() - lastdowny > 0 && this.getscrolly() == 0;
  if (istop) { // 允许父控件拦截,即不允许父控件拦截设为false
   parentrequestdisallowintercepttouchevent(false);
   return false;
  } else { // 不允许父控件拦截,即不允许父控件拦截设为true
   parentrequestdisallowintercepttouchevent(true);
   return true;
  }
//  break;
  case motionevent.action_up:
  log.d(tag, "___up, this.getscrolly(): " + this.getscrolly());
  parentrequestdisallowintercepttouchevent(true); // 保证事件可往下传递
  break;
  case motionevent.action_cancel:
  log.d(tag, "___cancel");
  break;
 }
 return false;
 }

 /**
 * 是否允许父控件拦截事件
 * @param disallowintercept
 */
 private void parentrequestdisallowintercepttouchevent(boolean disallowintercept) {
 viewparent vp = getparent();
 if (null == vp) {
  return;
 }
 vp.requestdisallowintercepttouchevent(disallowintercept);
 }

}

refreshablelistview.java代码:

package refreshableview;

import android.animation.animator;
import android.animation.animatorlisteneradapter;
import android.animation.valueanimator;
import android.content.context;
import android.util.attributeset;
import android.util.log;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.view.animation.linearinterpolator;
import android.widget.listadapter;
import android.widget.listview;

import refreshableview.refreshablehelper.refreshablehelper;


public class refreshablelistview extends listview {
 private static final string tag = refreshablelistview.class.getsimplename();

 public refreshablelistview(context context) {
 super(context);
 init(context);
 }

 public refreshablelistview(context context, attributeset attrs) {
 super(context, attrs);
 init(context);
 }

 public refreshablelistview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 init(context);
 }

 /**
 * 刷新状态
 */
 public static final int state_refresh_normal = 0x21;
 public static final int state_refresh_not_arrived = 0x22;
 public static final int state_refresh_arrived = 0x23;
 public static final int state_refreshing = 0x24;
 private int refreshstate;
 // 刷新状态监听
 private refreshablehelper refreshablehelper;
 /**
 * 刷新的view
 */
 private view refreshheaderview;
 /**
 * 刷新的view的真实高度
 */
 private int originrefreshheight;
 /**
 * 有效下拉刷新需要达到的高度
 */
 private int refresharrivedstateheight;
 /**
 * 刷新时显示的高度
 */
 private int refreshingheight;
 /**
 * 正常未刷新高度
 */
 private int refreshnormalheight;

 public void setrefreshablehelper(refreshablehelper refreshablehelper) {
 this.refreshablehelper = refreshablehelper;
 }

 private context context;

 private void init(context context) {
 this.context = context;

 }

 @override
 protected void onsizechanged(int w, int h, int oldw, int oldh) {
 super.onsizechanged(w, h, oldw, oldh);
 log.d(tag, "[onsizechanged]w: " + w + ", h: " + h);
 log.d(tag, "[onsizechanged]oldw: " + oldw + ", oldh: " + oldh);
 log.d(tag, "[onsizechanged]child counts: " + this.getchildcount());

 }

 @override
 public void setadapter(listadapter adapter) {
 if (null == refreshheaderview) {
  addheaderview();
 }
 super.setadapter(adapter);
 }

 private void addheaderview() {
 if (null != refreshablehelper) {
  refreshheaderview = refreshablehelper.oninitrefreshheaderview();
 }
 if (null == refreshheaderview) {
  log.e(tag, "refreshheaderview is null!");
  return;
 }
 this.addheaderview(refreshheaderview);
 // 计算refreshheadview尺寸
 int width = measurespec.makemeasurespec(0, measurespec.unspecified);
 int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most);
 refreshheaderview.measure(width, expandspec);

 originrefreshheight = refreshheaderview.getmeasuredheight();

 boolean isusedefault = true;
 if (null != refreshablehelper) {
  isusedefault = refreshablehelper.oninitrefreshheight(originrefreshheight);
 }

 // 初始化各个高度
 if (isusedefault) {
  refresharrivedstateheight = originrefreshheight;
  refreshingheight = originrefreshheight;
  refreshnormalheight = 0;
 }
 log.d(tag, "[onsizechanged]refreshheaderview origin height: " + originrefreshheight);
 changeviewheight(refreshheaderview, refreshnormalheight);

 // 初始化为正常状态
 setrefreshstate(state_refresh_normal);
 }

 @override
 public boolean dispatchtouchevent(motionevent ev) {
 super.dispatchtouchevent(ev);
// if(motionevent.action_move == ev.getaction()){
//  return true;
// }
// return false;
 return true;
 }

 private float lastdowny = float.max_value;

 @override
 public boolean ontouchevent(motionevent ev) {
// super.ontouchevent(ev);
 log.d(tag, "[ontouchevent]ev action: " + ev.getaction());
 switch (ev.getaction()) {
  case motionevent.action_down:
  super.ontouchevent(ev);
  lastdowny = ev.gety();
  log.d(tag, "down --> lastdowny: " + lastdowny);
  break;
  case motionevent.action_move:
  float cury = ev.gety();
  float deltay = cury - lastdowny;
  log.d(tag, "move --> deltay(cury - downy): " + deltay);

  int curheight = refreshheaderview.getmeasuredheight();
  int exceptheight = curheight + (int) (deltay / 2);

//  boolean istop = this.getscrolly() == 0;
  boolean istop = 0 == this.getchildat(0).gettop();
  boolean isdropdown = cury - lastdowny > 0;
  log.d(tag, "istop: " + istop + ", gettop(): " + this.getchildat(0).gettop());
  if (istop) {
   if (refreshstate == state_refresh_normal && !isdropdown) { // 正常状态,手指往上(列表往下滚动)
   super.ontouchevent(ev);
   } else {
   // 如果当前没有处在正在刷新状态,则更新刷新状态
   if (state_refreshing != refreshstate) {
    if (curheight >= refresharrivedstateheight) { // 达到可刷新状态
    setrefreshstate(state_refresh_arrived);
    } else if (curheight == refreshnormalheight) { // 正常状态
    setrefreshstate(state_refresh_normal);
    } else { // 未达到可刷新状态
    setrefreshstate(state_refresh_not_arrived);
    }
    changeviewheight(refreshheaderview, math.max(refreshnormalheight, exceptheight));
   } else {
    super.ontouchevent(ev);
   }

   }

  } else {
   super.ontouchevent(ev);
  }
  lastdowny = cury;
  break;
  case motionevent.action_up:
  super.ontouchevent(ev);
  lastdowny = float.max_value;
  log.d(tag, "up --> lastdowny: " + lastdowny);
  // 如果是达到刷新状态,则设置正在刷新状态的高度
  if (state_refresh_arrived == refreshstate) { // 达到了刷新的状态
   startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshingheight);
   setrefreshstate(state_refreshing);
  } else if (state_refreshing == refreshstate) { // 正在刷新的状态
   startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshingheight);
  } else {
   // 执行动画后回归正常状态
   startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshnormalheight, normalanimatorlistener);
  }
  break;
 }
 return true;
 }


 /**
 * 刷新完毕后调用此方法
 */
 public void oncompleterefresh() {
 if (state_refreshing == refreshstate) {
  setrefreshstate(state_refresh_normal);
  startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshnormalheight);
 }
 }

 /**
 * 修改当前的刷新状态
 *
 * @param expectrefreshstate
 */
 private void setrefreshstate(int expectrefreshstate) {
 if (expectrefreshstate != refreshstate) {
  refreshstate = expectrefreshstate;
  if (null != refreshablehelper) {
  refreshablehelper.onrefreshstatechanged(refreshheaderview, refreshstate);
  }
 }
 }


 /**
 * 改变某控件的高度
 *
 * @param view
 * @param height
 */
 private void changeviewheight(view view, int height) {
 log.d(tag, "[changeviewheight]change height: " + height);
 viewgroup.layoutparams lp = view.getlayoutparams();
 if (null == lp) {
  lp = new layoutparams(viewgroup.layoutparams.match_parent, 0);
 }
 lp.height = height;
 view.setlayoutparams(lp);
 }

 /**
 * 改变某控件的高度动画
 *
 * @param view
 * @param fromheight
 * @param toheight
 */
 private void startheightanimation(final view view, int fromheight, int toheight) {
 startheightanimation(view, fromheight, toheight, null);
 }

 private void startheightanimation(final view view, int fromheight, int toheight, animator.animatorlistener animatorlistener) {
 if (toheight == view.getmeasuredheight()) {
  return;
 }
 valueanimator heightanimator = valueanimator.ofint(fromheight, toheight);
 heightanimator.addupdatelistener(new valueanimator.animatorupdatelistener() {
  @override
  public void onanimationupdate(valueanimator valueanimator) {
  integer value = (integer) valueanimator.getanimatedvalue();
  if (null == value) return;
  changeviewheight(view, value);
  }
 });
 if (null != animatorlistener) {
  heightanimator.addlistener(animatorlistener);
 }
 heightanimator.setinterpolator(new linearinterpolator());
 heightanimator.setduration(300/*ms*/);
 heightanimator.start();
 }

 animatorlisteneradapter normalanimatorlistener = new animatorlisteneradapter() {
 @override
 public void onanimationend(animator animation) {
  super.onanimationend(animation);
  setrefreshstate(state_refresh_normal); // 回归正常状态
 }
 };

 public void setrefresharrivedstateheight(int refresharrivedstateheight) {
 this.refresharrivedstateheight = refresharrivedstateheight;
 }

 public void setrefreshingheight(int refreshingheight) {
 this.refreshingheight = refreshingheight;
 }

 public void setrefreshnormalheight(int refreshnormalheight) {
 this.refreshnormalheight = refreshnormalheight;
 }

 public int getoriginrefreshheight() {
 return originrefreshheight;
 }

}

refreshableview.java代码:

package refreshableview;

import android.animation.animator;
import android.animation.animatorlisteneradapter;
import android.animation.valueanimator;
import android.content.context;
import android.content.res.typedarray;
import android.util.attributeset;
import android.util.log;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.view.animation.linearinterpolator;
import android.widget.linearlayout;

import refreshableview.refreshablehelper.refreshablehelper;
import siso.refreshablev.r;

public class refreshableview extends linearlayout {
 private static final string tag = refreshableview.class.getsimplename();

 public refreshableview(context context) {
 super(context);
 init(context);
 }

 public refreshableview(context context, attributeset attrs) {
 super(context, attrs);

 typedarray a = context.obtainstyledattributes(attrs, r.styleable.refreshableview);
 for (int i = 0, len = a.length(); i < len; i++) {
  int attrindex = a.getindex(i);
  switch (attrindex) {
  case r.styleable.refreshableview_interceptallmoveevents:
   interceptallmoveevents = a.getboolean(i, false);
   break;
  }
 }
 a.recycle();

 init(context);
 }

 public refreshableview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 init(context);
 }

 /**
 * 刷新状态
 */
 public static final int state_refresh_normal = 0x21;
 public static final int state_refresh_not_arrived = 0x22;
 public static final int state_refresh_arrived = 0x23;
 public static final int state_refreshing = 0x24;
 private int refreshstate;
 // 刷新状态监听
 private refreshablehelper refreshablehelper;

 public void setrefreshablehelper(refreshablehelper refreshablehelper) {
 this.refreshablehelper = refreshablehelper;
 }

 private context context;
 /**
 * 刷新的view
 */
 private view refreshheaderview;
 /**
 * 刷新的view的真实高度
 */
 private int originrefreshheight;
 /**
 * 有效下拉刷新需要达到的高度
 */
 private int refresharrivedstateheight;
 /**
 * 刷新时显示的高度
 */
 private int refreshingheight;
 /**
 * 正常未刷新高度
 */
 private int refreshnormalheight;


 /**
 * 默认不允许拦截(即,往子view传递事件),该属性只有在interceptallmoveevents为false的时候才有效
 */
 private boolean disallowintercept = true;
 /**
 * xml中可设置它的值为false,表示不把移动的事件传递给子控件
 */
 private boolean interceptallmoveevents;

 private void init(context context) {
 this.context = context;
 this.setorientation(vertical);

// log.d(tag, "[init]originrefreshheight: " + refreshheaderview.getmeasuredheight());
 }

 @override
 protected void onsizechanged(int w, int h, int oldw, int oldh) {
 super.onsizechanged(w, h, oldw, oldh);

 if (null != refreshablehelper) {
  refreshheaderview = refreshablehelper.oninitrefreshheaderview();
 }
// refreshheaderview = layoutinflater.from(context).inflate(r.layout.refresh_head, null);
 if (null == refreshheaderview) {
  log.e(tag, "refreshheaderview is null!");
  return;
 }
 this.removeview(refreshheaderview);
 this.addview(refreshheaderview, 0);

 // 计算refreshheadview尺寸
 int width = measurespec.makemeasurespec(0, measurespec.unspecified);
 int expandspec = measurespec.makemeasurespec(integer.max_value >> 2, measurespec.at_most);
 refreshheaderview.measure(width, expandspec);

 log.d(tag, "[onsizechanged]w: " + w + ", h: " + h);
 log.d(tag, "[onsizechanged]oldw: " + oldw + ", oldh: " + oldh);
 log.d(tag, "[onsizechanged]child counts: " + this.getchildcount());
 originrefreshheight = refreshheaderview.getmeasuredheight();

 boolean isusedefault = true;
 if (null != refreshablehelper) {
  isusedefault = refreshablehelper.oninitrefreshheight(originrefreshheight);
 }

 // 初始化各个高度
 if (isusedefault) {
  refresharrivedstateheight = originrefreshheight;
  refreshingheight = originrefreshheight;
  refreshnormalheight = 0;
 }
 log.d(tag, "[onsizechanged]refreshheaderview origin height: " + originrefreshheight);
 changeviewheight(refreshheaderview, refreshnormalheight);

 // 初始化为正常状态
 setrefreshstate(state_refresh_normal);

 }

 @override
 public boolean dispatchtouchevent(motionevent ev) {
 log.d(tag, "[dispatchtouchevent]ev action: " + ev.getaction());
 return super.dispatchtouchevent(ev);
 }

 @override
 public boolean onintercepttouchevent(motionevent ev) {
 log.d(tag, "[onintercepttouchevent]ev action: " + ev.getaction());
 if (!interceptallmoveevents) {
  return !disallowintercept;
 }
 // 如果设置了拦截所有move事件,即interceptallmoveevents为true
 if (motionevent.action_move == ev.getaction()) {
  return true;
 }
 return false;
 }

 @override
 public void requestdisallowintercepttouchevent(boolean disallowintercept) {
 if (this.disallowintercept == disallowintercept) {
  return;
 }
 this.disallowintercept = disallowintercept;
 super.requestdisallowintercepttouchevent(disallowintercept);
 }

 private float downy = float.max_value;

 @override
 public boolean ontouchevent(motionevent event) {
// super.ontouchevent(event);
 log.d(tag, "[ontouchevent]ev action: " + event.getaction());
 switch (event.getaction()) {
  case motionevent.action_down:
  downy = event.gety();
  log.d(tag, "down --> downy: " + downy);
  requestdisallowintercepttouchevent(true); // 保证事件可往下传递
  break;
  case motionevent.action_move:

  float cury = event.gety();
  float deltay = cury - downy;
  // 是否是有效的往下拖动事件(则需要显示加载header)
  boolean isdropdownvalidate = float.max_value != downy;
  /**
   * 修改拦截设置
   * 如果是有效往下拖动事件,则事件需要在本viewgroup中处理,所以需要拦截不往子控件传递,即不允许拦截设为false
   * 如果不是有效往下拖动事件,则事件传递给子控件处理,所以不需要拦截,并往子控件传递,即不允许拦截设为true
   */
  requestdisallowintercepttouchevent(!isdropdownvalidate);

  downy = cury;
  log.d(tag, "move --> deltay(cury - downy): " + deltay);
  int curheight = refreshheaderview.getmeasuredheight();
  int exceptheight = curheight + (int) (deltay / 2);

  // 如果当前没有处在正在刷新状态,则更新刷新状态
  if (state_refreshing != refreshstate) {
   if (curheight >= refresharrivedstateheight) { // 达到可刷新状态
   setrefreshstate(state_refresh_arrived);
   } else { // 未达到可刷新状态
   setrefreshstate(state_refresh_not_arrived);
   }
  }
  if (isdropdownvalidate) {
   changeviewheight(refreshheaderview, math.max(refreshnormalheight, exceptheight));
  } else { // 防止从子控件修改拦截后引发的downy为float.max_value的问题
   changeviewheight(refreshheaderview, math.max(curheight, exceptheight));
  }

  break;
  case motionevent.action_up:
  downy = float.max_value;
  log.d(tag, "up --> downy: " + downy);
  requestdisallowintercepttouchevent(true); // 保证事件可往下传递
  // 如果是达到刷新状态,则设置正在刷新状态的高度
  if (state_refresh_arrived == refreshstate) { // 达到了刷新的状态
   startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshingheight);
   setrefreshstate(state_refreshing);
  } else if (state_refreshing == refreshstate) { // 正在刷新的状态
   startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshingheight);
  } else {
   // 执行动画后回归正常状态
   startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshnormalheight, normalanimatorlistener);
  }

  break;
  case motionevent.action_cancel:
  log.d(tag, "cancel");
  break;
 }
 return true;
 }

 /**
 * 刷新完毕后调用此方法
 */
 public void oncompleterefresh() {
 if (state_refreshing == refreshstate) {
  setrefreshstate(state_refresh_normal);
  startheightanimation(refreshheaderview, refreshheaderview.getmeasuredheight(), refreshnormalheight);
 }
 }

 /**
 * 修改当前的刷新状态
 *
 * @param expectrefreshstate
 */
 private void setrefreshstate(int expectrefreshstate) {
 if (expectrefreshstate != refreshstate) {
  refreshstate = expectrefreshstate;
  if (null != refreshablehelper) {
  refreshablehelper.onrefreshstatechanged(refreshheaderview, refreshstate);
  }
 }
 }


 /**
 * 改变某控件的高度
 *
 * @param view
 * @param height
 */
 private void changeviewheight(view view, int height) {
 log.d(tag, "[changeviewheight]change height: " + height);
 viewgroup.layoutparams lp = view.getlayoutparams();
 lp.height = height;
 view.setlayoutparams(lp);
 }

 /**
 * 改变某控件的高度动画
 *
 * @param view
 * @param fromheight
 * @param toheight
 */
 private void startheightanimation(final view view, int fromheight, int toheight) {
 startheightanimation(view, fromheight, toheight, null);
 }

 private void startheightanimation(final view view, int fromheight, int toheight, animator.animatorlistener animatorlistener) {
 if (toheight == view.getmeasuredheight()) {
  return;
 }
 valueanimator heightanimator = valueanimator.ofint(fromheight, toheight);
 heightanimator.addupdatelistener(new valueanimator.animatorupdatelistener() {
  @override
  public void onanimationupdate(valueanimator valueanimator) {
  integer value = (integer) valueanimator.getanimatedvalue();
  if (null == value) return;
  changeviewheight(view, value);
  }
 });
 if (null != animatorlistener) {
  heightanimator.addlistener(animatorlistener);
 }
 heightanimator.setinterpolator(new linearinterpolator());
 heightanimator.setduration(300/*ms*/);
 heightanimator.start();
 }

 animatorlisteneradapter normalanimatorlistener = new animatorlisteneradapter() {
 @override
 public void onanimationend(animator animation) {
  super.onanimationend(animation);
  setrefreshstate(state_refresh_normal); // 回归正常状态
 }
 };

 public void setrefresharrivedstateheight(int refresharrivedstateheight) {
 this.refresharrivedstateheight = refresharrivedstateheight;
 }

 public void setrefreshingheight(int refreshingheight) {
 this.refreshingheight = refreshingheight;
 }

 public void setrefreshnormalheight(int refreshnormalheight) {
 this.refreshnormalheight = refreshnormalheight;
 }

 public int getoriginrefreshheight() {
 return originrefreshheight;
 }
}

refreshablehelper.java代码:

package refreshableview.refreshablehelper;

import android.view.view;


public interface refreshablehelper {
 /**
 * 初始化刷新view
 *
 * @return
 */
 view oninitrefreshheaderview();

 /**
 * 初始化各个尺寸高度
 *
 * @param originrefreshheight
 * @return
 */
 boolean oninitrefreshheight(int originrefreshheight);

 /**
 * 刷新状态改变
 *
 * @param refreshview
 * @param refreshstate
 */
 void onrefreshstatechanged(view refreshview, int refreshstate);
}

main.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:rv="http://schemas.android.com/tools"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <refreshableview.refreshableview
  android:id="@+id/main_refresh_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  rv:interceptallmoveevents="false"
  >
 <refreshableview.nestscrollview android:layout_width="match_parent" android:layout_height="wrap_content"
  android:fillviewport="true"
  >

  <textview
   android:id="@+id/main_tv"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:padding="20dp"
   android:textsize="18sp"
   android:text="下拉刷新\n\n\n\n\n\n\n\n\n\n\n下拉刷新\n下拉刷新\n\n\n\n\n\n\n\n\n\n\n下拉刷新\n下拉刷新\n\n\n\n\n\n\n\n\n\n\n下拉刷新\n下拉刷新\n\n\n\n\n\n\n\n\n\n\n下拉刷新---下拉刷新\n\n\n\n\n\n\n\n\n\n\n"
   />

 </refreshableview.nestscrollview>
 </refreshableview.refreshableview>

</linearlayout>

activity_refreshable_list.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingbottom="@dimen/activity_vertical_margin"
 android:paddingleft="@dimen/activity_horizontal_margin"
 android:paddingright="@dimen/activity_horizontal_margin"
 android:paddingtop="@dimen/activity_vertical_margin"
 tools:context="siso.refreshablev.refreshablelistactivity">

</relativelayout>

refresh_head.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="70dp"
  android:gravity="center"
  android:background="#aa000000"
 >
 <textview
  android:id="@+id/refresh_head_tv"
  android:layout_width="fill_parent"
  android:layout_height="70dp"
  android:gravity="center"
  android:background="#aabbcc"
  android:textsize="18sp"
  android:text="refreshing"
  />

</linearlayout>

refreshable_list.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


 <refreshableview.refreshablelistview
  android:id="@+id/refreshable_lv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:cachecolorhint="@android:color/transparent"
  />

</linearlayout>

运行结果如图:

Android学习教程之下拉刷新实现代码(11)

Android学习教程之下拉刷新实现代码(11)

Android学习教程之下拉刷新实现代码(11)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。