[android-developers] Re: Get scroll position in a list activity
I think that's what I'm looking for. Can't wait to give it a try after work. Although, I have another thread in my main Activity downloading all pictures. Thanks, I will give it a try. Ivan Soto Fernandez Web Developer http://ivansotof.com On Tue, Mar 24, 2009 at 8:37 AM, Streets Of Boston wrote: > > Here is a code-snippet. I may not compile, but i think you'll get the > idea :-) > > [code] > ExecutorService EXECUTOR = Executors.newFixedThreadPool(3); // max 3 > worker threads. > ... > public View getView(final AbsListView listView, View convertView, int > pos, long id) { > > ... > ... > > final String imgUrl = ... > ... > final ImageView imgView = ... > ... > Bitmap bm = mBitmapCache.get(imgUrl); > if (bm != null) >imgView.setImageBitmap(bm); > else { >imgView.setTag(imgUrl); >FutureTask task = EXECUTOR.submit(new Runnable() { > public void run() { >final Bitmap newBM = getImageFrom(imgUrl); // you have to > write this method >if (newBM == null) > return; > >// be sure that mBitmapCache is thread-safe. >mBitmapCache.put(imgUrl, newBM); > >// instead of 'listView', you could use a new Handler > instance. >listView.post(new Runnable() { > public void run() { > String checkUrl = (String)imgView.getTag(); > if (checkUrl != null && !checkUrl.equals(imgUrl)) >return; > > imgView.setImageBitmap(newBM); > } >}); > } > > private Bitmap getImageFrom(String url) { >// download the data from imgUrl. >// create a bitmap from it. >return bitMap; > } >}); > } > > // if you want, you can hold on to 'task' and call 'cancel' on it if > necessary, e.g. > // when this convertView is about to be re-used and to be assigned > to a different image. > return convertView; > } > [/code] > > On Mar 24, 10:13 am, Streets Of Boston > wrote: > > I've done the same in my apps for ListView (whether they be in > > ListActivity or in a plain Activity) with good success. > > > > I use the java.util.concurrent's ExecutorService to obtain images: > > > > 1. Your getView(..) (or bindView/newView/etc) needs to assign an image/ > > thumbnail (bitmap) to an ImageView. But you don't have the bitmap yet. > > If you do have it (store in a limited size cache), just set it . > > 2. If not, obtain a FutureTask from the ExecutorService and this > > FutureTask then will download the image, create a thumbnail from it > > and creates a Bitmap from this thumbnail. Remember the id of the image > > (can be an id, Uri, URL, whatever, as long as it is unique) and assign > > it to the ImageView (setTag()). > > 3. When ready, the FutureTask will 'post' back to the main-thread that > > it has an new thumbnail. > > 4. On the 'post'-back, loop through the children of ListView, get the > > appropriate ImageView, the one whose tag (getTag()) is equal to the > > one that FutureTask you got the image for, assign the Bitmap to this > > ImageView. This is it. > > > > For myself I created a sub-system of ExecutorService and FutureTask, > > called 'Cancelable' tasks, which make it easier to cancel queued up > > tasks when they're no longer necessary. But this is an optimization. > > > > On Mar 24, 8:06 am, Mark Murphy wrote: > > > > > > > > > Ivan Soto wrote: > > > > Do you have any article/tutorial about the placeholder images to > share? > > > > I'm trying to find one with no luck. > > > > > I have used the technique, but not in code I'm allowed to share. I do > > > need to more formally write this up at some point, but I do not have > > > anything immediately handy. > > > > > The gist of it is that you create your adapter and set it up, in > > > getView() or newView()/bindView() (depending on adapter choice), to see > > > if the thumbnail has been downloaded. If so, use it for the list row > > > being inflated/updated; if not, leave the ImageView in the row pointing > > > to some placeholder Drawable resource. This means as the user scrolls, > > > she will pick up the thumbnails. Also, at the end, you can quickly > > > iterate over the rows (ListView is a ViewGroup, IIRC, so there are > > > methods to iterate its children) and ensure each of those rows' > > > ImageViews are using their associated thumbnails. > > > > > -- > > > Mark Murphy (a Commons Guy)http://commonsware.com > > > Android App Developer Books:http://commonsware.com/books.html- Hide > quoted text - > > > > - Show quoted text - > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~-
[android-developers] Re: Get scroll position in a list activity
Thanks for explaining. Right now I'm using the efficient list method used on the ApiDemos so when the user scrolls it will refreshes the images that didn't load before. But if you don't move anything, even if the image is downloaded it won't redraw in the imageview. I will start reading about that FutureTask/ExecutorService Ivan Soto Fernandez Web Developer http://ivansotof.com On Tue, Mar 24, 2009 at 8:13 AM, Streets Of Boston wrote: > > I've done the same in my apps for ListView (whether they be in > ListActivity or in a plain Activity) with good success. > > I use the java.util.concurrent's ExecutorService to obtain images: > > 1. Your getView(..) (or bindView/newView/etc) needs to assign an image/ > thumbnail (bitmap) to an ImageView. But you don't have the bitmap yet. > If you do have it (store in a limited size cache), just set it . > 2. If not, obtain a FutureTask from the ExecutorService and this > FutureTask then will download the image, create a thumbnail from it > and creates a Bitmap from this thumbnail. Remember the id of the image > (can be an id, Uri, URL, whatever, as long as it is unique) and assign > it to the ImageView (setTag()). > 3. When ready, the FutureTask will 'post' back to the main-thread that > it has an new thumbnail. > 4. On the 'post'-back, loop through the children of ListView, get the > appropriate ImageView, the one whose tag (getTag()) is equal to the > one that FutureTask you got the image for, assign the Bitmap to this > ImageView. This is it. > > For myself I created a sub-system of ExecutorService and FutureTask, > called 'Cancelable' tasks, which make it easier to cancel queued up > tasks when they're no longer necessary. But this is an optimization. > > On Mar 24, 8:06 am, Mark Murphy wrote: > > Ivan Soto wrote: > > > Do you have any article/tutorial about the placeholder images to share? > > > I'm trying to find one with no luck. > > > > I have used the technique, but not in code I'm allowed to share. I do > > need to more formally write this up at some point, but I do not have > > anything immediately handy. > > > > The gist of it is that you create your adapter and set it up, in > > getView() or newView()/bindView() (depending on adapter choice), to see > > if the thumbnail has been downloaded. If so, use it for the list row > > being inflated/updated; if not, leave the ImageView in the row pointing > > to some placeholder Drawable resource. This means as the user scrolls, > > she will pick up the thumbnails. Also, at the end, you can quickly > > iterate over the rows (ListView is a ViewGroup, IIRC, so there are > > methods to iterate its children) and ensure each of those rows' > > ImageViews are using their associated thumbnails. > > > > -- > > Mark Murphy (a Commons Guy)http://commonsware.com > > Android App Developer Books:http://commonsware.com/books.html > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
Here is a code-snippet. I may not compile, but i think you'll get the idea :-) [code] ExecutorService EXECUTOR = Executors.newFixedThreadPool(3); // max 3 worker threads. ... public View getView(final AbsListView listView, View convertView, int pos, long id) { ... ... final String imgUrl = ... ... final ImageView imgView = ... ... Bitmap bm = mBitmapCache.get(imgUrl); if (bm != null) imgView.setImageBitmap(bm); else { imgView.setTag(imgUrl); FutureTask task = EXECUTOR.submit(new Runnable() { public void run() { final Bitmap newBM = getImageFrom(imgUrl); // you have to write this method if (newBM == null) return; // be sure that mBitmapCache is thread-safe. mBitmapCache.put(imgUrl, newBM); // instead of 'listView', you could use a new Handler instance. listView.post(new Runnable() { public void run() { String checkUrl = (String)imgView.getTag(); if (checkUrl != null && !checkUrl.equals(imgUrl)) return; imgView.setImageBitmap(newBM); } }); } private Bitmap getImageFrom(String url) { // download the data from imgUrl. // create a bitmap from it. return bitMap; } }); } // if you want, you can hold on to 'task' and call 'cancel' on it if necessary, e.g. // when this convertView is about to be re-used and to be assigned to a different image. return convertView; } [/code] On Mar 24, 10:13 am, Streets Of Boston wrote: > I've done the same in my apps for ListView (whether they be in > ListActivity or in a plain Activity) with good success. > > I use the java.util.concurrent's ExecutorService to obtain images: > > 1. Your getView(..) (or bindView/newView/etc) needs to assign an image/ > thumbnail (bitmap) to an ImageView. But you don't have the bitmap yet. > If you do have it (store in a limited size cache), just set it . > 2. If not, obtain a FutureTask from the ExecutorService and this > FutureTask then will download the image, create a thumbnail from it > and creates a Bitmap from this thumbnail. Remember the id of the image > (can be an id, Uri, URL, whatever, as long as it is unique) and assign > it to the ImageView (setTag()). > 3. When ready, the FutureTask will 'post' back to the main-thread that > it has an new thumbnail. > 4. On the 'post'-back, loop through the children of ListView, get the > appropriate ImageView, the one whose tag (getTag()) is equal to the > one that FutureTask you got the image for, assign the Bitmap to this > ImageView. This is it. > > For myself I created a sub-system of ExecutorService and FutureTask, > called 'Cancelable' tasks, which make it easier to cancel queued up > tasks when they're no longer necessary. But this is an optimization. > > On Mar 24, 8:06 am, Mark Murphy wrote: > > > > > Ivan Soto wrote: > > > Do you have any article/tutorial about the placeholder images to share? > > > I'm trying to find one with no luck. > > > I have used the technique, but not in code I'm allowed to share. I do > > need to more formally write this up at some point, but I do not have > > anything immediately handy. > > > The gist of it is that you create your adapter and set it up, in > > getView() or newView()/bindView() (depending on adapter choice), to see > > if the thumbnail has been downloaded. If so, use it for the list row > > being inflated/updated; if not, leave the ImageView in the row pointing > > to some placeholder Drawable resource. This means as the user scrolls, > > she will pick up the thumbnails. Also, at the end, you can quickly > > iterate over the rows (ListView is a ViewGroup, IIRC, so there are > > methods to iterate its children) and ensure each of those rows' > > ImageViews are using their associated thumbnails. > > > -- > > Mark Murphy (a Commons Guy)http://commonsware.com > > Android App Developer Books:http://commonsware.com/books.html- Hide quoted > > text - > > - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
I've done the same in my apps for ListView (whether they be in ListActivity or in a plain Activity) with good success. I use the java.util.concurrent's ExecutorService to obtain images: 1. Your getView(..) (or bindView/newView/etc) needs to assign an image/ thumbnail (bitmap) to an ImageView. But you don't have the bitmap yet. If you do have it (store in a limited size cache), just set it . 2. If not, obtain a FutureTask from the ExecutorService and this FutureTask then will download the image, create a thumbnail from it and creates a Bitmap from this thumbnail. Remember the id of the image (can be an id, Uri, URL, whatever, as long as it is unique) and assign it to the ImageView (setTag()). 3. When ready, the FutureTask will 'post' back to the main-thread that it has an new thumbnail. 4. On the 'post'-back, loop through the children of ListView, get the appropriate ImageView, the one whose tag (getTag()) is equal to the one that FutureTask you got the image for, assign the Bitmap to this ImageView. This is it. For myself I created a sub-system of ExecutorService and FutureTask, called 'Cancelable' tasks, which make it easier to cancel queued up tasks when they're no longer necessary. But this is an optimization. On Mar 24, 8:06 am, Mark Murphy wrote: > Ivan Soto wrote: > > Do you have any article/tutorial about the placeholder images to share? > > I'm trying to find one with no luck. > > I have used the technique, but not in code I'm allowed to share. I do > need to more formally write this up at some point, but I do not have > anything immediately handy. > > The gist of it is that you create your adapter and set it up, in > getView() or newView()/bindView() (depending on adapter choice), to see > if the thumbnail has been downloaded. If so, use it for the list row > being inflated/updated; if not, leave the ImageView in the row pointing > to some placeholder Drawable resource. This means as the user scrolls, > she will pick up the thumbnails. Also, at the end, you can quickly > iterate over the rows (ListView is a ViewGroup, IIRC, so there are > methods to iterate its children) and ensure each of those rows' > ImageViews are using their associated thumbnails. > > -- > Mark Murphy (a Commons Guy)http://commonsware.com > Android App Developer Books:http://commonsware.com/books.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
Ivan Soto wrote: > Do you have any article/tutorial about the placeholder images to share? > I'm trying to find one with no luck. I have used the technique, but not in code I'm allowed to share. I do need to more formally write this up at some point, but I do not have anything immediately handy. The gist of it is that you create your adapter and set it up, in getView() or newView()/bindView() (depending on adapter choice), to see if the thumbnail has been downloaded. If so, use it for the list row being inflated/updated; if not, leave the ImageView in the row pointing to some placeholder Drawable resource. This means as the user scrolls, she will pick up the thumbnails. Also, at the end, you can quickly iterate over the rows (ListView is a ViewGroup, IIRC, so there are methods to iterate its children) and ensure each of those rows' ImageViews are using their associated thumbnails. -- Mark Murphy (a Commons Guy) http://commonsware.com Android App Developer Books: http://commonsware.com/books.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
Don't refresh it with setListAdapter! Just modify the content of the adapter instead. On Mar 23, 2009 7:03 PM, "Ivan Soto" wrote: But when I refresh the listview with setlistadapter(listadapter) it returns to the top. Mark: Do you have any article/tutorial about the placeholder images to share? I'm trying to find one with no luck. Thanks for helping. Ivan Soto Fernandez Web Developer http://ivansotof.com On Mon, Mar 23, 2009 at 7:38 PM, Romain Guy wrote: > > > > ListView does not use scrollY. There's no need to save the scroll > > anyway because ListView do... > -- > > > Romain Guy > Android framework engineer > romain...@android.com > > > Note: please don't send priva... > > > --~--~-~--~~~---~--~~ You received this message because you are sub... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
But when I refresh the listview with setlistadapter(listadapter) it returns to the top. Mark: Do you have any article/tutorial about the placeholder images to share? I'm trying to find one with no luck. Thanks for helping. Ivan Soto Fernandez Web Developer http://ivansotof.com On Mon, Mar 23, 2009 at 7:38 PM, Romain Guy wrote: > > ListView does not use scrollY. There's no need to save the scroll > anyway because ListView does it for you. > > On Mon, Mar 23, 2009 at 6:26 PM, Ivan Soto wrote: > > Hi, > > I'm trying to get the scroll position on a ListActivity. Here is what I'm > > trying to do: > > I have a list activity that is populated from an XML file. Another thread > > downloads all pictures so the user can see the list while the thread is > > still downloading the files. > > What I'm trying to do is when the thread finishes getting all pics it > reads > > the listview scroll position, refresh the listview and scroll to that > > position again. > > ListView main = getListView(); <-- not sure if I'm really getting the > > ListView this way. > > int scY = main.getScrollY(); > > Log.d("Scroll", scY + " "); <--- this is printing zero even when at the > > moment I run this I already scrolled the list. > > setListAdapter(listadapter); > > main.scrollTo(0, scY); > > > > Any ideas? > > Thanks! > > > > Ivan Soto Fernandez > > Web Developer > > http://ivansotof.com > > > > > > > > > > > > > -- > Romain Guy > Android framework engineer > romain...@android.com > > Note: please don't send private questions to me, as I don't have time > to provide private support. All such questions should be posted on > public forums, where I and others can see and answer them > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
ListView does not use scrollY. There's no need to save the scroll anyway because ListView does it for you. On Mon, Mar 23, 2009 at 6:26 PM, Ivan Soto wrote: > Hi, > I'm trying to get the scroll position on a ListActivity. Here is what I'm > trying to do: > I have a list activity that is populated from an XML file. Another thread > downloads all pictures so the user can see the list while the thread is > still downloading the files. > What I'm trying to do is when the thread finishes getting all pics it reads > the listview scroll position, refresh the listview and scroll to that > position again. > ListView main = getListView(); <-- not sure if I'm really getting the > ListView this way. > int scY = main.getScrollY(); > Log.d("Scroll", scY + " "); <--- this is printing zero even when at the > moment I run this I already scrolled the list. > setListAdapter(listadapter); > main.scrollTo(0, scY); > > Any ideas? > Thanks! > > Ivan Soto Fernandez > Web Developer > http://ivansotof.com > > > > > -- Romain Guy Android framework engineer romain...@android.com Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Get scroll position in a list activity
Ivan Soto wrote: > I'm trying to get the scroll position on a ListActivity. Here is what > I'm trying to do: > I have a list activity that is populated from an XML file. Another > thread downloads all pictures so the user can see the list while the > thread is still downloading the files. > > What I'm trying to do is when the thread finishes getting all pics it > reads the listview scroll position, refresh the listview and scroll to > that position again. Have you considered using placeholder images instead? Replace the placeholder images with the real ones as they come in. Then, when the background thread finishes, you do not have to do anything -- the images are already loaded. This eliminates the need to "refresh the listview" and any accompanying confusing such a refresh might cause the user. -- Mark Murphy (a Commons Guy) http://commonsware.com Android App Developer Books: http://commonsware.com/books.html --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---