ListView 在图像上使用 AsynTask 滚动到底部

ListView Scroll To Bottom with AsynTask on Image

问题:ListView 使用 AsynTask 加载图像时无法滚动到底部。

详细信息:我想在用户打开 listView 时将 listView 滚动到底部。列表视图包含图像和文本。为了更好地管理内存,我使用建议的方法 here,它在加载位图时显示一个小图标。

为了滚动到底部,我使用了

`listview.setStackFromBottom(true);`

和:

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

但是,对于图像列表视图,它无法滚动到底部。这个问题有什么解决办法吗?

在列表视图 xml 布局中使用 android:stackFromBottom="true"

例如,

<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>

希望一切顺利。

你的 AsyncTask onPostExecute 应该看起来像这样

@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();
        }
    }

然后是方法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());
        }
    });
}

大功告成:)

终于找到问题的根源了。 当您使用 "AsyncTask" 在 listView 中加载图像时,您需要使用

listView.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);

我设置的列表视图无法滚动到底部

listView.setOverScrollMode(View.OVER_SCROLL_NEVER);

这导致了上面讨论的问题。