SoFunction
Updated on 2025-03-11

Android pull-down refresh and detailed explanation of how to use GridView

GridView is a control similar to ListView, except that GridView can use multiple columns to render content, while ListView is in behavior units, so the usage is similar.
The main layout file is added because it needs to be refreshed by pull-down, so a ProgressBar is added. The numColumns property of GridView refers to how many columns there are in each row.

 <RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="" >

  <ProgressBar
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

  <GridView
    android:layout_below="@id/pb"
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:gravity="center"
    android:numColumns="2" >
  </GridView>

</RelativeLayout>

The layout file for each Item, here is a simple picture and a paragraph of text.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <ImageView
    android:
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="3" />

  <TextView
    android:
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

</LinearLayout>

Main activity code:

public class MainActivity extends Activity {
  private GridView gv;
  private ProgressBar pb;
  private List&lt;Map&lt;String, Object&gt;&gt; list;
  private SimpleAdapter adapter;
  private GestureDetector gsDetector;

  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch () {
      case 1:
        ();
        (, "Refresh successfully", 200).show();
        break;
      default:
        break;
      }
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    pb = (ProgressBar) findViewById();
    gv = (GridView) findViewById();
    ();
    initData();
    adapter = new SimpleAdapter(this, list, .item_layout,
        new String[] { "image", "text" }, new int[] { .item_iv,
            .item_tv });
    (adapter);
    gsDetector = new GestureDetector(this, new Mlistener());
    (new OnTouchListener() {

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        // ("MainActivity", ()+"");
        return (event);
      }
    });
  }

  private void initData() {
    list = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
    for (int i = 0; i &lt; 20; i++) {
      Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
      ("image", .gift_item_default);
      ("text", "A Ice Pig");
      (map);
    }
  }

  class Mlistener implements OnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
      return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
      return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
        float distanceX, float distanceY) {
      return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
      if (() - () &gt; 0 &amp;&amp; () == 0) {
        ();
        Animation animation = new ScaleAnimation(1f, 1f, 0, 1f);
        (300);
        (animation);

        new Thread(new Runnable() {
          @Override
          public void run() {
            try {
              (2000);
              Message msg = new Message();
               = 1;
              (msg);
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              ();
            }
          }
        }).start();

      }
      return false;
    }

  }

}

Analysis:
In the onCreate method, first get two components, then set progressBar to hide, display it when it is pulled down, and hide it after refreshing. Then set the data source for the GridView. For convenience, use SimpleAdapter, then set the ontouchListener for the GridView, and hand the touch event to our customized GestureDetector object in the onTouch method to handle it. The pull-down event is handled in the onFling method of GestureDetector, and determine whether the pull-down and whether the GridView is at the top. If so, display the progressBar control and open a thread to handle the refresh. Here, it will sleep for 2000 milliseconds. Finally, use the message object to return a message to the Handler, and the Handler updates the GridView in the main thread.

@Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    pb = (ProgressBar) findViewById();
    gv = (GridView) findViewById();
    ();
    initData();
    adapter = new SimpleAdapter(this, list, .item_layout,
        new String[] { "image", "text" }, new int[] { .item_iv,
            .item_tv });
    (adapter);
    gsDetector = new GestureDetector(this, new Mlistener());
    (new OnTouchListener() {

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        // ("MainActivity", ()+"");
        return (event);
      }
    });
  }

class Mlistener implements OnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
      return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
      return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
        float distanceX, float distanceY) {
      return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
      if (() - () > 0 && () == 0) {
        ();
        Animation animation = new ScaleAnimation(1f, 1f, 0, 1f);
        (300);
        (animation);

        new Thread(new Runnable() {
          @Override
          public void run() {
            try {
              (2000);
              Message msg = new Message();
               = 1;
              (msg);
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              ();
            }
          }
        }).start();

      }
      return false;
    }

  }

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.