java - Horizontal scroll view scrolling one item at time -
i implementing panel include horizontal list view using horizontal scroll view . in need control scrolling of horizontal scroll view , scrolling 1 item @ time.
could 1 give me idea kind of thing.
i manged using overriding horizontalscrollview , merging code got various internet examples .it working fine .
i have used gesturedetector , onfling method achieve task.
public void setcenter(int index) { viewgroup parent = (viewgroup) getchildat(0); view preview = parent.getchildat(previndex); //preview.setbackgroundcolor(color.parsecolor("#64cbd8")); android.widget.linearlayout.layoutparams lp = new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); lp.setmargins(5, 0, 5, 0); preview.setlayoutparams(lp); view view = parent.getchildat(index); //view.setbackgroundcolor(color.red); int screenwidth = ((activity) context).getwindowmanager() .getdefaultdisplay().getwidth(); int scrollx = (view.getleft() - (screenwidth / 2)) + (view.getwidth() / 2); this.smoothscrollto(scrollx, 0); previndex = index; log.d("item", string.valueof(previndex)); } @override public boolean ondown(motionevent e) { // todo auto-generated method stub return false; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { if (flingdisable) return false; boolean returnvalue = false; float ptx1 = 0, ptx2 = 0; if (e1 == null || e2 == null) return false; ptx1 = e1.getx(); ptx2 = e2.getx(); // right left if (ptx1 - ptx2 > swipe_min_distance && math.abs(velocityx) > swipe_threshold_velocity) { if (activeitem < maxitem - 1) activeitem = activeitem + 1; returnvalue = true; } else if (ptx2 - ptx1 > swipe_min_distance && math.abs(velocityx) > swipe_threshold_velocity) { if (activeitem > 0) activeitem = activeitem - 1; returnvalue = true; } //scrollto = activeitem * itemwidth; //this.smoothscrollto(0, scrollto); setcenter(activeitem); return returnvalue; } @override public boolean ontouch(view v, motionevent event) { if (gesturedetector.ontouchevent(event)) { return true; } boolean returnvalue = gesturedetector.ontouchevent(event); int x = (int) event.getrawx(); switch (event.getaction()) { case motionevent.action_move: if (start) { this.prevscrollx = x; start = false; } break; case motionevent.action_up: start = true; this.currentscrollx = x; int minfactor = itemwidth / swipe_page_on_factor; if ((this.prevscrollx - this.currentscrollx) > minfactor) { if (activeitem < maxitem - 1) activeitem = activeitem + 1; } else if ((this.currentscrollx - this.prevscrollx) > minfactor) { if (activeitem > 0) activeitem = activeitem - 1; } log.d("item", string.valueof(activeitem)); setcenter(activeitem); returnvalue = true; break; } return returnvalue; }
Comments
Post a Comment