android - HorizontalListview not scrolling -
my horizontallistview not scroll me please solution of previous post how load dynamic images in custom listview horizontallistview class display image problem not scrolling if add 3 images 1 &half display how add scrol view in code me please. every thing work fine horizontal listview not scroll
import android.content.context; import android.database.datasetobserver; import android.graphics.rect; import android.util.attributeset; import android.util.log; import android.view.gesturedetector; import android.view.gesturedetector.ongesturelistener; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.widget.adapterview; import android.widget.listadapter; import android.widget.scroller; public class horizontallistview extends adapterview<listadapter> { public boolean malwaysoverridetouch = true; protected listadapter madapter; private int mleftviewindex = -1; private int mrightviewindex = 0; protected int mcurrentx; protected int mnextx; private int mmaxx = integer.max_value; private int mdisplayoffset = 0; protected scroller mscroller; private gesturedetector mgesture; private queue<view> mremovedviewqueue = new linkedlist<view>(); private onitemselectedlistener monitemselected; private onitemclicklistener monitemclicked; private onitemlongclicklistener monitemlongclicked; private boolean mdatachanged = false; private boolean mmeasureheightforvisibleonly = true; public horizontallistview(context context, attributeset attrs) { super(context, attrs); initview(); } private synchronized void initview() { mleftviewindex = -1; mrightviewindex = 0; mdisplayoffset = 0; mcurrentx = 0; mnextx = 0; mmaxx = integer.max_value; mscroller = new scroller(getcontext()); mgesture = new gesturedetector(getcontext(), mongesture); } @override public void setonitemselectedlistener(adapterview.onitemselectedlistener listener) { monitemselected = listener; } @override public void setonitemclicklistener(adapterview.onitemclicklistener listener){ monitemclicked = listener; } @override public void setonitemlongclicklistener(adapterview.onitemlongclicklistener listener) { monitemlongclicked = listener; } private datasetobserver mdataobserver = new datasetobserver() { @override public void onchanged() { synchronized(horizontallistview.this){ mdatachanged = true; } invalidate(); requestlayout(); } @override public void oninvalidated() { reset(); invalidate(); requestlayout(); } }; @override public listadapter getadapter() { return madapter; } @override public view getselectedview() { //todo: implement return null; } @override public void setadapter(listadapter adapter) { if(madapter != null) { madapter.unregisterdatasetobserver(mdataobserver); } madapter = adapter; madapter.registerdatasetobserver(mdataobserver); reset(); } /** * @param visibleonly - if set true, height calculated * using visible views. if set false height * calculated using _all_ views in adapter. default true. * careful when passing false, may result in * significant performance hit larger number of views. */ public void setheightmeasuremode(boolean visibleonly) { if(mmeasureheightforvisibleonly != visibleonly) { mmeasureheightforvisibleonly = visibleonly; requestlayout(); } } private synchronized void reset(){ initview(); removeallviewsinlayout(); requestlayout(); } @override public void setselection(int position) { //todo: implement } private void addandmeasurechild(final view child, int viewpos) { layoutparams params = child.getlayoutparams(); if(params == null) { params = new layoutparams(layoutparams.fill_parent, layoutparams.fill_parent); } addviewinlayout(child, viewpos, params, true); child.measure(measurespec.makemeasurespec(getwidth(), measurespec.at_most), measurespec.makemeasurespec(getheight(), measurespec.at_most)); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); if(measurespec.getmode(heightmeasurespec) != measurespec.exactly) { int height = 0; if(mmeasureheightforvisibleonly) { int childcount = getchildcount(); for(int = 0; < childcount; i++) { view v = getchildat(i); v.measure(measurespec.unspecified, measurespec.unspecified); if(v.getmeasuredheight() > height) { height = v.getmeasuredheight(); } } } else { /* traverses _all_ views! bypasses view recycler! */ hashmap<integer, view> mrecycler = new hashmap<integer, view>(); int childcount = getadapter().getcount(); for(int = 0; < childcount; i++) { int type = getadapter().getitemviewtype(i); view convertview = mrecycler.get(type); view v = getadapter().getview(i, convertview, this); mrecycler.put(type, v); v.measure(measurespec.unspecified, measurespec.unspecified); if(v.getmeasuredheight() > height) { height = v.getmeasuredheight(); } } } if(measurespec.getmode(heightmeasurespec) == measurespec.at_most) { int maxheight = measurespec.getsize(heightmeasurespec); if(maxheight < height) { height = maxheight; } } setmeasureddimension(getmeasuredwidth(), height); } } @override protected synchronized void onlayout(boolean changed, int left, int top, int right, int bottom) { super.onlayout(changed, left, top, right, bottom); if(madapter == null){ return; } if(mdatachanged){ int oldcurrentx = mcurrentx; initview(); removeallviewsinlayout(); mnextx = oldcurrentx; mdatachanged = false; } if(mscroller.computescrolloffset()){ int scrollx = mscroller.getcurrx(); mnextx = scrollx; } if(mnextx <= 0){ mnextx = 0; mscroller.forcefinished(true); } if(mnextx >= mmaxx) { mnextx = mmaxx; mscroller.forcefinished(true); } int dx = mcurrentx - mnextx; removenonvisibleitems(dx); filllist(dx); positionitems(dx); mcurrentx = mnextx; if(!mscroller.isfinished()){ post(new runnable(){ public void run() { requestlayout(); } }); } } private void filllist(final int dx) { int edge = 0; view child = getchildat(getchildcount()-1); if(child != null) { edge = child.getright(); } filllistright(edge, dx); edge = 0; child = getchildat(0); if(child != null) { edge = child.getleft(); } filllistleft(edge, dx); } private void filllistright(int rightedge, final int dx) { while(rightedge + dx < getwidth() && mrightviewindex < madapter.getcount()) { view child = madapter.getview(mrightviewindex, mremovedviewqueue.poll(), this); addandmeasurechild(child, -1); rightedge += child.getmeasuredwidth(); if(mrightviewindex == madapter.getcount()-1) { mmaxx = mcurrentx + rightedge - getwidth(); } if (mmaxx < 0) { mmaxx = 0; } mrightviewindex++; } } private void filllistleft(int leftedge, final int dx) { while(leftedge + dx > 0 && mleftviewindex >= 0) { view child = madapter.getview(mleftviewindex, mremovedviewqueue.poll(), this); addandmeasurechild(child, 0); leftedge -= child.getmeasuredwidth(); mleftviewindex--; mdisplayoffset -= child.getmeasuredwidth(); } } private void removenonvisibleitems(final int dx) { view child = getchildat(0); while(child != null && child.getright() + dx <= 0) { mdisplayoffset += child.getmeasuredwidth(); mremovedviewqueue.offer(child); removeviewinlayout(child); mleftviewindex++; child = getchildat(0); } child = getchildat(getchildcount()-1); while(child != null && child.getleft() + dx >= getwidth()) { mremovedviewqueue.offer(child); removeviewinlayout(child); mrightviewindex--; child = getchildat(getchildcount()-1); } } private void positionitems(final int dx) { if(getchildcount() > 0){ mdisplayoffset += dx; int left = mdisplayoffset; for(int i=0;i<getchildcount();i++){ view child = getchildat(i); int childwidth = child.getmeasuredwidth(); child.layout(left, 0, left + childwidth, child.getmeasuredheight()); left += childwidth; } } } public synchronized void scrollto(int x) { mscroller.startscroll(mnextx, 0, x - mnextx, 0); requestlayout(); } @override public boolean dispatchtouchevent(motionevent ev) { boolean handled = super.dispatchtouchevent(ev); handled |= mgesture.ontouchevent(ev); return handled; } protected boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { synchronized(horizontallistview.this){ mscroller.fling(mnextx, 0, (int)-velocityx, 0, 0, mmaxx, 0, 0); } requestlayout(); return true; } protected boolean ondown(motionevent e) { mscroller.forcefinished(true); return true; } private ongesturelistener mongesture = new gesturedetector.simpleongesturelistener() { @override public boolean ondown(motionevent e) { return horizontallistview.this.ondown(e); } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { return horizontallistview.this.onfling(e1, e2, velocityx, velocityy); } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { synchronized(horizontallistview.this){ mnextx += (int)distancex; } requestlayout(); return true; } @override public boolean onsingletapconfirmed(motionevent e) { rect viewrect = new rect(); for(int i=0;i<getchildcount();i++){ view child = getchildat(i); int left = child.getleft(); int right = child.getright(); int top = child.gettop(); int bottom = child.getbottom(); viewrect.set(left, top, right, bottom); if(viewrect.contains((int)e.getx(), (int)e.gety())){ if(monitemclicked != null){ monitemclicked.onitemclick(horizontallistview.this, child, mleftviewindex + 1 + i, madapter.getitemid( mleftviewindex + 1 + )); } if(monitemselected != null){ monitemselected.onitemselected(horizontallistview.this, child, mleftviewindex + 1 + i, madapter.getitemid( mleftviewindex + 1 + )); } break; } } return true; } @override public void onlongpress(motionevent e) { rect viewrect = new rect(); int childcount = getchildcount(); (int = 0; < childcount; i++) { view child = getchildat(i); int left = child.getleft(); int right = child.getright(); int top = child.gettop(); int bottom = child.getbottom(); viewrect.set(left, top, right, bottom); if (viewrect.contains((int) e.getx(), (int) e.gety())) { if (monitemlongclicked != null) { monitemlongclicked.onitemlongclick(horizontallistview.this, child, mleftviewindex + 1 + i, madapter.getitemid(mleftviewindex + 1 + i)); } break; } } } }; } <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <imageview android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#fff" android:src="@drawable/icon" /> <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#000" android:gravity="center_horizontal" /> </linearlayout> <com.schoollunchapp.horizontallistview android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="0.6" android:background="#eeeeee"> </com.schoollunchapp.horizontallistview>
@override public boolean dispatchtouchevent(motionevent ev) { boolean handled = super.dispatchtouchevent(ev); handled |= mgesture.ontouchevent(ev); //this true during scrolling return handled; //returning true stop listview functions scroll }
@override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { synchronized(horizontallistview.this){ mnextx += (int)distancex; } requestlayout(); return true; }
this above code problem
according me listview won't scroll if return true in dispatchtouchevent function, true while scrolling in case. onscroll gesture returns true , listview never scrolls.
you trying perform scrolling manually. try returning false , don't handle scrolling manually.
i can't read complete code because long, have spotted problem you, can handle way want.
Comments
Post a Comment