简体   繁体   English

ListView OnFocusChangeListener

[英]ListView OnFocusChangeListener

I have a ListView in a ListFragment and I want to change the background of the fragment when the user changes the focused item in the list. 我在ListFragment中有一个ListView,我想在用户更改列表中的焦点项时更改片段的背景。 I specifically say focus here because this is a Google TV app so I'm targeting dpad controls and item selection does something else. 我特别说这里是焦点,因为这是一个谷歌电视应用程序,所以我的目标是dpad控件和项目选择做其他事情。

The questions is, how can I get a notification or callback when the list item focus changes, similar on onListItemClick, but for focus? 问题是,如何在列表项焦点发生变化时获得通知或回调,类似于onListItemClick,但是对于焦点? I've tried setting the OnFocusChangeListener on the row view when the adapter creates it but that doesn't ever seem to be called. 我已经尝试在适配器创建时在行视图上设置OnFocusChangeListener,但似乎从未调用过。

Use onItemSelected instead, this will be invoked when an item in listView has been selected. 使用onItemSelected ,当选择了listView中的项时,将调用此方法。

listview.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
        //do stuff here       
    }
});

Selected state should be used when user navigate a listview with a directional control (such as when navigating through a list with a d-pad). 当用户使用方向控件导航列表视图时(例如,使用d-pad导航列表时),应使用选定状态。 In this situation, the listView will get focused state and the listItems inside it will get selected state. 在这种情况下,listView将获得聚焦状态,其中的listItems将获得选择状态。 For more information about state, see this 有关状态的更多信息,请参阅此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM