gridview - Pull to refresh in Android without popular library -
i want implement pull-to-refresh in android app, don't want use pull-to-refresh library available on internet because slow gridview using. want implement hand, know how this? or methods should use gridview?
pullrefreshcontainerview.java
import android.content.context; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; /** * container listview can pulled refresh. * create listview , refresh header automatically, can * customize them using {@link #setlist(listview)} , {@link #setrefreshheader(view, int)} * <p> * use, put put listview. since not extend * listview, must use {@link #getlist()} modify list or provide own. * <p> * actions of list, use {@link onchangestatelistener} {@link #setonchangestatelistener(onchangestatelistener)}. * if want change how refresh header looks, should during these state changes. */ public class pullrefreshcontainerview extends linearlayout { /** * interface listening when refresh container changes state. */ public interface onchangestatelistener { /** * notifies listener when refresh view's state changes. * @param container container contains header * @param state state of header. may state_idle, state_ready, * or state_refreshing. */ public void onchangestate(pullrefreshcontainerview container, int state); } /** * state of refresh header when doing nothing or being pulled down slightly. */ public static final int state_idle = 0; /** * state of refresh header when has been pulled down not enough start refreshing, , * has not yet been released. */ public static final int state_pull = 1; /** * state of refresh header when has been pulled down enough start refreshing, * has not yet been released. */ public static final int state_release = 2; /** * state of refresh header when list should refreshing. */ public static final int state_loading = 3; private linearlayout mheadercontainer; private view mheaderview; private listview mlist; private int mstate; private onchangestatelistener monchangestatelistener; private int refresh_view_height = 60; /** * creates new pull refresh container. * * @param context application context */ public pullrefreshcontainerview(context context) { super(context); init(context); } /** * creates new pull refresh container. * * @param context application context * @param attrs xml attribute set */ public pullrefreshcontainerview(context context, attributeset attrs) { super(context, attrs); init(context); } /** * creates new pull refresh container. * * @param context application context * @param attrs xml attribute set * @param defstyle style view */ public pullrefreshcontainerview(context context, attributeset attrs, int defstyle) { super(context, attrs); init(context); } private void init(context context) { mstate = state_idle; // start out idle. float densityfactor = context.getresources().getdisplaymetrics().density; refresh_view_height *= densityfactor; // don't want see fading edge on container. setverticalfadingedgeenabled(false); setverticalscrollbarenabled(false); setorientation(linearlayout.vertical); // set default list , header. mheadercontainer = new linearlayout(context); addview(mheadercontainer); setrefreshviewheight(1); textview headerview = new textview(context); headerview.settext("default refresh header."); setrefreshheader(headerview); listview list = new listview(context); setlist(list); } private boolean mscrollinglist = true; private float mintercepty; private int mlastmotiony; @override public boolean dispatchtouchevent (motionevent ev) { float oldlasty = mintercepty; mintercepty = ev.gety(); if (mstate == state_loading) { return super.dispatchtouchevent(ev); } switch (ev.getaction()) { case motionevent.action_down: mlastmotiony = (int) ev.gety(); mscrollinglist = true; return super.dispatchtouchevent(ev); case motionevent.action_move: if (mlist.getfirstvisibleposition() == 0 && (mlist.getchildcount() == 0 || mlist.getchildat(0).gettop() == 0)) { if ((mintercepty - oldlasty > 5) || (mstate == state_pull) || (mstate == state_release)) { mscrollinglist = false; applyheaderheight(ev); return true; } else { mscrollinglist = true; return super.dispatchtouchevent(ev); } } else if (mscrollinglist) { return super.dispatchtouchevent(ev); } else { return super.dispatchtouchevent(ev); } case motionevent.action_up: case motionevent.action_cancel: if (mstate == state_release) { refresh(); } else { changestate(state_idle); } if (mscrollinglist) { return super.dispatchtouchevent(ev); } else { return true; } default: return super.dispatchtouchevent(ev); } } private void applyheaderheight(motionevent ev) { final int historysize = ev.gethistorysize(); if (historysize > 0) { (int h = 0; h < historysize; h++) { int historicaly = (int) (ev.gethistoricaly(h)); updaterefreshview(historicaly - mlastmotiony); } } else { int historicaly = (int) ev.gety(); updaterefreshview(historicaly - mlastmotiony); } } private void updaterefreshview(int height) { if (height <= 0) { return; } if ((refresh_view_height/4 <= mcurrefreshviewheight) && (mcurrefreshviewheight < refresh_view_height)) { setrefreshviewheight(height); changestate(state_pull); } else if (mcurrefreshviewheight >= refresh_view_height) { if (height > refresh_view_height) { height = (int) (refresh_view_height + (height - refresh_view_height) * refresh_view_height * 1.0f/height); } setrefreshviewheight(height); changestate(state_release); } else { setrefreshviewheight(height); } } private int mcurrefreshviewheight = 60; private void setrefreshviewheight(int height) { if (mcurrefreshviewheight == height) { return; } if (height == 1) { mheadercontainer.setlayoutparams(new layoutparams(1, 1)); } else { mheadercontainer.setlayoutparams(new layoutparams(layoutparams.match_parent, height)); } mcurrefreshviewheight = height; } private void changestate(int state) { switch (state) { case state_idle: setrefreshviewheight(1); break; case state_pull: break; case state_release: break; case state_loading: setrefreshviewheight(refresh_view_height); break; } mstate = state; notifystatechanged(); } /** * sets list used in pull refresh container. * @param list list use */ public void setlist(listview list) { if (mlist != null) { removeview(mlist); } mlist = list; if (mlist.getparent() != null) { viewgroup parent = (viewgroup) mlist.getparent(); parent.removeview(mlist); } mlist.setlayoutparams(new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent)); addview(mlist); } /** * @return list inside pull refresh container */ public listview getlist() { return mlist; } /** * sets view use refresh header. * <p /> * header view view @ top show while list * refreshing. usually, simple rectangle says "refreshing" , like. * <p /> * * @param headerview view use whole header. */ public void setrefreshheader(view header) { if (mheaderview != null) { mheadercontainer.removeview(mheaderview); } if (header == null) { throw new runtimeexception("please supply non-null header container."); } mheadercontainer.addview(header, 0); mheaderview = header; } public void refresh() { changestate(state_loading); } /** * notifies pull-to-refresh view refreshing complete. * hide refreshing header. */ public void completerefresh() { changestate(state_idle); } /** * notifies listener state has changed. */ private void notifystatechanged() { if (monchangestatelistener != null) { monchangestatelistener.onchangestate(this, mstate); } } /** * @param listener listener notified when header state should change */ public void setonchangestatelistener(onchangestatelistener listener) { monchangestatelistener = listener; } }
how use usagedemoactivity.java
import java.util.arraylist; import java.util.timer; import java.util.timertask; import com.dmobile.pulltorefresh.pullrefreshcontainerview.onchangestatelistener; import com.dmobile.pulltorefresh.r; import android.app.activity; import android.os.bundle; import android.view.gravity; import android.view.viewgroup.layoutparams; import android.widget.arrayadapter; import android.widget.listview; import android.widget.textview; public class usagedemoactivity extends activity { private pullrefreshcontainerview mcontainerview; private textview mrefreshheader; private listview mlist; private arraylist<string> mstrings = new arraylist<string>(); private arrayadapter<string> madapter; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mrefreshheader = new textview(this); mrefreshheader.setlayoutparams(new layoutparams(layoutparams.match_parent, layoutparams.match_parent)); mrefreshheader.setgravity(gravity.center); mrefreshheader.settext("pull refresh..."); mcontainerview = (pullrefreshcontainerview) findviewbyid(r.id.container); mcontainerview.setrefreshheader(mrefreshheader); mcontainerview.setonchangestatelistener(new onchangestatelistener() { @override public void onchangestate(pullrefreshcontainerview container, int state) { switch (state) { case pullrefreshcontainerview.state_idle: case pullrefreshcontainerview.state_pull: mrefreshheader.settext("pull refresh..."); break; case pullrefreshcontainerview.state_release: mrefreshheader.settext("release refresh..."); break; case pullrefreshcontainerview.state_loading: mrefreshheader.settext("loading..."); final timer t = new timer(); t.schedule(new timertask() { @override public void run() { usagedemoactivity.this.runonuithread(new runnable() { @override public void run() { addstrings(1); mcontainerview.completerefresh(); t.cancel(); } }); } }, 5000, 5000); break; } } }); mlist = mcontainerview.getlist(); madapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, mstrings); mlist.setadapter(madapter); addstrings(3); } private void addstrings(int count) { int cursize = mstrings.size(); (int = 0; < count; ++i) { mstrings.add("string " + (cursize + i)); } madapter.notifydatasetchanged(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.dmobile.pulltorefresh.pullrefreshcontainerview android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </linearlayout>
update work gridview pullrefreshcontainerview.java
private void init(context context) { mstate = state_idle; // start out idle. float densityfactor = context.getresources().getdisplaymetrics().density; refresh_view_height *= densityfactor; // don't want see fading edge on container. setverticalfadingedgeenabled(false); setverticalscrollbarenabled(false); setorientation(linearlayout.vertical); // set default list , header. mheadercontainer = new linearlayout(context); addview(mheadercontainer); setrefreshviewheight(1); textview headerview = new textview(context); headerview.settext("default refresh header."); setrefreshheader(headerview); gridview grid = new gridview(context); setlist(grid); }
and make related change in setlistmethod();
Comments
Post a Comment