亲宝软件园·资讯

展开

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

Fndroid 人气:0
想了解Android下拉刷新以及GridView使用方法详解的相关内容吗,Fndroid在本文为您仔细讲解Android下拉刷新以及GridView使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,下拉刷新,GridView,下面大家一起来学习吧。

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;
    }

  }

加载全部内容

相关教程
猜你喜欢
用户评论