The GridView is a ListView-like control, except that the GridView can use more than one column to render the content, whereas ListView is in the behavior unit, so the usage is almost the same.
The main layout file, because you want to do a drop-down refresh, so add a Progressbar,gridview numcolumns attribute is the number of columns per row
<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>
Each item layout file, here a relatively simple picture with a paragraph of text
<?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:o" rientation= "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>
Main Activity code:
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 (msg) {switch (Msg.wha
T) {case 1:pb.setvisibility (view.gone);
Toast.maketext (Mainactivity.this, "refresh Success"). 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.i
Tem_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 < i++) {map<string, object> Map = new hashmap<string, object> ();
Map.put ("image", r.drawable.gift_item_default);
Map.put ("Text", "an ice-ying pig");
List.add (map); } class Mlistener implements Ongesturelistener {@Override public boolean ondown (Motionevent e) {RE
Turn false; @Override public void onshowpress (Motionevent e) {} @Override public boolean onsingletapup (motio
Nevent 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.getf
Irstvisibleposition () = = 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);
msg = new Message ();
Msg.what = 1;
Handler.sendmessage (msg);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
}}). Start ();
return false;
}
}
}
Resolution:
In the OnCreate method to get two components first, and then set the ProgressBar to hide, Drop-down time to display, refresh and then hide. Then set the data source for the GridView, where it is convenient to use Simpleadapter, and then set the Ontouchlistener for the GridView, In the Ontouch method, the touch event is given to our custom Gesturedetector object to handle, and the Drop-down event is handled in the Gesturedetector onfling method, which determines whether the dropdown and the GridView are at the top, and if so, Displays the ProgressBar control and opens a thread to handle the refresh, where the simulation sleeps for 2000 milliseconds, and finally returns a message to the Handler,handler in the main thread to update the 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) { c16/>//log.e ("Mainactivity", Event.getx () + "");
Return Gsdetector.ontouchevent (event);
}
);
Class Mlistener implements Ongesturelistener {@Override public boolean ondown (Motionevent e) {return FAL
Se @Override public void onshowpress (Motionevent e) {} @Override public boolean onsingletapup (motio
Nevent 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.getf
Irstvisibleposition () = = 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);
msg = new Message ();
Msg.what = 1;
Handler.sendmessage (msg);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
}}). Start ();
return false;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.