android - How to load dynamic images in custom ListView -
how load dynamic images in custom listview
ii follow tutorial show listview in horizontal http://www.dev-smart.com/archives/34 problem example used same sample icon,but want use dynamic images resource or web parsing
horizontallistviewdemo.java :
public class horizontallistviewdemo extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listviewdemo); horizontiallistview listview = (horizontiallistview) findviewbyid(r.id.listview); listview.setadapter(madapter); } private static string[] dataobjects = new string[]{ "text #1", "text #2", "text #3" }; private baseadapter madapter = new baseadapter() { @override public int getcount() { return dataobjects.length; } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view retval = layoutinflater.from(parent.getcontext()).inflate(r.layout.viewitem, null); textview title = (textview) retval.findviewbyid(r.id.title); title.settext(dataobjects[position]); return retval; } }; }
layout file :
<linearlayout xmlns:android="http://schemas.android.com/apk/res /android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff"> <com.devsmart.android.ui.horizontiallistview android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ddd"> </com.devsmart.android.ui.horizontiallistview> </linearlayout> <linearlayout xmlns:android="http://schemas.android.com /apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#fff"> <imageview android:id="@+id/image" android:layout_width="150dip" android:layout_height="150dip" android:scaletype="centercrop" android:src="@drawable/icon"> <textview android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#000" android:gravity="center_horizontal"> </textview></imageview></linearlayout>
horizontallistview.java
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; 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(); } 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 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(){ @override 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 + child.getpaddingright(); } } } 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) { for(int i=0;i<getchildcount();i++){ view child = getchildat(i); if (iseventwithinview(e, child)) { 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) { int childcount = getchildcount(); (int = 0; < childcount; i++) { view child = getchildat(i); if (iseventwithinview(e, child)) { if (monitemlongclicked != null) { monitemlongclicked.onitemlongclick(horizontallistview.this, child, mleftviewindex + 1 + i, madapter.getitemid(mleftviewindex + 1 + i)); } break; } } } private boolean iseventwithinview(motionevent e, view child) { rect viewrect = new rect(); int[] childposition = new int[2]; child.getlocationonscreen(childposition); int left = childposition[0]; int right = left + child.getwidth(); int top = childposition[1]; int bottom = top + child.getheight(); viewrect.set(left, top, right, bottom); return viewrect.contains((int) e.getrawx(), (int) e.getrawy()); } }; }
take @ link below. have written library it.
android universal image loader
i used this library in recent projects , worked flawlessly.
hope help.
Comments
Post a Comment