Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android下拉刷新以及GridView使用方法詳解

Android下拉刷新以及GridView使用方法詳解

編輯:關於Android編程

GridView是類似於ListView的控件,只是GridView可以使用多個列來呈現內容,而ListView是以行為單位,所以用法上是差不多的。
主布局文件,因為要做下拉刷新,所以加了一個ProgressBar,GridView的numColumns屬性是指每一行有多少列

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.girdlayoutdemo.MainActivity" >

  <ProgressBar
    android:id="@+id/pb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

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

</RelativeLayout>

每個Item的布局文件,這裡比較簡單的一張圖片加一段文字

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <ImageView
    android:id="@+id/item_iv"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="3" />

  <TextView
    android:id="@+id/item_tv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

</LinearLayout>

主活動代碼:

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

  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case 1:
        pb.setVisibility(View.GONE);
        Toast.makeText(MainActivity.this, "刷新成功", 200).show();
        break;
      default:
        break;
      }
    }
  };

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

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        // Log.e("MainActivity", event.getX()+"");
        return gsDetector.onTouchEvent(event);
      }
    });
  }

  private void initData() {
    list = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < 20; i++) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("image", R.drawable.gift_item_default);
      map.put("text", "一只冰瑩豬豬");
      list.add(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 (e2.getY() - e1.getY() > 0 && gv.getFirstVisiblePosition() == 0) {
        pb.setVisibility(View.VISIBLE);
        Animation animation = new ScaleAnimation(1f, 1f, 0, 1f);
        animation.setDuration(300);
        pb.startAnimation(animation);

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

      }
      return false;
    }

  }

}

解析:
 在onCreate方法中先獲取兩個組件,然後把progressBar設置為隱藏,下拉的時候再顯示,刷新完畢再隱藏。然後為GridView設置數據源,這裡方便起見用SimpleAdapter,然後給GridView設置ontouchListener,並在onTouch方法中把觸摸事件交給我們自定義的GestureDetector對象來處理,在GestureDetector的onFling方法中處理下拉事件,在裡面判斷是否下拉以及GridView是否在最頂端,如果是,顯示progressBar控件並開一個線程來處理刷新,這裡做模擬就睡眠2000毫秒,最後用message對象返回一個消息給Handler,Handler在主線程中更新GridView。

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

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        // Log.e("MainActivity", event.getX()+"");
        return gsDetector.onTouchEvent(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 (e2.getY() - e1.getY() > 0 && gv.getFirstVisiblePosition() == 0) {
        pb.setVisibility(View.VISIBLE);
        Animation animation = new ScaleAnimation(1f, 1f, 0, 1f);
        animation.setDuration(300);
        pb.startAnimation(animation);

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

      }
      return false;
    }

  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved