我定制了一个pull-to-refresh列表视图,我重写了方法" onTouchEvent(MotionEvent ev)"。但是当我在屏幕上拉下来时,MotionEvent.ACTION_DOWN赢了&# 39; t表演。这是我的代码。
public class ExListView extends ListView implements AbsListView.OnScrollListener {
private int firstVisibleItemPosition;
private int downY; // delta Y when pulling down
private final int DOWN_PULL_REFRESH = 0;
private final int RELEASE_REFRESH = 1;
private final int REFRESHING = 2;
private int currentState = DOWN_PULL_REFRESH;
private OnRefreshListener mOnRefreshListener;
private Animation upAnimation;
private Animation downAnimation;
private View headerView;
private ImageView ivArrow;
private ProgressBar mProgressBar;
private TextView tvState; // state of header
private int headerViewHeight; // header height
private View footerView; // footer
private int footerViewHeight; // footer height
private boolean isScrollToBottom;
private boolean isLoadingMore = false;
public ExListView(Context context) {
super(context);
init();
}
public ExListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ExListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING) {
if (isScrollToBottom && !isLoadingMore) {
isLoadingMore = true;
footerView.setPadding(0, 0, 0, 0);
this.setSelection(this.getCount());
if (mOnRefreshListener != null) {
mOnRefreshListener.onLoadMore();
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
firstVisibleItemPosition = firstVisibleItem;
if (getLastVisiblePosition() == (totalItemCount - 1)) {
isScrollToBottom = true;
} else {
isScrollToBottom = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) ev.getY();
int diff = (moveY - downY) / 2;
int paddingTop = -headerViewHeight + diff;
super.onTouchEvent(ev);
if (firstVisibleItemPosition == 0 && -headerViewHeight < paddingTop) {
if (paddingTop > 0 && currentState == DOWN_PULL_REFRESH) {
currentState = RELEASE_REFRESH;
refreshHeaderView();
} else if (paddingTop < 0 && currentState == RELEASE_REFRESH) {
currentState = DOWN_PULL_REFRESH;
refreshHeaderView();
}
headerView.setPadding(0, paddingTop, 0, 0);
return true;
}
break;
case MotionEvent.ACTION_UP:
if (currentState == RELEASE_REFRESH) {
headerView.setPadding(0, 0, 0, 0);
currentState = REFRESHING;
refreshHeaderView();
if (mOnRefreshListener != null) {
mOnRefreshListener.onRefresh();
}
} else if (currentState == DOWN_PULL_REFRESH) {
// hide header
headerView.setPadding(0, -headerViewHeight, 0, 0);
}
break;
default:
break;
}
return super.onTouchEvent(ev);
}
private void init() {
this.setOnScrollListener(this);
//animation
upAnimation = new RotateAnimation(0f, -180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
upAnimation.setDuration(500);
upAnimation.setFillAfter(true);
downAnimation = new RotateAnimation(-180f, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
downAnimation.setDuration(500);
downAnimation.setFillAfter(true);
//header
headerView = View.inflate(getContext(), R.layout.listview_header, null);
ivArrow = (ImageView) headerView.findViewById(R.id.iv_listview_header_arrow);
mProgressBar = (ProgressBar) headerView.findViewById(R.id.pb_listview_header);
tvState = (TextView) headerView.findViewById(R.id.tv_listview_header_state);
headerView.measure(0, 0);
headerViewHeight = headerView.getMeasuredHeight();
headerView.setPadding(0, -headerViewHeight, 0, 0);
this.addHeaderView(headerView);
//footer
footerView = View.inflate(getContext(), R.layout.listview_footer, null);
footerView.measure(0, 0);
footerViewHeight = footerView.getMeasuredHeight();
footerView.setPadding(0, -footerViewHeight, 0, 0);
this.addFooterView(footerView);
}
private void refreshHeaderView() {
switch (currentState) {
case DOWN_PULL_REFRESH: // pull down
tvState.setText("Pull to refresh");
ivArrow.startAnimation(downAnimation);
break;
case RELEASE_REFRESH: // release
tvState.setText("Release to refresh");
ivArrow.startAnimation(upAnimation);
break;
case REFRESHING: // freshing
ivArrow.clearAnimation();
ivArrow.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
tvState.setText("Refreshing...");
break;
default:
break;
}
}
public void hideHeaderView() {
postDelayed(new Runnable() {
@Override
public void run() {
headerView.setPadding(0, -headerViewHeight, 0, 0);
ivArrow.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
tvState.setText("Pull to refresh");
currentState = DOWN_PULL_REFRESH;
}
}, 1000);
}
public void hideFooterView() {
postDelayed(new Runnable() {
@Override
public void run() {
footerView.setPadding(0, -footerViewHeight, 0, 0);
isLoadingMore = false;
}
}, 1000);
}
public interface OnRefreshListener {
void onRefresh();
void onLoadMore();
}
public void setOnRefreshListener(OnRefreshListener listener) {
mOnRefreshListener = listener;
}
}
布局xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/navi_bar" />
<LinearLayout
android:id="@+id/page_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<my.widgets.ExListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include layout="@layout/empty_view" />
</LinearLayout>
先谢谢!
答案 0 :(得分:0)
好的,所以按下操作只是在您触摸屏幕时才开始点击。现在我不是专家,但是我会这样做:
private boolean touch = false;
@Override public boolean onTouchEvent (MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = (int) ev.getY();
touch = true;
return true;
case MotionEvent.ACTION_UP:
if (touch) {
touch = false;
moveY = (int) ev.getY();
diff = (moveY - downY) / 2;
//do the rest
}
return true;
}
return true;
}
}
我将变量输出到控制台,这给了我正确的downY,moveY和差异。