Android - How to tap ListView item programmatically

前端 未结 6 878
轻奢々
轻奢々 2020-12-09 02:36

How to call ItemClickListener programmatically? listView.performItemClick() does not work. Is that possible?

相关标签:
6条回答
  • 2020-12-09 03:09

    If you need it for testing purposes, then you can use Robotium ( http://code.google.com/p/robotium/ ).

    You could also achieve what you want by calling the onClick method of the ClickController with the correct parameters.

    0 讨论(0)
  • 2020-12-09 03:09

    The answer is

    listView1.performItemClick(listView1, 3, listView1.getItemIdAtPosition(3));

    from the link

    http://mantascode.com/?p=486

    0 讨论(0)
  • 2020-12-09 03:17
    mList.performItemClick(
        mList.getAdapter().getView(mActivePosition, null, null),
        mActivePosition,
        mList.getAdapter().getItemId(mActivePosition));
    

    Where mActivePosition is your click position!

    0 讨论(0)
  • 2020-12-09 03:19

    You can set up an onItemClick listener for your list view via

    listView.setOnClickListener(new OnClickListener() {
        @Override
        public void   onClick(View v) {
            //here you do something
        }
    });
    
    0 讨论(0)
  • 2020-12-09 03:22

    Assign tag in the adapter to each View, and findviewByTag() this worked for me:

    listView.performItemClick(listView.findViewWithTag(listView.getAdapter().getItem(selectedIndex)), selectedIndex, listView.getAdapter().getItemId(selectedIndex));
    

    Also refer this answer.

    0 讨论(0)
  • 2020-12-09 03:29

    If you want to click/tap/select 3rd list item then.

    listView.performItemClick(listView.getAdapter().getView(3, null, null), 3, listView.getItemIdAtPosition(3));
    

    This worked perfectly for me.

    0 讨论(0)
提交回复
热议问题