Issues Implementing Android fling gesture -
i'm attempting implement fling gesture. have same function native android contact app, you'll swipe listview , it'll open dialer. i'm having issue getting 100% though. of listview loads should, after googling, , tutorials i've come , need help.
simplegesturefilter
public final static int swipe_up = 1; public final static int swipe_down = 2; public final static int swipe_left = 3; public final static int swipe_right = 4; public final static int mode_transparent = 0; public final static int mode_solid = 1; public final static int mode_dynamic = 2; private final static int action_fake = -13; //just unlikely number private int swipe_min_distance = 100; private int swipe_max_distance = 350; private int swipe_min_velocity = 100; private int mode = mode_dynamic; private boolean running = true; private boolean tapindicator = false; private activity context; private gesturedetector detector; private simplegesturelistener listener; public simplegesturefilter(activity context,simplegesturelistener sgl) { this.context = context; this.detector = new gesturedetector(context, this); this.listener = sgl; } public void ontouchevent(motionevent event){ if(!this.running) return; boolean result = this.detector.ontouchevent(event); if(this.mode == mode_solid) event.setaction(motionevent.action_cancel); else if (this.mode == mode_dynamic) { if(event.getaction() == action_fake) event.setaction(motionevent.action_up); else if (result) event.setaction(motionevent.action_cancel); else if(this.tapindicator){ event.setaction(motionevent.action_down); this.tapindicator = false; } } //else nothing, it's transparent } public void setmode(int m){ this.mode = m; } public int getmode(){ return this.mode; } public void setenabled(boolean status){ this.running = status; } public void setswipemaxdistance(int distance){ this.swipe_max_distance = distance; } public void setswipemindistance(int distance){ this.swipe_min_distance = distance; } public void setswipeminvelocity(int distance){ this.swipe_min_velocity = distance; } public int getswipemaxdistance(){ return this.swipe_max_distance; } public int getswipemindistance(){ return this.swipe_min_distance; } public int getswipeminvelocity(){ return this.swipe_min_velocity; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { final float xdistance = math.abs(e1.getx() - e2.getx()); final float ydistance = math.abs(e1.gety() - e2.gety()); if(xdistance > this.swipe_max_distance || ydistance > this.swipe_max_distance) return false; velocityx = math.abs(velocityx); velocityy = math.abs(velocityy); boolean result = false; if(velocityx > this.swipe_min_velocity && xdistance > this.swipe_min_distance){ if(e1.getx() > e2.getx()) // right left this.listener.onswipe(swipe_left); else this.listener.onswipe(swipe_right); result = true; } else if(velocityy > this.swipe_min_velocity && ydistance > this.swipe_min_distance){ if(e1.gety() > e2.gety()) // bottom this.listener.onswipe(swipe_up); else this.listener.onswipe(swipe_down); result = true; } return result; } @override public boolean onsingletapup(motionevent e) { this.tapindicator = true; return false; } @override public boolean ondoubletap(motionevent arg0) { this.listener.ondoubletap();; return true; } @override public boolean ondoubletapevent(motionevent arg0) { return true; } @override public boolean onsingletapconfirmed(motionevent arg0) { if(this.mode == mode_dynamic){ // owe action_up, fake arg0.setaction(action_fake); //action converted action_up later. this.context.dispatchtouchevent(arg0); } return false; } static interface simplegesturelistener{ void onswipe(int direction); void ondoubletap(); } }
displaycompanies.java
public class displaycompanies extends listactivity { //jsonarrays? jsonarray directory; private simplegesturefilter detector; //json node names private static string tag_id = "id"; private static string tag_company= "organization"; private static string tag_phone= "number"; private static string tag_category= "companies"; private final static string url= "api url base here"; jsonobject json; companylistjsonparser jparser = new companylistjsonparser(); arraylist<hashmap<string, string>> directorylist; @suppresslint("newapi") protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.service); directorylist = new arraylist<hashmap<string, string>>(); request request = new request(); request.execute(); // make sure we're running on honeycomb or higher use actionbar apis if (build.version.sdk_int >= build.version_codes.honeycomb) { // show button in action bar. getactionbar().setdisplayhomeasupenabled(true); settitle("available companies"); } }// end of oncreate method @suppresswarnings("unused") public class request extends asynctask<string, void, jsonobject> { private static final int registration_timeout = 3 * 1000; private static final int wait_timeout = 30 * 1000; private progressdialog dialog = new progressdialog(displaycompanies.this); protected void onpreexecute() { dialog = new progressdialog(displaycompanies.this); dialog.setmessage("getting available companies... please wait..."); dialog.show(); } protected jsonobject doinbackground(string... params) { string cat = null; bundle extras = getintent().getextras(); if (extras != null) { cat = extras.getstring("catid"); } string fullurl = url + cat; json = jparser.getjsonfromurl(fullurl); return json; } protected void onpostexecute(jsonobject s) { super.onpostexecute(s); string name = null; string id = null; string phone = null; dialog.dismiss(); try { directory = s.getjsonarray(tag_category); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } for(int = 0; < directory.length(); i++){; try { id = directory.getjsonobject(i).getstring(tag_id); name = directory.getjsonobject(i).getstring(tag_company); phone = directory.getjsonobject(i).getstring(tag_phone); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } displaycatlist(id, name, phone); } } } public void displaycatlist(string id, string name, string phone){ //create new hashmap hashmap<string,string> map = new hashmap<string, string>(); //add each child node hashmap key map.put(tag_id, id); map.put(tag_company, name); map.put(tag_phone, phone); //adding hashlist arrarlist directorylist.add(map); mysimpleadapter adapter = new mysimpleadapter(this, r.layout.list_item, android.r.id.text1, directorylist); setlistadapter(adapter); adapter.notifydatasetchanged(); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); } public class mysimpleadapter extends arrayadapter<hashmap<string, string>> { list<hashmap<string, string>> listitems; public mysimpleadapter(context context, int resource, int textviewresourceid, list<hashmap<string, string>> objects) { super(context, resource, textviewresourceid, objects); listitems = objects; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub if(convertview == null) { layoutinflater inflator = (layoutinflater) getsystemservice(context.layout_inflater_service); convertview = inflator.inflate(r.layout.list_item, null); } textview listname = (textview) convertview.findviewbyid(r.id.listname); listname.settag(listitems.get(position).get(tag_id)); listname.settext(listitems.get(position).get(tag_company)); //for use onclick listname.setonclicklistener(new onclicklistener() { @override /** * method provides action when selected item in list view clicked. */ public void onclick(view v){ log.i("click", "id # " + listitems.get(position).get(tag_id) + " company: " + listitems.get(position).get(tag_company)+ " phone #: " + listitems.get(position).get(tag_phone)); //intent displaycompanies = new intent (displayserviceactivity.this, displaycompanies.class); //displayserviceactivity.this.startactivity(displaycompanies); } }); //gesture or fling implementation detector = new simplegesturefilter(this,this); return convertview; } public boolean dispatchtouchevent(motionevent me){ detector.ontouchevent(me); return super.dispatchtouchevent(me); } public void onswipe(int direction) { string str = ""; switch (direction) { case simplegesturefilter.swipe_right : str = "swipe right"; break; case simplegesturefilter.swipe_left : str = "swipe left"; break; case simplegesturefilter.swipe_down : str = "swipe down"; break; case simplegesturefilter.swipe_up : str = "swipe up"; break; } toast.maketext(this, str, toast.length_short).show(); } public void ondoubletap() { toast.maketext(this, "double tap", toast.length_short).show(); } } } }
at point eclipse won't compile , these underlined:
public boolean dispatchtouchevent(motionevent me){ detector.ontouchevent(me); return super.dispatchtouchevent(me); }
because: method dispatchtouchevent(motionevent) undefined type arrayadapter>
and
toast.maketext(this, "double tap", toast.length_short).show();
because: method maketext(context, charsequence, int) in type toast not applicable arguments (displaycompanies.mysimpleadapter, string, int)
any on great. thank guys in advance!
you keep making new gesture filters , assigning them on , on same thing code.
//gesture or fling implementation detector = new simplegesturefilter(this,this);
you should make gesture detector each view, instead of 1 listview
Comments
Post a Comment