1

Problem: ListView Cannot Scroll To Bottom when Image(s) are loaded using AsynTask.

Details: I want to scroll the listView to bottom when user opens the listView. The listview contains both images and text. For better memory managemt, i am using the method suggested here, which shows a small icon when the bitmap is loading.

In order to scroll to bottom, I used"

`listview.setStackFromBottom(true);`

and:

listView.setSelection(adapter.getCount()-1);

However, for images listview, it just can't scroll to bottom. Is there any solutions for this issue?

enter image description here

Michael
  • 3,308
  • 5
  • 24
  • 36
Antoine Murion
  • 773
  • 1
  • 14
  • 26
  • Try to add `android:stackFromBottom="true"` and `android:transcriptMode="alwaysScroll"` in your litview layout – Rami Apr 18 '15 at 09:10

3 Answers3

1

your AsyncTask onPostExecute should look something like this

@Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if(adapter!=null && commentList.size()>0){
            adapter=new ImageAdapter((Activity) activity, imageList);
            listView.setAdapter(adapter);
            scrollListViewToBottom();
        }
    }

and then the method scrollListViewToBottom()

    private void scrollListViewToBottom() {
    listView.post(new Runnable() {
        @Override
        public void run() {
            // Select the last row so it will scroll into view...
            listView.setSelection(adapter.getCount());
        }
    });
}

and you are done :)

Jeffy Lazar
  • 1,903
  • 13
  • 20
  • I don't use the ImageAdapter. Instead, the getView of adapter will get the imagepath and i use a new AsyncTask to load it into bitmap. I have thought of using your method but i don't know how to know when my last AsyncTask is finished.... – Antoine Murion Apr 18 '15 at 10:09
  • 1) There is no such thing as ImageAdapter, it was only for your reference. This can work with any adapter (Baseadapter, Arrayadapter) 2) onPostExecute method of asynctask you can check (this)[http://stackoverflow.com/questions/20492641/get-the-result-from-async-task] – Jeffy Lazar Apr 20 '15 at 05:37
0

use android:stackFromBottom="true" in your listview xml layout.

For example,

<ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:divider="@null"
        android:stackFromBottom="true" >
</ListView>

Hope this works fine.

Jithin Varghese
  • 2,018
  • 3
  • 29
  • 56
0

I finally figure out the root of the problem. When you are loading images in listView using "AsyncTask", you need to use

listView.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);

My listview cannot scroll to the bottom as i set

listView.setOverScrollMode(View.OVER_SCROLL_NEVER);

which leads to the problem discussed above.

Antoine Murion
  • 773
  • 1
  • 14
  • 26