Android - 单击某一项后,保持 ListView 的项目突出显示
因此,我有一个包含 2 个 ListView 小部件的活动,当您在第一个小部件中选择一个值时,第二个小部件将填充与第一个 ListView 中的选择相关的值。这个机制工作没有问题,但现在我希望用户选择保持突出显示。我已经阅读了大量与该主题相关的问题,似乎有无数种方法可以实现这一目标,但在尝试了大约 4-5 个之后,我仍然无法让它发挥作用。
我已经使用 android:listSelector="#CCCCCC"
XML 属性在第二个 ListView
上工作,但是一旦 OnItemClickListener 似乎就被清除了
被引入到混合中(就像我在第一个 ListView
上使用的那样)。
到目前为止,这是我所得到的:
自定义OnItemClickListener
我发现浏览有关此主题的各种答案(稍微修改它以便它在第二个ListView中加载我的信息):
private class ItemHighlighterListener implements OnItemClickListener {
private View oldSelection = null;
public void clearSelection() {
if(oldSelection != null) {
oldSelection.setBackgroundColor(android.R.color.transparent);
}
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
clearSelection();
oldSelection = view;
view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.list_selector));
loadClubs(mXMLPortalOptions.getRegion(pos).getId());
mClubList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_white, mClubs));
}
}
这是我的 list_selector.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"><shape>
<solid android:color="#CCCCCC" />
</shape></item>
<item android:state_selected="false"><shape>
<solid android:color="#FFFFFF" />
</shape></item>
</selector>
方法 (OnItemClick) 被调用并执行,但我的 ListItem
的背景保持相同的颜色:/
我不敢相信这个简单的事实证明任务是如此复杂。
如果我省略了可能有用的代码或者我的问题缺乏细节,请随时指出,我会尽力解释自己。
So I have an activity with 2 ListView
widgets, when you select a value in the first one, the second is populated with values related to the selection in the first ListView
. This mechanic works without a problem, but now I want the user choices to stay highlighted. I've read a good ammount of question related to this topic and it seems there is a myriad of ways one can accomplish this but after trying about 4-5 of em' I still can't get it to work.
I've got it working on the second ListView
using the android:listSelector="#CCCCCC"
XML Attribute, but this seems to be wiped clean once a OnItemClickListener
is introduced into the mix (like the one I use on my first ListView
).
So far here's what I've got:
Custom OnItemClickListener
I found browsing various answer regarding this topic (slightly modified it in order for it to load my info the second ListView):
private class ItemHighlighterListener implements OnItemClickListener {
private View oldSelection = null;
public void clearSelection() {
if(oldSelection != null) {
oldSelection.setBackgroundColor(android.R.color.transparent);
}
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
clearSelection();
oldSelection = view;
view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.list_selector));
loadClubs(mXMLPortalOptions.getRegion(pos).getId());
mClubList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_white, mClubs));
}
}
Here's my list_selector.xml
file :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"><shape>
<solid android:color="#CCCCCC" />
</shape></item>
<item android:state_selected="false"><shape>
<solid android:color="#FFFFFF" />
</shape></item>
</selector>
The method (OnItemClick) is called and executed, but the background of my ListItem
stays the same color :/
I can't believe that this simple task has proven so complicated.
If I have omitted code that could be useful or if my question is lacking details, feel free to point that out and I'll do my best to explain myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
为所选项目放置一个位置变量。更改
onItemClicked()
方法中的位置。检查getView()
中 List Adapter 中选定的位置,并设置选定项目的背景。现在,单击列表项时设置 selectedIndex 变量。
Put a position variable for selected item. Change the position in
onItemClicked()
method. Check the selected position in List Adapter insidegetView()
and set the background for the selected item.Now set the selectedIndex variable when a list item clicked.
为了扩展 Shaful 的出色解决方案,您可能无法让他的解决方案适合您的情况。
如果您使用的代码全部位于
public void onListItemClick(ListView l, View v, int index, long id)
中,如果您使用片段并且必须声明一个接口而不是实现 OnListItemClickListener,或者任何导致 IDE 生成错误的原因,您可能必须静态访问变量和方法。在 Your_adapter 中:
其他一些区别是您不必将任何变量初始化为“0”或“-1”,并且在您的活动中调用 notificationDataSetChanged() 。
再次感谢您的解决方案@Shaiful。它确实帮助我节省了尝试让 iOS 中的默认设置适用于 Android 的时间,同时避免了选择器/项目/聚焦/按下/等等。
To expand on Shaiful's great solution, you might not get his to work in your situation.
If you are using have your code all in
public void onListItemClick(ListView l, View v, int index, long id)
, if you're using fragments and have to declare an interface instead of implementing OnListItemClickListener, or whatever is causing your IDE to generate errors, you might have to access variables and methods statically.And in Your_adapter:
Some other differences are that you don't have to initialize any variables as "0" or "-1" and notifyDataSetChanged() is called in your activity.
Once again, thanks for your solution @Shaiful. It certainly helped save me time trying to get what is default in iOS to work for Android, all while avoiding selector/item/focused/pressed/etc.
我遇到了类似的问题。这是我的解决方案:
首先将自定义列表选择器添加到列表视图中:
在 listselector.xml 中:
最后是一个可绘制的 bg.xml,其中包含突出显示的颜色:
I faced similar problem. That's my solution:
First add custom list selector to your list view:
Inside listselector.xml:
And finally a drawable bg.xml with color of your highlight:
我认为最好和最简单的解决方案是这样的。您不需要需要在 ListView 本身上设置任何
android:listSelector
或对适配器进行任何更改。您不需要甚至不需要在OnItemClickListener
中调用任何setSelection(position)
,因为它是自动处理的。设置为您的 ListView:
设置列表项本身的背景:
就是这样。
这样您将获得默认的系统行为。这就是默认
android.R.layout.simple_list_item_activated_1
布局中的完成方式。I think the best and easiest solution is this. You don't need to set any
android:listSelector
on the ListView itself or do any changes to the adapter. You don't even need to call anysetSelection(position)
in theOnItemClickListener
as it is handled automatically.Set to your ListView:
Set background of the list item itself:
That's it.
This way you'll get default system behavior. That's how it is done in the default
android.R.layout.simple_list_item_activated_1
layout.lv.setSelector(R.drawable.highlighter);
将
highlighter.png
图像放入可绘制文件夹中在列表视图中突出显示所选项目的最简单方法。
lv.setSelector(R.drawable.highlighter);
put a
highlighter.png
image in drawable foldersimplest way to highlight selected item in list view.
两周前我一直在寻找它,结果是使用可绘制选择器是不可能的。
有关详细信息,请阅读 Android 开发者博客中的这篇文章:触摸模式
在恢复中:仅当您的手指在屏幕上时,才会选择项目。
另一种可能性是保存在 var 中选择的项目并使用自定义适配器绘制不同的内容,如 Shaiful 所说。
I was looking for it two weeks ago and the result is that is not possible with a drawable selector.
For more info read this post in Android Developers Blog: Touch Mode
In resume: Only when your finger are on the screen, item is selected.
Other possibility is save which item is selected in a var and paint different using your custom adapter like Shaiful says.
// 并查看输出。
// And see the output.
如果您可以使用可绘制对象来显示突出显示的 listItem,那么您应该使用以下代码:-
它可以工作。
If you can use a drawable for displaying listItem Highlighted then you should use following code:-
It works.
有一个简单的完全 XML 解决方案,对我有用。
首先,使用选择器代码定义 XML-drawable,其中“正常”状态将对应于列表项的“选定的未按下”视觉状态,state_pressed=true 对应于“按下”视觉状态。
文件“custom_item_selector.xml”的示例,类似于 Holo 蓝色选择:(
也可以在那里设置焦点状态)。
其次,将此 xml-drawable 应用为 ListView 的 listSelector 并设置其所需的 choiceMode:
仅此而已。它允许为“简单选择”和“按下选择”项目定义不同的视觉状态,例如使项目在按下时更亮。
There is simple fully-XML solution, which worked for me.
Firstly, define XML-drawable with selector code in which "normal" state will correspond to "selected unpressed" visual state of a list item, and state_pressed=true to "pressed" visual state.
Example of file "custom_item_selector.xml", resembling Holo blue selection:
(may also set focused state there).
Secondly, apply this xml-drawable as ListView's listSelector and set it's desired choiceMode:
That's all. It allows to define different visual states for "simply selected" and "pressed selected" items, for example making items brighter on press.
要保持列表项(多项选择)突出显示,单击(激活)时,请按照以下步骤操作。
1.将背景设置为可绘制的列表项布局。
2.可绘制选择器
3. Listview设置多选模式
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
按下时:
下图显示,当用户选择多个列表项时。
激活后:
To keep the list items(multiple selection) highlighted, when clicked(activated), please follow the steps.
1. Set background to list item layout as drawable.
2. drawable selector
3. Listview set multiple choice mode
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
When Pressed:
Below image shows, when user selected multiple list items.
When Activated:
为了总结这篇文章,也许可以帮助其他人,我建议答案:)
首先,我们需要创建包含以下内容的
res/drawable/list_item_background.xml
文件:当然,指定您的可绘制资源。您还可以添加其他具有不同状态的
元素,例如state_pressed
、state_focused
等。然后,我们应该设置
我们的自定义列表项
参数如下:ViewGroup
元素(fires/layout/list_item_layout.xml
)的background下一步是修改我们的自定义
适配器
类。下面是以下代码片段:最后,我们应该在
AdapterView.OnItemClickListener
onItemClick(...) 方法中调用setSelectedIndex(position)
适配器的方法。代码>.现在,我们可以对正确的列表项突出显示感到满意了:)
PS如果我们想在列表上启用多项选择模式,我们只需将以下字符串放置到保留
listView
实例的活动类中:因此,我们将获得正确的多个项目突出显示。
——希望这对任何人都有帮助:)
To summarize this post and maybe help someone else in future I suggest the answer :)
First, we need to create
res/drawable/list_item_background.xml
file with the following contents:Specify your drawable resources, of course. And you can also add other
<item>
elemens with different states likestate_pressed
,state_focused
etc.Then, we should set the
background
parameter to our custom list itemViewGroup
element (f.i.res/layout/list_item_layout.xml
) like this:The next step is modifying our custom
Adapter
class. Here is the following code fragment:Finally, we should call the
setSelectedIndex(position)
adapter's method inonItemClick(...)
method ofAdapterView.OnItemClickListener
.Now, we can be satisfied with proper list items highlighting :)
P.S. If we want to enable multiple choice mode on our list we'll just place the following string to our activity class where
listView
instance is kept:Thus, we'll get proper multiple items highlighting.
-- Hope this helps anyone :)