@Override
public boolean onTouchEvent(MotionEvent event) {
final int y = (int) event.getY(); // 获取点击位置的Y坐标
switch (event.getAction()) {
case MotionEvent.ACTION_UP: // 手指抬起
if (!isVerticalScrollBarEnabled()) {
setVerticalScrollBarEnabled(true);
}
if (getFirstVisiblePosition() == 0 && mRefreshState != REFRESHING) {
if ((mRefreshHeaderView.getBottom() > mRefreshViewHeight
|| mRefreshHeaderView.getTop() >= 0)
&& mRefreshState == RELEASE_TO_REFRESH) {
// Initiate the refresh
mRefreshState = REFRESHING; // 刷新状态
prepareForRefresh();
onRefresh();
} else if (mRefreshHeaderView.getBottom() < mRefreshViewHeight
|| mRefreshHeaderView.getTop() < 0) {
// Abort refresh and scroll down below the refresh view
resetHeader();
setSelection(1);
}
}
break;
case MotionEvent.ACTION_DOWN:
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
applyHeaderPadding(event);
break;
}
return super.onTouchEvent(event);
}
private void applyHeaderPadding(MotionEvent ev) {
final int historySize = ev.getHistorySize();
// Workaround for getPointerCount() which is unavailable in 1.5
// (it’s always 1 in 1.5)
int pointerCount = 1;
try {
Method method = MotionEvent.class.getMethod(“getPointerCount”);
pointerCount = (Integer)method.invoke(ev);
} catch (NoSuchMethodException e) {
pointerCount = 1;
} catch (IllegalArgumentException e) {
throw e;
} catch (IllegalAccessException e) {
System.err.println("unexpected " + e);
} catch (InvocationTargetException e) {
System.err.println("unexpected " + e);
}
for (int h = 0; h < historySize; h++) {
for (int p = 0; p < pointerCount; p++) {
if (mRefreshState == RELEASE_TO_REFRESH) {
if (isVerticalFadingEdgeEnabled()) {
setVerticalScrollBarEnabled(false);
}
int historicalY = 0;
try {
// For Android > 2.0
Method method = MotionEvent.class.getMethod(
“getHistoricalY”, Integer.TYPE, Integer.TYPE);
historicalY = ((Float) method.invoke(ev, p, h)).intValue();
} catch (NoSuchMethodException e) {
// For Android < 2.0
historicalY = (int) (ev.getHistoricalY(h));
} catch (IllegalArgumentException e) {
throw e;
} catch (IllegalAccessException e) {
System.err.println("unexpected " + e);
} catch (InvocationTargetException e) {
System.err.println("unexpected " + e);
}
// Calculate the padding to apply, we divide by 1.7 to
// simulate a more resistant effect during pull.
int topPadding = (int) (((historicalY - mLastMotionY)
- mRefreshViewHeight) / 1.7);
// 设置上、下、左、右四个位置的间隙间隙
mRefreshHeaderView.setPadding(
mRefreshHeaderView.getPaddingLeft(),
topPadding,
mRefreshHeaderView.getPaddingRight(),
mRefreshHeaderView.getPaddingBottom());
}
}
}
}
/**
- Sets the header padding back to original size.
- 设置头部填充会原始大小
*/
private void resetHeaderPadding() {
mRefreshHeaderView.setPadding(
mRefreshHeaderView.getPaddingLeft(),
mRefreshOriginalTopPadding,
mRefreshHeaderView.getPaddingRight(),
mRefreshHeaderView.getPaddingBottom());
}
/**
- Resets the header to the original state.
- 重新设置头部为原始状态
*/
private void resetHeader() {
if (mRefreshState != TAP_TO_REFRESH) {
mRefreshState = TAP_TO_REFRESH;
resetHeaderPadding();
// Set refresh view text to the pull label
mRefreshViewText.setText(R.string.pull_to_refresh_tap_label);
// Replace refresh drawable with arrow drawable
mRefreshViewImage.setImageResource(R.drawable.ic_pulltorefresh_arrow);
// Clear the full rotation animation
mRefreshViewImage.clearAnimation();
// Hide progress bar and arrow.
mRefreshViewImage.setVisibility(View.GONE);
mRefreshViewProgress.setVisibility(View.GONE);
}
}
/**
- 重设ListView尾部视图为初始状态
*/
private void resetFooter() {
if(mLoadState != TAP_TO_LOADMORE) {
mLoadState = TAP_TO_LOADMORE;
// 进度条设置为不可见
mLoadMoreProgress.setVisibility(View.GONE);
// 按钮的文本替换为“加载更多”
mLoadMoreText.setText(R.string.loadmore_label);
}
}
/**
- 测量视图的大小
- @param child
*/
private void measureView(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_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) {
// When the refresh view is completely visible, change the text to say
// “Release to refresh…” and flip the arrow drawable.
if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
&& mRefreshState != REFRESHING) {
if (firstVisibleItem == 0) { // 如果第一个可见条目为0
mRefreshViewImage.setVisibility(View.VISIBLE); // 让指示箭头变得可见
/如果头部视图相对与父容器的位置大于其自身高度+20或者头部视图的顶部位置>0,并且要在刷新状态不等于"释放以刷新"/
if ((mRefreshHeaderView.getBottom() > mRefreshViewHeight + 20
|| mRefreshHeaderView.getTop() >= 0)
&& mRefreshState != RELEASE_TO_REFRESH) {
mRefreshViewText.setText(R.string.pull_to_refresh_release_label);// 设置刷新文本为"Release to refresh…"
mRefreshViewImage.clearAnimation(); // 清除动画
mRefreshViewImage.startAnimation(mFlipAnimation); // 启动动画
mRefreshState = RELEASE_TO_REFRESH; // 更改刷新状态为“释放以刷新"
} else if (mRefreshHeaderView.getBottom() < mRefreshViewHeight + 20
&& mRefreshState != PULL_TO_REFRESH) {
mRefreshViewText.setText(R.string.pull_to_refresh_pull_label);// 设置刷新文本为"Pull to refresh…"
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);
}
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(view, firstVisibleItem,
visibleItemCount, totalItemCount);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
mCurrentScrollState = scrollState;
if (mOnScrollListener != null) {
mOnScrollListener.onScrollStateChanged(view, scrollState);
}
}
/为刷新做准备/
public void prepareForRefresh() {
resetHeaderPadding();
mRefreshViewImage.setVisibility(View.GONE); // 去掉刷新的箭头
// We need this hack, otherwise it will keep the previous drawable.
mRefreshViewImage.setImageDrawable(null);
mRefreshViewProgress.setVisibility(View.VISIBLE); // 圆形进度条变为可见
// Set refresh view text to the refreshing label
mRefreshViewText.setText(R.string.pull_to_refresh_refreshing_label);
mRefreshState = REFRESHING;
}
/为加载更多做准备/
public void prepareForLoadMore() {
mLoadMoreProgress.setVisibility(View.VISIBLE);
mLoadMoreText.setText(R.string.loading_label);
mLoadState = LOADING;
}
public void onRefresh() {
Log.d(TAG, “onRefresh”);
if (mOnRefreshListener != null) {
mOnRefreshListener.onRefresh();
}
}
public void OnLoadMore() {
Log.d(TAG, “onLoadMore”);
if(mOnRefreshListener != null) {
mOnRefreshListener.onLoadMore();
}
}
/**
- Resets the list to a normal state after a refresh.
- @param lastUpdated Last updated at.
*/
public void onRefreshComplete(CharSequence lastUpdated) {
setLastUpdated(lastUpdated); // 显示更新时间
onRefreshComplete();
}
/**
- Resets the list to a normal state after a refresh.
*/
public void onRefreshComplete() {
Log.d(TAG, “onRefreshComplete”);
resetHeader();
// If refresh view is visible when loading completes, scroll down to
// the next item.
if (mRefreshHeaderView.getBottom() > 0) {
invalidateViews();
setSelection(1);
}
}
public void onLoadMoreComplete() {
Log.d(TAG, “onLoadMoreComplete”);
resetFooter();
}
/**
- Invoked when the refresh view is clicked on. This is mainly used when
- there’s only a few items in the list and it’s not possible to drag the
- list.
- 点击刷新
*/
private class OnClickRefreshListener implements OnClickListener {
@Override
public void onClick(View v) {
if (mRefreshState != REFRESHING) {
prepareForRefresh();
onRefresh();
}
}
}
/**
- @author wwj
- 加载更多
*/
private class OnClickLoadMoreListener implements OnClickListener {
@Override
public void onClick(View v) {
if(mLoadState != LOADING) {
prepareForLoadMore();
OnLoadMore();
}
}
}
/**
- Interface definition for a callback to be invoked when list should be
- refreshed.
- 接口定义一个回调方法当列表应当被刷新
*/
public interface OnRefreshListener {
/**
- Called when the list should be refreshed.
- 当列表应当被刷新是调用这个方法
- A call to {@link PullToRefreshListView #onRefreshComplete()} is
- expected to indicate that the refresh has completed.
*/
public void onRefresh();
public void onLoadMore();
}
}
使用方法:
package com.markupartist.android.example.pulltorefresh;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;
public class PullToRefreshActivity extends Activity {
private LinkedList mListItems;
public static PullToRefreshListView weiboListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh);
weiboListView = (PullToRefreshListView) findViewById(R.id.weibolist);
// Set a listener to be invoked when the list should be refreshed.
weiboListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask(PullToRefreshActivity.this, 0).execute();
}
@Override
public void onLoadMore() {
new GetDataTask(PullToRefreshActivity.this, 1).execute();
}
});
mListItems = new LinkedList();
mListItems.addAll(Arrays.asList(mStrings));
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, mListItems);
weiboListView.setAdapter(adapter);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
private Context context;
private int index;
public GetDataTask(Context context, int index) {
this.context = context;
this.index = index;
}
@Override
protected String[] doInBackground(Void… params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
;
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
if (index == 0) {
// 将字符串“Added after refresh”添加到顶部
mListItems.addFirst(“Added after refresh…”);
SimpleDateFormat format = new SimpleDateFormat(
“yyyy年MM月dd日 HH:mm”);
String date = format.format(new Date());
// Call onRefreshComplete when the list has been refreshed.
weiboListView.onRefreshComplete(date);
} else if (index == 1) {
mListItems.addLast(“Added after loadmore…”);
weiboListView.onLoadMoreComplete();
}
super.onPostExecute(result);
}
}
public static String[] mStrings = { “一条微博”, “两条微博”, “三条微博”, “四条微博”, “五条微博”,
“六条微博”, “七条微博”, “八条微博”, “九条微博”, “十条微博”, “十一条微博”, “十二条微博” };
}
下拉刷新的那个头部布局
/2013.08.22_PullToRefresh_ListView_Demo/res/layout/pull_to_refresh_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:gravity=“center”
android:paddingBottom=“15dip”
android:paddingTop=“10dip” >
<ProgressBar
android:id=“@+id/pull_to_refresh_progress”
style=“?android:attr/progressBarStyleSmall”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_centerVertical=“true”
android:layout_marginLeft=“30dip”
android:layout_marginRight=“20dip”
android:layout_marginTop=“10dip”
android:indeterminate=“true”
android:visibility=“gone” />
<ImageView
android:id=“@+id/pull_to_refresh_image”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:layout_marginLeft=“30dip”
android:layout_marginRight=“20dip”
android:gravity=“center”
android:src=“@drawable/ic_pulltorefresh_arrow”
android:visibility=“gone” />
<TextView
android:id=“@+id/pull_to_refresh_text”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:gravity=“center”
android:paddingTop=“5dip”
android:text=“@string/pull_to_refresh_tap_label”
android:textAppearance=“?android:attr/textAppearanceMedium”
android:textStyle=“bold” />
<TextView
android:id=“@+id/pull_to_refresh_updated_at”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_below=“@+id/pull_to_refresh_text”
android:layout_gravity=“center”
android:gravity=“center”
android:textAppearance=“?android:attr/textAppearanceSmall”
android:visibility=“gone” />
加载更多的底部布局
/2013.08.22_PullToRefresh_ListView_Demo/res/layout/loadmore_footer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id=“@+id/loadmore_layout”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:gravity=“center”
android:paddingBottom=“15dip”
android:paddingTop=“10dip” >
<ProgressBar
android:id=“@+id/loadmore_progress”
style=“?android:attr/progressBarStyleSmall”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_centerVertical=“true”
android:layout_marginLeft=“30dip”
android:layout_marginRight=“20dip”
android:layout_marginTop=“10dip”
android:indeterminate=“true”
android:visibility=“gone” />
<TextView
android:id=“@+id/loadmore_text”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:gravity=“center”
android:paddingTop=“5dip”
android:text=“@string/loadmore_label”
android:textAppearance=“?android:attr/textAppearanceMedium”
android:textStyle=“bold” />
自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
droid:background=“@drawable/weibo_list_item_selector”
android:text=“@string/loadmore_label”
android:textColor=“@color/loadmore_btn_selector”
android:textSize=“18.0sp” />
–>
<ProgressBar
android:id=“@+id/loadmore_progress”
style=“?android:attr/progressBarStyleSmall”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_centerVertical=“true”
android:layout_marginLeft=“30dip”
android:layout_marginRight=“20dip”
android:layout_marginTop=“10dip”
android:indeterminate=“true”
android:visibility=“gone” />
<TextView
android:id=“@+id/loadmore_text”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:gravity=“center”
android:paddingTop=“5dip”
android:text=“@string/loadmore_label”
android:textAppearance=“?android:attr/textAppearanceMedium”
android:textStyle=“bold” />
自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
[外链图片转存中…(img-1l5Rfvfy-1714555461427)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
版权归原作者 2401_84149607 所有, 如有侵权,请联系我们删除。