Android ListView item highlight programmatically

若如初见. 提交于 2019-12-19 06:54:06

问题


I know it's been asked around but I haven't found quite the answer yet (new to Android so sorry about the ignorance)

my app's launch Activity has an EditText (called searchbox) , a ListView (designations) and a Spinner (types)

I use the EditText as a searchbox, I have to pass the string through a custom editing to make searching more flexible. After that I match that against the closest approximation I find in 'designations' and do

designations.setSelection(j);

As expected, this sets the desired item to the top of designations. But I can't find a way to highlight it via code.

NOW, i do know that if the device is in touch mode the highlighting of a selected item won't occur. So the last 4 lines of my searchbox's onTextChanged event are:

        designations.setFocusable(true);            
        designations.setFocusableInTouchMode(true);
        if (match==true)      designations.setSelection(j);
        if (st.length()==0)   designations.setSelection(0);

to no avail.

now, i don't have any code on searchbox's afterTextChanged(Editable s);

so could anyone give me a clue on this?

regards ~dh


回答1:


check out requestFocus() and maybe requestChildFocus()




回答2:


Have you tried setting the actual View to be selected?

designations.setSelection(j);
designations.getSelectedView().setSelected(true);


来源:https://stackoverflow.com/questions/2428184/android-listview-item-highlight-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!