将焦点设置在 android 中列表视图的任何项目上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3168247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set focus on any item of listview in android
提问by sarvesh
I have a listview which contains textviews as its elements.
我有一个包含文本视图作为其元素的列表视图。
- Now I want the first item of the list to be automatically focused when I launch the application
- How can I set the focus on any item of the list when I click on the some other view for example a button?
- 现在我希望在启动应用程序时自动聚焦列表的第一项
- 当我单击某个其他视图(例如按钮)时,如何将焦点设置在列表的任何项目上?
回答by Abhilasha
Setting selection and setting focus are two different things. If you want to just setSelection to some item then you can use below code.
设置选择和设置焦点是两件不同的事情。如果您只想将选择设置为某个项目,那么您可以使用以下代码。
mListView.setSelection(position);
But this definitely does not mean the Listview
is focused.For focussing you have to use
但这绝对不意味着Listview
聚焦。对于聚焦,你必须使用
mListView.requestFocus();
For changing focus on click of a button you can place the code on onClick()
of the button.
要更改单击按钮的焦点,您可以将代码放在onClick()
按钮上。
回答by Robby Pond
ListView has a setSelectedmethod that takes the index of the item in the list.
ListView 有一个setSelected方法,它获取列表中项目的索引。
回答by Lukas Hanacek
for me the problem was solved by
对我来说,问题解决了
listView.setItemsCanFocus(true);
回答by Grimmy
Your ListView should be like this:
<ListView android:id="@+id/my_list" android:layout_width="wrap_content" android:layout_height="200dp" android:layout_margin="10dp" android:choiceMode="none" android:focusable="true" android:fadeScrollbars="false"> <requestFocus /> </ListView>
mListView.setSelection(3); //or any other number
你的 ListView 应该是这样的:
<ListView android:id="@+id/my_list" android:layout_width="wrap_content" android:layout_height="200dp" android:layout_margin="10dp" android:choiceMode="none" android:focusable="true" android:fadeScrollbars="false"> <requestFocus /> </ListView>
mListView.setSelection(3); //or any other number
回答by Jecimi
I guess I was in the same situation. I wanted to be able to control the focus of the listview programmatically with buttons .
我想我处于同样的情况。我希望能够使用按钮以编程方式控制列表视图的焦点。
One solution is to deal with the setFocusableInTouchMode
, but I've never achieved to make it work.
一种解决方案是处理setFocusableInTouchMode
,但我从未实现过使其工作。
The other solution is to forget about the focus and use a checkable listview. First set your listview to "single choice mode" in XML or in java : Mylistview.setChoiceMode(1)
另一个解决方案是忘记焦点并使用可检查的列表视图。首先在 XML 或 java 中将您的列表视图设置为“单选模式”:Mylistview.setChoiceMode(1)
Then you'll be able to check any item you want with Mylistview.setItemChecked(position, true)
然后你就可以检查你想要的任何项目 Mylistview.setItemChecked(position, true)
So when you lunch the application (OnCreate), use Mylistview.setItemChecked(0, true)
to check your first item.
因此,当您午餐应用程序 (OnCreate) 时,请使用Mylistview.setItemChecked(0, true)
检查您的第一项。
Then if you want your button to select the next item for exemple, use :
然后,如果您希望您的按钮选择下一个项目,例如,使用:
Mylistview.setItemChecked(Mylistview.getCheckedItemPosition() + 1, true)
You can specify the look when the item is checked or not and there's different pre-built chekable listviews.
您可以指定选中或不选中项目时的外观,并且有不同的预构建可检查列表视图。
If you want more explanations, see my post
如果你想要更多的解释,请看我的帖子