Android下拉刷新以及GridView使用方法详解
程序员文章站
2024-03-05 13:29:42
gridview是类似于listview的控件,只是gridview可以使用多个列来呈现内容,而listview是以行为单位,所以用法上是差不多的。
主布局文件,因为要做...
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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Groovy编程入门攻略
下一篇: Java 泛型有哪些好处详解
推荐阅读
-
Android下拉刷新以及GridView使用方法详解
-
Android官方下拉刷新控件SwipeRefreshLayout使用详解
-
Android开源项目PullToRefresh下拉刷新功能详解2
-
Android下拉刷新以及GridView使用方法详解
-
Android下拉刷新SwipeRefreshLayout控件使用方法
-
Android下拉刷新SwipeRefreshLayout控件使用方法
-
Android使用RecyclerView实现自定义列表、点击事件以及下拉刷新
-
Android开发之无痕过渡下拉刷新控件的实现思路详解
-
Android使用RecyclerView实现自定义列表、点击事件以及下拉刷新
-
Android开发之无痕过渡下拉刷新控件的实现思路详解