Back to project page student_travel_app.
The source code is released under:
Apache License
If you think the Android project student_travel_app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.piasy.simpletravel; /*from www .j a v a 2s .co m*/ 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.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; public class ScrollRefreshListView extends ListView implements OnScrollListener { private final static int RELEASE_To_REFRESH = 0;// ????????? private final static int PULL_To_REFRESH = 1; // ??????????????? private final static int REFRESHING = 2;// ????????? private final static int DONE = 3; private final static int LOADING = 4; // ???padding???????????????????? private final static int RATIO = 3; private LayoutInflater inflater; // ListView????????? private LinearLayout headerView; private TextView lvHeaderTipsTv; // private TextView lvHeaderLastUpdatedTv; private ImageView lvHeaderArrowIv; private ProgressBar lvHeaderProgressBar; // ?????????????? private int headerContentHeight; private RotateAnimation animation; private RotateAnimation reverseAnimation; private int startY; private int state; private boolean isBack; // ??????startY????????touch??????????? private boolean isRecored; private OnRefreshListener refreshListener; private boolean isRefreshable; public ScrollRefreshListView(Context context) { super(context); init(context); } public ScrollRefreshListView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { setCacheColorHint(context.getResources().getColor(android.R.color.transparent)); inflater = LayoutInflater.from(context); headerView = (LinearLayout) inflater.inflate(R.layout.lv_header, null); lvHeaderTipsTv = (TextView) headerView .findViewById(R.id.lvHeaderTipsTv); // lvHeaderLastUpdatedTv = (TextView) headerView // .findViewById(R.id.lvHeaderLastUpdatedTv); lvHeaderArrowIv = (ImageView) headerView .findViewById(R.id.lvHeaderArrowIv); // ????????????????? lvHeaderArrowIv.setMinimumWidth(70); lvHeaderArrowIv.setMinimumHeight(50); lvHeaderProgressBar = (ProgressBar) headerView .findViewById(R.id.lvHeaderProgressBar); measureView(headerView); headerContentHeight = headerView.getMeasuredHeight(); // ???????????????????????????????????? headerView.setPadding(0, -1 * headerContentHeight, 0, 0); // ????? headerView.invalidate(); // ??????????ListView??? addHeaderView(headerView, null, false); // ?????????? setOnScrollListener(this); // ???????? animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); animation.setInterpolator(new LinearInterpolator()); animation.setDuration(250); animation.setFillAfter(true); reverseAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); reverseAnimation.setInterpolator(new LinearInterpolator()); reverseAnimation.setDuration(200); reverseAnimation.setFillAfter(true); // ??????????????????????DONE state = DONE; // ???????? isRefreshable = false; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem == 0) { isRefreshable = true; } else { isRefreshable = false; } } @Override public boolean onTouchEvent(MotionEvent ev) { if (isRefreshable) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: if (!isRecored) { isRecored = true; startY = (int) ev.getY();// ????????????? } break; case MotionEvent.ACTION_UP: if (state != REFRESHING && state != LOADING) { if (state == PULL_To_REFRESH) { state = DONE; changeHeaderViewByState(); } if (state == RELEASE_To_REFRESH) { state = REFRESHING; changeHeaderViewByState(); onLvRefresh(); } } isRecored = false; isBack = false; break; case MotionEvent.ACTION_MOVE: int tempY = (int) ev.getY(); if (!isRecored) { isRecored = true; startY = tempY; } if (state != REFRESHING && isRecored && state != LOADING) { //???????padding????????????????head? //????????????????????????????????????? // ???????????? if (state == RELEASE_To_REFRESH) { setSelection(0); // ???????????????head?????????????????? if (((tempY - startY) / RATIO < headerContentHeight)// ?????????????????????? && (tempY - startY) > 0) { state = PULL_To_REFRESH; changeHeaderViewByState(); } // ???????? else if (tempY - startY <= 0) {// ???????????????done??? state = DONE; changeHeaderViewByState(); } } // ????????????????,DONE???PULL_To_REFRESH??? if (state == PULL_To_REFRESH) { setSelection(0); // ?????????RELEASE_TO_REFRESH???? if ((tempY - startY) / RATIO >= headerContentHeight) {// ?done???????????????????? state = RELEASE_To_REFRESH; isBack = true; changeHeaderViewByState(); } // ????? else if (tempY - startY <= 0) {// ?DOne??????????????done??? state = DONE; changeHeaderViewByState(); } } // done???? if (state == DONE) { if (tempY - startY > 0) { state = PULL_To_REFRESH; changeHeaderViewByState(); } } // ??headView?size if (state == PULL_To_REFRESH) { headerView.setPadding(0, -1 * headerContentHeight + (tempY - startY) / RATIO, 0, 0); } // ??headView?paddingTop if (state == RELEASE_To_REFRESH) { headerView.setPadding(0, (tempY - startY) / RATIO - headerContentHeight, 0, 0); } } break; default: break; } } return super.onTouchEvent(ev); } // ???????????????????????? private void changeHeaderViewByState() { switch (state) { case RELEASE_To_REFRESH: lvHeaderArrowIv.setVisibility(View.VISIBLE); lvHeaderProgressBar.setVisibility(View.GONE); lvHeaderTipsTv.setVisibility(View.VISIBLE); // lvHeaderLastUpdatedTv.setVisibility(View.VISIBLE); lvHeaderArrowIv.clearAnimation();// ???? lvHeaderArrowIv.startAnimation(animation);// ?????? lvHeaderTipsTv.setText("??????"); break; case PULL_To_REFRESH: lvHeaderProgressBar.setVisibility(View.GONE); lvHeaderTipsTv.setVisibility(View.VISIBLE); // lvHeaderLastUpdatedTv.setVisibility(View.VISIBLE); lvHeaderArrowIv.clearAnimation(); lvHeaderArrowIv.setVisibility(View.VISIBLE); // ??RELEASE_To_REFRESH??????????? if (isBack) { isBack = false; lvHeaderArrowIv.clearAnimation(); lvHeaderArrowIv.startAnimation(reverseAnimation); lvHeaderTipsTv.setText("????"); } else { lvHeaderTipsTv.setText("????"); } break; case REFRESHING: headerView.setPadding(0, 0, 0, 0); lvHeaderProgressBar.setVisibility(View.VISIBLE); lvHeaderArrowIv.clearAnimation(); lvHeaderArrowIv.setVisibility(View.GONE); lvHeaderTipsTv.setText("????..."); // lvHeaderLastUpdatedTv.setVisibility(View.VISIBLE); break; case DONE: headerView.setPadding(0, -1 * headerContentHeight, 0, 0); lvHeaderProgressBar.setVisibility(View.GONE); lvHeaderArrowIv.clearAnimation(); lvHeaderArrowIv.setImageResource(R.drawable.arrow_down); lvHeaderTipsTv.setText("????"); // lvHeaderLastUpdatedTv.setVisibility(View.VISIBLE); break; } } // ?????????????????????demo????????headView?width????height private void measureView(View child) { ViewGroup.LayoutParams params = child.getLayoutParams(); if (params == null) { params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, params.width); int lpHeight = params.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } child.measure(childWidthSpec, childHeightSpec); } public void setonRefreshListener(OnRefreshListener refreshListener) { this.refreshListener = refreshListener; isRefreshable = true; } public interface OnRefreshListener { public void onRefresh(); } public void onRefreshComplete() { state = DONE; changeHeaderViewByState(); } private void onLvRefresh() { if (refreshListener != null) { refreshListener.onRefresh(); } } public void setAdapter(MyListViewAdapter adapter) { super.setAdapter(adapter); } }