Android下拉刷新可扩展实现
2013-06-25 20:45
295 查看
现在几乎所有的主流商业应用都支持某些页面的下拉刷新(如天猫,京东,美团,新浪微博等),下拉刷新的实现其实可以基于一个重写的framelayout来完成,主要就是重写它的scrollTo方法。进行刷新的页面可以扩展到任何ViewGroup。最近在学习下拉刷新的实现,这个demo的UI效果和一些成熟APP是一致的,并有良好的可扩展性。
主要包含两个类,一个类是PullRefreshLayout (extends FrameLayout),详细实现见demo源码。另一个是Activtity实现类,demo中仅仅模拟了网络刷新,但预留了刷新页面接口。代码如下:
package com.banking.pullrefresh;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import com.banking.pullrefresh.PullRefreshLayout.OnPullListener;
import com.banking.pullrefresh.PullRefreshLayout.OnPullStateListener;
public class PullRefreshActivity extends Activity implements OnPullListener, OnPullStateListener{
private final String TAG = "PullRefreshActivity";
private Animation mRotateUpAnimation;
private Animation mRotateDownAnimation;
private boolean mInLoading = false;
private PullRefreshLayout mPullLayout;
private TextView mActionText;
private TextView mTimeText;
private View mProgress;
private View mActionImage;
private Handler mHandler;
private TextView contentTv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initPullRefreshView();
dataLoadFinished();
}
/**
* 下拉刷新view初始化
*/
private void initPullRefreshView() {
mHandler = new Handler();
mRotateUpAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_up);
mRotateDownAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_down);
mInLoading = true;
contentTv = (TextView)findViewById(R.id.content_tv);
mPullLayout = (PullRefreshLayout) findViewById(R.id.pull_container);
mPullLayout.setOnActionPullListener(this);
mPullLayout.setOnPullStateChangeListener(this);
mPullLayout.setEnableStopInActionView(true);
mProgress = findViewById(android.R.id.progress);
mProgress.setVisibility(View.VISIBLE);
mActionImage = findViewById(android.R.id.icon);
mActionImage.setVisibility(View.GONE);
mActionText = (TextView) findViewById(R.id.pull_note);
mActionText.setText("下拉刷新");
contentTv.setText("初始数据.");
mTimeText = (TextView) findViewById(R.id.refresh_time);
// mTimeText.setText(R.string.note_not_update);
}
@Override
public void onPullOut() {
if (!mInLoading) {
mActionText.setText(R.string.note_pull_refresh);
mActionImage.clearAnimation();
mActionImage.startAnimation(mRotateUpAnimation);
}
}
@Override
public void onPullIn() {
if (!mInLoading) {
mActionText.setText(R.string.note_pull_down);
mActionImage.clearAnimation();
mActionImage.startAnimation(mRotateDownAnimation);
}
}
@Override
public void onSnapToTop() {// 下拉后,弹到顶部时,开始刷新数据
if (!mInLoading) {
mInLoading = true;
mPullLayout.setEnableStopInActionView(true);
mActionImage.clearAnimation();
mActionImage.setVisibility(View.GONE);
mProgress.setVisibility(View.VISIBLE);
mActionText.setText(R.string.note_pull_loading);
pullRefreshData();
}
}
@Override
public void onShow() {
// TODO Auto-generated method stub
}
@Override
public void onHide() {
// TODO Auto-generated method stub
}
/**
* 当当前页面其他元素加载完成后调用该方法
*/
private void dataLoadFinished() {
Log.d(TAG, " dataLoaded -->> mInLoading=" + mInLoading);
if (mInLoading) {
mInLoading = false;
mPullLayout.setEnableStopInActionView(false);
mPullLayout.hideActionView();
mActionImage.setVisibility(View.VISIBLE);
mProgress.setVisibility(View.GONE);
if (mPullLayout.isPullOut()) {
mActionText.setText(R.string.note_pull_refresh);
mActionImage.clearAnimation();
mActionImage.startAnimation(mRotateUpAnimation);
} else {
mActionText.setText(R.string.note_pull_down);
}
mTimeText.setText(getString(R.string.note_update_at, formatDate(new Date(System.currentTimeMillis()))));
}
}
/**
* 这里加入相关网络请求
*/
private void pullRefreshData() {
new Thread() {
public void run() {
Log.d("PullRefreshActivity","pullRefreshData-->data fresh");
//TODO 网络请求
//还原控件,更新页面
mHandler.post(new Runnable() {
@Override
public void run() {
contentTv.setText(""+formatDate(new Date(System.currentTimeMillis()))+"时刻数据");
dataLoadFinished();
}
});
};
}.start();
}
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static Date parseDate(String dateStr) throws ParseException {
try {
return dateFormat.parse(dateStr);
} catch (ParseException e) {
throw e;
}
}
public static String formatDate(Date date) {
return dateFormat.format(date);
}
}
demo源码地址:https://github.com/banking/PullRefresh
主要包含两个类,一个类是PullRefreshLayout (extends FrameLayout),详细实现见demo源码。另一个是Activtity实现类,demo中仅仅模拟了网络刷新,但预留了刷新页面接口。代码如下:
package com.banking.pullrefresh;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import com.banking.pullrefresh.PullRefreshLayout.OnPullListener;
import com.banking.pullrefresh.PullRefreshLayout.OnPullStateListener;
public class PullRefreshActivity extends Activity implements OnPullListener, OnPullStateListener{
private final String TAG = "PullRefreshActivity";
private Animation mRotateUpAnimation;
private Animation mRotateDownAnimation;
private boolean mInLoading = false;
private PullRefreshLayout mPullLayout;
private TextView mActionText;
private TextView mTimeText;
private View mProgress;
private View mActionImage;
private Handler mHandler;
private TextView contentTv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initPullRefreshView();
dataLoadFinished();
}
/**
* 下拉刷新view初始化
*/
private void initPullRefreshView() {
mHandler = new Handler();
mRotateUpAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_up);
mRotateDownAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_down);
mInLoading = true;
contentTv = (TextView)findViewById(R.id.content_tv);
mPullLayout = (PullRefreshLayout) findViewById(R.id.pull_container);
mPullLayout.setOnActionPullListener(this);
mPullLayout.setOnPullStateChangeListener(this);
mPullLayout.setEnableStopInActionView(true);
mProgress = findViewById(android.R.id.progress);
mProgress.setVisibility(View.VISIBLE);
mActionImage = findViewById(android.R.id.icon);
mActionImage.setVisibility(View.GONE);
mActionText = (TextView) findViewById(R.id.pull_note);
mActionText.setText("下拉刷新");
contentTv.setText("初始数据.");
mTimeText = (TextView) findViewById(R.id.refresh_time);
// mTimeText.setText(R.string.note_not_update);
}
@Override
public void onPullOut() {
if (!mInLoading) {
mActionText.setText(R.string.note_pull_refresh);
mActionImage.clearAnimation();
mActionImage.startAnimation(mRotateUpAnimation);
}
}
@Override
public void onPullIn() {
if (!mInLoading) {
mActionText.setText(R.string.note_pull_down);
mActionImage.clearAnimation();
mActionImage.startAnimation(mRotateDownAnimation);
}
}
@Override
public void onSnapToTop() {// 下拉后,弹到顶部时,开始刷新数据
if (!mInLoading) {
mInLoading = true;
mPullLayout.setEnableStopInActionView(true);
mActionImage.clearAnimation();
mActionImage.setVisibility(View.GONE);
mProgress.setVisibility(View.VISIBLE);
mActionText.setText(R.string.note_pull_loading);
pullRefreshData();
}
}
@Override
public void onShow() {
// TODO Auto-generated method stub
}
@Override
public void onHide() {
// TODO Auto-generated method stub
}
/**
* 当当前页面其他元素加载完成后调用该方法
*/
private void dataLoadFinished() {
Log.d(TAG, " dataLoaded -->> mInLoading=" + mInLoading);
if (mInLoading) {
mInLoading = false;
mPullLayout.setEnableStopInActionView(false);
mPullLayout.hideActionView();
mActionImage.setVisibility(View.VISIBLE);
mProgress.setVisibility(View.GONE);
if (mPullLayout.isPullOut()) {
mActionText.setText(R.string.note_pull_refresh);
mActionImage.clearAnimation();
mActionImage.startAnimation(mRotateUpAnimation);
} else {
mActionText.setText(R.string.note_pull_down);
}
mTimeText.setText(getString(R.string.note_update_at, formatDate(new Date(System.currentTimeMillis()))));
}
}
/**
* 这里加入相关网络请求
*/
private void pullRefreshData() {
new Thread() {
public void run() {
Log.d("PullRefreshActivity","pullRefreshData-->data fresh");
//TODO 网络请求
//还原控件,更新页面
mHandler.post(new Runnable() {
@Override
public void run() {
contentTv.setText(""+formatDate(new Date(System.currentTimeMillis()))+"时刻数据");
dataLoadFinished();
}
});
};
}.start();
}
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static Date parseDate(String dateStr) throws ParseException {
try {
return dateFormat.parse(dateStr);
} catch (ParseException e) {
throw e;
}
}
public static String formatDate(Date date) {
return dateFormat.format(date);
}
}
demo源码地址:https://github.com/banking/PullRefresh
相关文章推荐
- Android SwipeRefreshLayout{v4包中带的}扩展SwipyRefreshLayout实现下拉刷新,下拉加载
- Android中下拉刷新和加载更多的实现方式
- Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件
- 推荐大家一个好用的实现分页加载和下拉刷新Android工具包
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- android快速开发框架--快速实现 页面 加载中 加载失败 无数据等状态以及下拉刷新和自动加载
- android ListView的上部下拉刷新下部点击加载更多具体实现及拓展
- Android UI--自定义ListView(实现下拉刷新+加载更多)
- Android LinearLayout实现下拉刷新
- Android 下拉刷新框架实现
- Android UI开发第二十一篇——下拉刷新列表实现(pull to refresh)
- Android使用RecyclerView实现上拉加载更多,下拉刷新,分组显示
- Android中ListView下拉刷新的实现
- Android实现RecyclerView的下拉刷新和上拉加载更多
- android 使用PullToRefresh实现下拉刷新和上拉加载更多
- Android 怎么实现newbility的下拉刷新和加载更多的ListView
- Android实现ListView的下拉刷新、上拉加载更多
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- Android中ListView下拉刷新的实现
- Android实现RecyclerView自定义列表、点击事件以及下拉刷新