android programming pull down refresh implementation method analysis

  • 2020-10-31 21:59:01
  • OfStack

An example of this article describes the android programming pull-down refresh implementation. To share for your reference, the details are as follows:

At present, there are many drop-downs and refreshes in android app. Today, I will study 1 related data by myself


public class MyListView extends ListView implements OnScrollListener {
 private static final String TAG = "listview";
 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;
 //  The actual padding The ratio of the distance to the offset distance on the interface 
 private final static int RATIO = 3;
 private LayoutInflater inflater;
 private LinearLayout headView;
 private TextView tipsTextview;
 private TextView lastUpdatedTextView;
 private ImageView arrowImageView;
 private ProgressBar progressBar;
 private RotateAnimation animation;
 private RotateAnimation reverseAnimation;
 //  Used to ensure startY The value of the 1 A complete touch Only recorded in the event 1 time 
 private boolean isRecored;
 private int headContentWidth;
 private int headContentHeight;
 private int startY;
 private int firstItemIndex;
 private int state;
 private boolean isBack;
 private OnRefreshListener refreshListener;
 private boolean isRefreshable;
 public MyListView(Context context) {
  super(context);
  init(context);
 }
 public MyListView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context);
 }
 private void init(Context context) {
  setCacheColorHint(context.getResources().getColor(R.color.transparent));
  inflater = LayoutInflater.from(context);
  headView = (LinearLayout) inflater.inflate(R.layout.head, null);
  arrowImageView = (ImageView) headView
    .findViewById(R.id.head_arrowImageView);
  arrowImageView.setMinimumWidth(70);
  arrowImageView.setMinimumHeight(50);
  progressBar = (ProgressBar) headView
    .findViewById(R.id.head_progressBar);
  tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
  lastUpdatedTextView = (TextView) headView
    .findViewById(R.id.head_lastUpdatedTextView);
  measureView(headView);
  headContentHeight = headView.getMeasuredHeight();
  headContentWidth = headView.getMeasuredWidth();
  headView.setPadding(0, -1 * headContentHeight, 0, 0);
  headView.invalidate();
  Log.v("size", "width:" + headContentWidth + " height:"
    + headContentHeight);
  addHeaderView(headView, 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);
  state = DONE;
  isRefreshable = false;
 }
 public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,
   int arg3) {
  firstItemIndex = firstVisiableItem;
 }
 public void onScrollStateChanged(AbsListView arg0, int arg1) {
 }
 public boolean onTouchEvent(MotionEvent event) {
  if (isRefreshable) {
   switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    if (firstItemIndex == 0 && !isRecored) {
     isRecored = true;
     startY = (int) event.getY();
     Log.v(TAG, " in down Time record current position ' ");
    }
    break;
   case MotionEvent.ACTION_UP:
    if (state != REFRESHING && state != LOADING) {
     if (state == DONE) {
      //  Nothing 
     }
     if (state == PULL_To_REFRESH) {
      state = DONE;
      changeHeaderViewByState();
      Log.v(TAG, " From the drop-down refresh state, to done state ");
     }
     if (state == RELEASE_To_REFRESH) {
      state = REFRESHING;
      changeHeaderViewByState();
      onRefresh();
      Log.v(TAG, " By releasing the refresh state, to done state ");
     }
    }
    isRecored = false;
    isBack = false;
    break;
   case MotionEvent.ACTION_MOVE:
    int tempY = (int) event.getY();
    if (!isRecored && firstItemIndex == 0) {
     Log.v(TAG, " in move Time to record the location ");
     isRecored = true;
     startY = tempY;
    }
    if (state != REFRESHING && isRecored && state != LOADING) {
     //  Be sure to set padding Process of the current position 1 Straight in head Otherwise, if the list goes off the screen, it will scroll while it is being pushed up 
     //  You can let go and refresh 
     if (state == RELEASE_To_REFRESH) {
      setSelection(0);
      //  I'm pushing it up. I'm pushing it up enough to cover the screen head To the extent, but not to the extent of a total cover-up 
      if (((tempY - startY) / RATIO < headContentHeight)
        && (tempY - startY) > 0) {
       state = PULL_To_REFRESH;
       changeHeaderViewByState();
       Log.v(TAG, " Change from release refresh state to drop down refresh state ");
      }
      // 1 Now it was pushed to the top 
      else if (tempY - startY <= 0) {
       state = DONE;
       changeHeaderViewByState();
       Log.v(TAG, " Change from release refresh state to done state ");
      }
      //  It's pulled down, or it's not pushed up to cover the top of the screen head The point of 
      else {
       //  No special operations, just updates paddingTop And that's it 
      }
     }
     //  The display has not yet reached the point of release to refresh ,DONE Or is it PULL_To_REFRESH state 
     if (state == PULL_To_REFRESH) {
      setSelection(0);
      //  Drop down to enter RELEASE_TO_REFRESH The state of the 
      if ((tempY - startY) / RATIO >= headContentHeight) {
       state = RELEASE_To_REFRESH;
       isBack = true;
       changeHeaderViewByState();
       Log.v(TAG, " by done Or the dropdown refresh state changes to the release refresh ");
      }
      //  We're at the top 
      else if (tempY - startY <= 0) {
       state = DONE;
       changeHeaderViewByState();
       Log.v(TAG, " by DOne Or the dropdown refresh state is changed to done state ");
      }
     }
     // done State, 
     if (state == DONE) {
      if (tempY - startY > 0) {
       state = PULL_To_REFRESH;
       changeHeaderViewByState();
      }
     }
     //  update headView the size
     if (state == PULL_To_REFRESH) {
      headView.setPadding(0, -1 * headContentHeight
        + (tempY - startY) / RATIO, 0, 0);
     }
     //  update headView the paddingTop
     if (state == RELEASE_To_REFRESH) {
      headView.setPadding(0, (tempY - startY) / RATIO
        - headContentHeight, 0, 0);
     }
    }
    break;
   }
  }
  return super.onTouchEvent(event);
 }
 //  This method is called to update the interface when the state changes 
 private void changeHeaderViewByState() {
  switch (state) {
  case RELEASE_To_REFRESH:
   arrowImageView.setVisibility(View.VISIBLE);
   progressBar.setVisibility(View.GONE);
   tipsTextview.setVisibility(View.VISIBLE);
   lastUpdatedTextView.setVisibility(View.VISIBLE);
   arrowImageView.clearAnimation();
   arrowImageView.startAnimation(animation);
   tipsTextview.setText(" Loosen the refresh ");
   Log.v(TAG, " Current state, release refresh ");
   break;
  case PULL_To_REFRESH:
   progressBar.setVisibility(View.GONE);
   tipsTextview.setVisibility(View.VISIBLE);
   lastUpdatedTextView.setVisibility(View.VISIBLE);
   arrowImageView.clearAnimation();
   arrowImageView.setVisibility(View.VISIBLE);
   //  Is made up of RELEASE_To_REFRESH It's a state shift 
   if (isBack) {
    isBack = false;
    arrowImageView.clearAnimation();
    arrowImageView.startAnimation(reverseAnimation);
    tipsTextview.setText(" The drop-down refresh ");
   } else {
    tipsTextview.setText(" The drop-down refresh ");
   }
   Log.v(TAG, " Current state, pull down and refresh ");
   break;
  case REFRESHING:
   headView.setPadding(0, 0, 0, 0);
   progressBar.setVisibility(View.VISIBLE);
   arrowImageView.clearAnimation();
   arrowImageView.setVisibility(View.GONE);
   tipsTextview.setText(" Is refreshing ...");
   lastUpdatedTextView.setVisibility(View.VISIBLE);
   Log.v(TAG, " The current state , Is refreshing ...");
   break;
  case DONE:
   headView.setPadding(0, -1 * headContentHeight, 0, 0);
   progressBar.setVisibility(View.GONE);
   arrowImageView.clearAnimation();
   arrowImageView.setImageResource(R.drawable.arrow);
   tipsTextview.setText(" The drop-down refresh ");
   lastUpdatedTextView.setVisibility(View.VISIBLE);
   Log.v(TAG, " Current status, done");
   break;
  }
 }
 public void setonRefreshListener(OnRefreshListener refreshListener) {
  this.refreshListener = refreshListener;
  isRefreshable = true;
 }
 public interface OnRefreshListener {
  public void onRefresh();
 }
 public void onRefreshComplete() {
  state = DONE;
  lastUpdatedTextView.setText(" Recent updates :" + new Date().toLocaleString());
  changeHeaderViewByState();
 }
 private void onRefresh() {
  if (refreshListener != null) {
   refreshListener.onRefresh();
  }
 }
 //  This method is copied directly from the Internet 1 A drop-down refresh demo ", "estimate" headView the width As well as 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);
 }
 public void setAdapter(BaseAdapter adapter) {
  lastUpdatedTextView.setText(" Recent updates :" + new Date().toLocaleString());
  super.setAdapter(adapter);
 }
}

I hope this article has been helpful in Android programming.


Related articles: