Android - How to tap ListView item programmaticall

2020-02-03 09:35发布

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

6条回答
\"骚年 ilove
2楼-- · 2020-02-03 10:03

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.

查看更多
forever°为你锁心
3楼-- · 2020-02-03 10:04

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
    }
});
查看更多
看我几分像从前
4楼-- · 2020-02-03 10:08

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.

查看更多
你好瞎i
5楼-- · 2020-02-03 10:09

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.

查看更多
6楼-- · 2020-02-03 10:18

The answer is

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

from the link

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

查看更多
霸刀☆藐视天下
7楼-- · 2020-02-03 10:27
mList.performItemClick(
    mList.getAdapter().getView(mActivePosition, null, null),
    mActivePosition,
    mList.getAdapter().getItemId(mActivePosition));

Where mActivePosition is your click position!

查看更多
登录 后发表回答