Android - ListView - performItemClick

前端 未结 24 1305
清歌不尽
清歌不尽 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 03:57

    This worked for me.

    listView.performItemClick(
        listView.getAdapter().getView(position, null, null), position, position);
    

    use the adapter to get the view for the position of the item. The other 2 parameters I didn't want so I left them null. Leaving convertView null causes the adapter to render a new view. It's a performance issue but since this is only happening once in a while it wont have much effect. I don't need to specify the parent for anything because I'm not using it.

    position is just the spot where your item is located. Additionally these 2 lines of code before your performItemClick create the illusion of having the list item selected. They also ensure the appropriate item is on the screen.

    listView.requestFocusFromTouch();
    listView.setSelection(position);
    

提交回复
热议问题