ListView Requires 2 setSelection's to Scroll to Item

强颜欢笑 提交于 2019-12-06 04:28:47

Rewrite:

If I take them out of the runnable it works with calling setselection just once. However then I don't get a delayed scrolling effect (to show the user it scrolled), which I guess I sorta like.

When I took a closer look at your code I noticed that you were calling getView() in the Adapter manually. Adapters recycle their views in a very particular but unpredictable order and attempting to call getView() yourself might create unwanted behavior... You should avoid doing this, try a different tactic:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data); // or changeCursor(data) as explained below
    getListView().postDelayed(new Runnable() {
        @Override
        public void run() {
            ListView listView = getListView(); // Save a local reference rather than calling `getListView()` three times
            listView.setSelection(selectedposition);
            listView.performItemClick(listView.getChildAt(0), selectedposition, selectedposition);
        }
    }, 500);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!