SoFunction
Updated on 2025-03-11

Android ListView implements the drop-down loading function

This example shares the specific code displayed by ListView drop-down loading for your reference. The specific content is as follows

1、

public class MyListView extends ListView implements OnScrollListener { 
 
 private final static int RELEASE_To_REFRESH = 0;// The status value of the pull-down process private final static int PULL_To_REFRESH = 1; // Return from pull-down to the status value that does not refresh private final static int REFRESHING = 2;// The refreshing status value private final static int DONE = 3; 
 private final static int LOADING = 4; 
 
 // The ratio of the actual padding distance to the offset distance on the interface private final static int RATIO = 3; 
 private LayoutInflater inflater; 
 
 // The layout of refreshing the ListView head pull-down private LinearLayout headerView; 
 private TextView lvHeaderTipsTv; 
 private TextView lvHeaderLastUpdatedTv; 
 private ImageView lvHeaderArrowIv; 
 private ProgressBar lvHeaderProgressBar; 
 
 // Define the height of the layout for refreshing by the head pull-down private int headerContentHeight; 
 
 private RotateAnimation animation; 
 private RotateAnimation reverseAnimation; 
 
 private int startY; 
 private int state; 
 private boolean isBack; 
 
 // Used to ensure that the value of startY is recorded only once in a complete touch event private boolean isRecored; 
 
 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) { 
  inflater = (context); 
  headerView = (LinearLayout) (.lv_header, null); 
  lvHeaderTipsTv = (TextView) headerView 
    .findViewById(); 
  lvHeaderLastUpdatedTv = (TextView) headerView 
    .findViewById(); 
 
  lvHeaderArrowIv = (ImageView) headerView 
    .findViewById(); 
  // Set the minimum height and width of the pull-down refresh icon  (70); 
  (50); 
 
  lvHeaderProgressBar = (ProgressBar) headerView 
    .findViewById(); 
  measureView(headerView); 
  headerContentHeight = (); 
  // Set the inner margin, just the height of the entire layout with a negative distance from the top, just hide the head  (0, -1 * headerContentHeight, 0, 0); 
  // Repaint it  (); 
  // Add the drop-down refreshed layout to the top of the ListView  addHeaderView(headerView, null, false); 
  // Set scrolling listening events  setOnScrollListener(this); 
 
  // Set rotation animation event  animation = new RotateAnimation(0, -180, 
    RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
    RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
  (new LinearInterpolator()); 
  (250); 
  (true); 
 
  reverseAnimation = new RotateAnimation(-180, 0, 
    RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
    RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
  (new LinearInterpolator()); 
  (200); 
  (true); 
 
  // The initial state is the state where the pull-down refresh is completed, so it is DONE  state = DONE; 
  // Is it refreshing?  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 (()) { 
   case MotionEvent.ACTION_DOWN: 
    if (!isRecored) { 
     isRecored = true; 
     startY = (int) ();// Record the current position when the finger is pressed    } 
    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) (); 
    if (!isRecored) { 
     isRecored = true; 
     startY = tempY; 
    } 
    if (state != REFRESHING && isRecored && state != LOADING) { 
     // Ensure that the current position is always at the head during the padding process. Otherwise, if the list exceeds the screen, the list will scroll at the same time when pushing up.     // You can let go and refresh     if (state == RELEASE_To_REFRESH) { 
      setSelection(0); 
      // Push it up, pushing it to the point where the screen covers the head enough, but it has not yet been pushed to the point where it covers all      if (((tempY - startY) / RATIO < headerContentHeight)// Change from release refresh state to pull-down refresh state        && (tempY - startY) > 0) { 
       state = PULL_To_REFRESH; 
       changeHeaderViewByState(); 
      } 
      // It was pushed to the top all at once      else if (tempY - startY <= 0) {// Change from release refresh state to done state       state = DONE; 
       changeHeaderViewByState(); 
      } 
     } 
     // DONE or PULL_To_REFRESH status has not yet been reached when the display is released and refreshed     if (state == PULL_To_REFRESH) { 
      setSelection(0); 
      // Pull down to the state where you can enter RELEASE_TO_REFRESH      if ((tempY - startY) / RATIO >= headerContentHeight) {// Change from done or pull-down refresh status to release refresh       state = RELEASE_To_REFRESH; 
       isBack = true; 
       changeHeaderViewByState(); 
      } 
      // Push it to the top      else if (tempY - startY <= 0) {// Change from DOne or pull-down refresh state to done state       state = DONE; 
       changeHeaderViewByState(); 
      } 
     } 
     // In done state     if (state == DONE) { 
      if (tempY - startY > 0) { 
       state = PULL_To_REFRESH; 
       changeHeaderViewByState(); 
      } 
     } 
     // Update the size of the headView     if (state == PULL_To_REFRESH) { 
      (0, -1 * headerContentHeight 
        + (tempY - startY) / RATIO, 0, 0); 
 
     } 
     // Update the paddingTop of headView     if (state == RELEASE_To_REFRESH) { 
      (0, (tempY - startY) / RATIO 
        - headerContentHeight, 0, 0); 
     } 
 
    } 
    break; 
 
   default: 
    break; 
   } 
  } 
  return (ev); 
 } 
 
 // When the state changes, call this method to update the interface private void changeHeaderViewByState() { 
  switch (state) { 
  case RELEASE_To_REFRESH: 
   (); 
   (); 
   (); 
   (); 
 
   ();// Clear animation   (animation);// Start animation effect 
   ("Release Refresh"); 
   break; 
  case PULL_To_REFRESH: 
   (); 
   (); 
   (); 
   (); 
   (); 
   // It is caused by the RELEASE_To_REFRESH state transition   if (isBack) { 
    isBack = false; 
    (); 
    (reverseAnimation); 
 
    ("Drive down to refresh"); 
   } else { 
    ("Drive down to refresh"); 
   } 
   break; 
 
  case REFRESHING: 
 
   (0, 0, 0, 0); 
 
   (); 
   (); 
   (); 
   ("Refreshing..."); 
   (); 
   break; 
  case DONE: 
   (0, -1 * headerContentHeight, 0, 0); 
 
   (); 
   (); 
   (); 
   ("Drive down to refresh"); 
   (); 
   break; 
  } 
 } 
 
 // This method is copied directly from a drop-down refresh demo on the network, here is the width and height of the headView. private void measureView(View child) { 
   params = (); 
  if (params == null) { 
   params = new ( 
     .FILL_PARENT, 
     .WRAP_CONTENT); 
  } 
  int childWidthSpec = (0, 0 + 0, 
    ); 
  int lpHeight = ; 
  int childHeightSpec; 
  if (lpHeight > 0) { 
   childHeightSpec = (lpHeight, 
     ); 
  } else { 
   childHeightSpec = (0, 
     ); 
  } 
  (childWidthSpec, childHeightSpec); 
 } 
 
 public void setonRefreshListener(OnRefreshListener refreshListener) { 
   = refreshListener; 
  isRefreshable = true; 
 } 
 
 public interface OnRefreshListener { 
  public void onRefresh(); 
 } 
 
 public void onRefreshComplete() { 
  state = DONE; 
  ("Latest Update:" + new Date().toLocaleString()); 
  changeHeaderViewByState(); 
 } 
 
 private void onLvRefresh() { 
  if (refreshListener != null) { 
   (); 
  } 
 } 
 
 public void setAdapter(LvAdapter adapter) { 
  ("Latest Update:" + new Date().toLocaleString()); 
  (adapter); 
 } 
 
} 

2、

public class MainActivity extends Activity { 
 private List<String> list; 
 private MyListView lv; 
 private LvAdapter adapter; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(); 
  lv = (MyListView) findViewById(); 
  list = new ArrayList<String>(); 
  ("loonggg"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  ("We are all developers"); 
  adapter = new LvAdapter(list, this); 
  (adapter); 
 
  (new OnRefreshListener() { 
 
   @Override 
   public void onRefresh() { 
    new AsyncTask<Void, Void, Void>() { 
     protected Void doInBackground(Void... params) { 
      try { 
       (1000); 
      } catch (Exception e) { 
       (); 
      } 
      ("Refreshed content"); 
      return null; 
     } 
 
     @Override 
     protected void onPostExecute(Void result) { 
      (); 
      (); 
     } 
    }.execute(null, null, null); 
   } 
  }); 
 } 
} 

3、LvAdapter
public class LvAdapter extends BaseAdapter { 
    private List<String> list; 
    private Context context; 
 
    public LvAdapter(List<String> list, Context context) { 
        = list; 
        = context; 
    } 
 
    @Override 
    public int getCount() { 
        return (); 
    } 
 
    @Override 
    public Object getItem(int position) { 
        return (position); 
    } 
 
    @Override 
    public long getItemId(int position) { 
        return position; 
    } 
 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        TextView tv = new TextView(()); 
        ((position)); 
        return tv; 
    } 
 

4、lv_header.xml
[html] view plain copy
<LinearLayout xmlns:androaa">/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" > 
 
<!-- Content -->
 
    <RelativeLayout 
        android: 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingLeft="30dp" > 
 
<!-- Arrow image, progress bar -->
 
        <FrameLayout 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentLeft="true" 
            android:layout_centerVertical="true" > 
 
<!-- Arrow -->
 
            <ImageView 
                android: 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_gravity="center" 
                android:src="@drawable/arrow" /> 
 
<!-- Progress Bar -->
 
            <ProgressBar 
                android: 
                style="?android:attr/progressBarStyleSmall" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_gravity="center" 
                android:visibility="gone" /> 
        </FrameLayout> 
 
<!-- Tip, Latest Updates -->
 
        <LinearLayout 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal" 
            android:orientation="vertical" > 
 
<!-- Tip -->
 
            <TextView 
                android: 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
android:text="drop-down refresh"
                android:textColor="#fff" 
                android:textSize="20sp" /> 
 
<!-- Recently updated -->
 
            <TextView 
                android: 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
android:text="Last updated"
                android:textColor="#333" 
                android:textSize="10sp" /> 
        </LinearLayout> 
    </RelativeLayout> 
 
</LinearLayout> 

5、
[html] view plain copy
<LinearLayout xmlns:androaa">/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#000000" 
    android:orientation="vertical" > 
 
    < 
        android: 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 
 
</LinearLayout> 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.