<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dip"
android:paddingBottom="15dip"
android:gravity="center"
android:id="@+id/pull_to_refresh_header"
>
<ProgressBar
android:id="@+id/pull_to_refresh_progress"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="20dip"
android:layout_marginTop="10dip"
android:visibility="gone"
android:layout_centerVertical="true"
style="?android:attr/progressBarStyleSmall"
/>
<ImageView
android:id="@+id/pull_to_refresh_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="20dip"
android:visibility="gone"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/ic_pulltorefresh_arrow"
/>
<TextView
android:id="@+id/pull_to_refresh_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:paddingTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
/>
<TextView
android:id="@+id/pull_to_refresh_updated_at"
android:layout_below="@+id/pull_to_refresh_text"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
/>
</RelativeLayout>
package com.notice.pullrefresh;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class PullToRefreshListView extends ListView implements OnScrollListener {
// 状态
private static final int TAP_TO_REFRESH = 1;
private static final int PULL_TO_REFRESH = 2;
private static final int RELEASE_TO_REFRESH = 3;
private static final int REFRESHING = 4;
private OnRefreshListener mOnRefreshListener;
// 监听对listview的滑动动作
private OnScrollListener mOnScrollListener;
private LayoutInflater mInflater;
//顶部刷新时出现的控件
private RelativeLayout mRefreshView;
private TextView mRefreshViewText;
private ImageView mRefreshViewImage;
private ProgressBar mRefreshViewProgress;
private TextView mRefreshViewLastUpdated;
// 当前滑动状态
private int mCurrentScrollState;
// 当前刷新状态
private int mRefreshState;
// 箭头动画效果
private RotateAnimation mFlipAnimation;
private RotateAnimation mReverseFlipAnimation;
private int mRefreshViewHeight;
private int mRefreshOriginalTopPadding;
private int mLastMotionY;
private boolean mBounceHack;
public PullToRefreshListView(Context context) {
super(context);
init(context);
}
public PullToRefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public PullToRefreshListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
/**
* 初始化控件和箭头动画(这里直接在代码中初始化动画而不是通过xml)
*/
private void init(Context context) {
mFlipAnimation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
mFlipAnimation.setInterpolator(new LinearInterpolator());
mFlipAnimation.setDuration(250);
mFlipAnimation.setFillAfter(true);
mReverseFlipAnimation = new RotateAnimation(-180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
mReverseFlipAnimation.setDuration(250);
mReverseFlipAnimation.setFillAfter(true);
mInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mRefreshView = (RelativeLayout) mInflater.inflate(
R.layout.pull_to_refresh_header, this, false);
mRefreshViewText =
(TextView) mRefreshView.findViewById(R.id.pull_to_refresh_text);
mRefreshViewImage =
(ImageView) mRefreshView.findViewById(R.id.pull_to_refresh_image);
mRefreshViewProgress =
(ProgressBar) mRefreshView.findViewById(R.id.pull_to_refresh_progress);
mRefreshViewLastUpdated =
(TextView) mRefreshView.findViewById(R.id.pull_to_refresh_updated_at);
mRefreshViewImage.setMinimumHeight(50);
mRefreshOriginalTopPadding = mRefreshView.getPaddingTop();
mRefreshState = TAP_TO_REFRESH;
//为listview头部增加一个view
addHeaderView(mRefreshView);
super.setOnScrollListener(this);
measureView(mRefreshView);
mRefreshViewHeight = mRefreshView.getMeasuredHeight();
}
@Override
protected void onAttachedToWindow() {
setSelection(1);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
setSelection(1);
}
/**
* 设置滑动监听器
*
*/
@Override
public void setOnScrollListener(AbsListView.OnScrollListener l) {
mOnScrollListener = l;
}
/**
* 注册一个list需要刷新时的回调接口
*
*/
public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
mOnRefreshListener = onRefreshListener;
}
/**
* 设置标签显示何时最后被刷新
*
* @param lastUpdated
* Last updated at.
*/
public void setLastUpdated(CharSequence lastUpdated) {
if (lastUpdated != null) {
mRefreshViewLastUpdated.setVisibility(View.VISIBLE);
mRefreshViewLastUpdated.setText(lastUpdated);
} else {
mRefreshViewLastUpdated.setVisibility(View.GONE);
}
}
// 实现该方法处理触摸
@Override
public boolean onTouchEvent(MotionEvent event) {
final int y = (int) event.getY();
mBounceHack = false;
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!isVerticalScrollBarEnabled()) {
setVerticalScrollBarEnabled(true);
}
if (getFirstVisiblePosition() == 0 && mRefreshState != REFRESHING) {
// 拖动距离达到刷新需要
if ((mRefreshView.getBottom() >= mRefreshViewHeight
|| mRefreshView.getTop() >= 0)
&& mRefreshState == RELEASE_TO_REFRESH) {
// 把状态设置为正在刷新
mRefreshState = REFRESHING;
// 准备刷新
prepareForRefresh();
// 刷新
onRefresh();
} else if (mRefreshView.getBottom() < mRefreshViewHeight
|| mRefreshView.getTop() <= 0) {
// 中止刷新
resetHeader();
setSelection(1);
}
}
break;
case MotionEvent.ACTION_DOWN:
// 获得按下y轴位置
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
// 计算边距
applyHeaderPadding(event);
break;
}
return super.onTouchEvent(event);
}
// 获得header的边距
private void applyHeaderPadding(MotionEvent ev) {
int pointerCount = ev.getHistorySize();
for (int p = 0; p < pointerCount; p++) {
if (mRefreshState == RELEASE_TO_REFRESH) {
if (isVerticalFadingEdgeEnabled()) {
setVerticalScrollBarEnabled(false);
}
int historicalY = (int) ev.getHistoricalY(p);
// 计算申请的边距,除以1.7使得拉动效果更好
int topPadding = (int) (((historicalY - mLastMotionY)
- mRefreshViewHeight) / 1.7);
mRefreshView.setPadding(
mRefreshView.getPaddingLeft(),
topPadding,
mRefreshView.getPaddingRight(),
mRefreshView.getPaddingBottom());
}
}
}
/**
* 将head的边距重置为初始的数值
*/
private void resetHeaderPadding() {
mRefreshView.setPadding(
mRefreshView.getPaddingLeft(),
mRefreshOriginalTopPadding,
mRefreshView.getPaddingRight(),
mRefreshView.getPaddingBottom());
}
/**
* 重置header为之前的状态
*/
private void resetHeader() {
if (mRefreshState != TAP_TO_REFRESH) {
mRefreshState = TAP_TO_REFRESH;
resetHeaderPadding();
// 将刷新图标换成箭头
mRefreshViewImage.setImageResource(R.drawable.ic_pulltorefresh_arrow);
// 清除动画
mRefreshViewImage.clearAnimation();
// 隐藏图标和进度条
mRefreshViewImage.setVisibility(View.GONE);
mRefreshViewProgress.setVisibility(View.GONE);
}
}
// 估算headview的width和height
private void measureView(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0,
0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// 在refreshview完全可见时,设置文字为松开刷新,同时翻转箭头
if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
&& mRefreshState != REFRESHING) {
if (firstVisibleItem == 0) {
mRefreshViewImage.setVisibility(View.VISIBLE);
if ((mRefreshView.getBottom() >= mRefreshViewHeight + 20
|| mRefreshView.getTop() >= 0)
&& mRefreshState != RELEASE_TO_REFRESH) {
mRefreshViewText.setText("松开加载...");
mRefreshViewImage.clearAnimation();
mRefreshViewImage.startAnimation(mFlipAnimation);
mRefreshState = RELEASE_TO_REFRESH;
} else if (mRefreshView.getBottom() < mRefreshViewHeight + 20
&& mRefreshState != PULL_TO_REFRESH) {
mRefreshViewText.setText("下拉刷新...");
if (mRefreshState != TAP_TO_REFRESH) {
mRefreshViewImage.clearAnimation();
mRefreshViewImage.startAnimation(mReverseFlipAnimation);
}
mRefreshState = PULL_TO_REFRESH;
}
} else {
mRefreshViewImage.setVisibility(View.GONE);
resetHeader();
}
} else if (mCurrentScrollState == SCROLL_STATE_FLING
&& firstVisibleItem == 0
&& mRefreshState != REFRESHING) {
setSelection(1);
mBounceHack = true;
} else if (mBounceHack && mCurrentScrollState == SCROLL_STATE_FLING) {
setSelection(1);
}
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(view, firstVisibleItem,
visibleItemCount, totalItemCount);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
mCurrentScrollState = scrollState;
if (mCurrentScrollState == SCROLL_STATE_IDLE) {
mBounceHack = false;
}
if (mOnScrollListener != null) {
mOnScrollListener.onScrollStateChanged(view, scrollState);
}
}
public void prepareForRefresh() {
resetHeaderPadding();// 恢复header的边距
mRefreshViewImage.setVisibility(View.GONE);
// 注意加上,否则仍然显示之前的图片
mRefreshViewImage.setImageDrawable(null);
mRefreshViewProgress.setVisibility(View.VISIBLE);
// 设置文字
mRefreshViewText.setText("加载中...");
mRefreshState = REFRESHING;
}
public void onRefresh() {
if (mOnRefreshListener != null) {
mOnRefreshListener.onRefresh();
}
}
/**
* 重置listview为普通的listview,该方法设置最后更新时间
*
* @param lastUpdated
* Last updated at.
*/
public void onRefreshComplete(CharSequence lastUpdated) {
setLastUpdated(lastUpdated);
onRefreshComplete();
}
/**
* 重置listview为普通的listview,不设置最后更新时间
*/
public void onRefreshComplete() {
resetHeader();
// 如果refreshview在加载结束后可见,下滑到下一个条目
if (mRefreshView.getBottom() > 0) {
invalidateViews();
setSelection(1);
}
}
/**
* 刷新监听器接口
*/
public interface OnRefreshListener {
/**
* list需要被刷新时调用
*/
public void onRefresh();
}
}
package com.notice.pullrefresh;
import java.util.Arrays;
import java.util.LinkedList;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.notice.pullrefresh.PullToRefreshListView.OnRefreshListener;
public class PullrefreshActivity extends ListActivity {
private LinkedList<String> mListItems;
ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh);
// list需要刷新时调用
((PullToRefreshListView) getListView())
.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// 在这执行后台工作
new GetDataTask().execute();
}
});
mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListItems);
setListAdapter(adapter);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
// 在这里可以做一些后台工作
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
// 下拉后增加的内容
mListItems.addFirst("Added after refresh...");
// 刷新完成调用该方法复位
((PullToRefreshListView) getListView()).onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings = { "normal data1", "normal data2",
"nomal data3", "normal data4", "norma data5", "normal data6" };
}
相关推荐
Android UI 高级之实现 ListView 的下拉加载 Android UI 高级之实现 ListView 的下拉加载是 Android 应用的一个常见功能,能够实现 ListView 的下拉刷新、分级显示、分页列表、逐页加载等功能。本文将介绍如何实现 ...
在Android开发中,实现ListView的下拉加载功能是一种常见的用户交互方式,它能够提高用户体验,使得内容的获取更加直观和便捷。本篇内容将详细介绍如何实现ListView的下拉刷新功能,以及涉及的相关技术点。 首先,...
通过分析这个"android-下拉加载更多demo",开发者可以学习到如何结合Android的UI组件和网络请求来实现这个功能。此外,这个示例还可能包含了错误处理、数据缓存等进阶技巧,这些都是Android应用开发中不可或缺的知识...
这篇"Android高手进阶教程"涵盖了多个核心主题,旨在帮助开发者从基础知识跃升至更高级的实践应用。以下是一些主要知识点的详细说明: 1. **Android常用命令**: - `android`:SDK和AVD管理器,用于管理Android...
`ListViewDemo`项目可能包含了实现自定义下拉刷新和加载更多的示例代码。在项目中,你应该能找到以下关键组件: - 一个自定义的SwipeRefreshLayout子类,可能包含自定义动画。 - 一个自定义的ListView Adapter,用于...
7. **下拉刷新与上拉加载**:ListView常与SwipeRefreshLayout结合,实现下拉刷新数据;当用户滚动到底部时,可以使用EndlessScrollListener加载更多数据。 8. **错误处理**:在网络请求失败或者数据加载异常时,...
本项目“安卓高仿qq的listview列表”旨在模仿QQ应用的ListView设计,它不仅包含了基本的列表展示功能,还集成了进阶特性如上拉加载更多、下拉刷新以及滑动删除,从而提升用户体验。 1. **ListView基础**: - ...
2. 分页加载:如果数据量非常大,考虑使用下拉刷新和上拉加载更多的功能,以减少内存消耗和初始化时间。 通过以上步骤,你可以在ListView中实现A-Z字母排序和筛选功能,提供更友好的用户体验。不断优化和改进,将使...
3. 下拉刷新和上拉加载更多(Pull to Refresh & Load More):可以集成SwipeRefreshLayout和EndlessScrollListener,实现下拉刷新数据和自动加载更多内容的功能。 总的来说,“ListViewDemo”项目是一个很好的起点...
5. **分页加载(下拉刷新和上拉加载更多)**:在处理大数据集时,可以采用分页加载策略,即当用户滚动到列表底部时,动态加载更多数据。这可以通过监听ListView的滚动事件和结合PullToRefresh库实现。 6. **...
8. **下拉刷新和上拉加载**:现代应用中,ListView常常需要支持下拉刷新和上拉加载更多数据的功能,`listviewdemo`可能会包含这些高级特性。 9. **分组和折叠效果**:ListView还可以实现分组列表,甚至带有折叠/...
在Android开发中,ListView是一种常用的UI组件,用于展示大量数据列表。`CompleteListView`项目针对ListView进行了增强,添加了下拉刷新、滑动删除以及固定标题等实用功能,提升了用户体验。下面将详细介绍这些功能...
本教程集合了Android端的一些基本视图操作实例,涵盖了ListView、Spinner以及ImageSwitch这三个常用组件,旨在帮助初学者快速入门Android UI设计。 **ListView** 是Android中用于展示大量数据的列表视图,它能滚动...
本资源包包含了多个关于Android瀑布流的示例代码,涵盖了从基础到进阶的各种实现方式,帮助开发者深入理解和掌握瀑布流布局的实现。 1. **基本原理** 瀑布流布局的核心是通过计算每一列的高度来确定内容的排列。...
2. **ListView/RecyclerView滚动效果**:在长列表中,可以添加滚动头部和底部的加载动画,比如上拉刷新和下拉加载更多。这些特效通常结合SwipeRefreshLayout和LoadMoreView来实现,可以增强用户交互体验。 3. **...
《Android应用源码之OssSystem(OA系统图书管理简单版)》是一个针对初学者和进阶者深入了解Android开发的实践项目。这个源码库提供了一个基础的图书管理系统,适用于办公室自动化(OA)场景,展示了如何在Android...
本教程以"动手学Android之九——列表没那么简单的例子程序"为主题,深入探讨ListView的使用和一些进阶技巧。博客作者希望通过这个实例,帮助开发者更好地理解和掌握Android中列表的复杂应用。 首先,我们要了解...