Android - ListView - performItemClick

前端 未结 24 1278
清歌不尽
清歌不尽 2020-11-29 03:10

I\'m facing some difficults when I try to use the performItemClick funcion of the ListView.

All I want to do is to perform a click programatically i

24条回答
  •  一向
    一向 (楼主)
    2020-11-29 04:01

    If you would get weird result when using getView, this is because the list item you want does not exist within visible parts. Use below:

    private View getViewFromAdapterByPosition(int position, ListView listView) 
    {
            View view;
            int firstVisiblePos = listView.getFirstVisiblePosition();
            int lastVisiblePos = listView.getLastVisiblePosition();
    
            if (position < firstVisiblePos || position > lastVisiblePos) {
                view = listView.getAdapter().getView(position, null, listView);
            } else {
                view = listView.getChildAt(position - firstVisiblePos);
            }
            return view;
        }
    

    And then,

    listView.performItemClick(getViewFromAdapterByPosition(index, listView), index, 0);
    

提交回复
热议问题