eclipse 单击时更改列表视图项的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20309133/
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
change the background color of a listview item when clicked
提问by Zamornews
my app contain a ListView and when a user click on one of the items, the selected item color change to a different color. i acomplished this by adding this android:listSelector="@android:color/darker_gray"
in my ListView located in my activity_main.xml file. This is precisely what i am looking for, but im having issues with it because the background color also changes when the user hold the item but dont click it. so i was wondering if the was a very extremely simple way i could put this code:android:listSelector="@android:color/darker_gray"
inside my OnItemClick method. that way when an item from my listview is clicked, i want the selected item background to change. also i do NOT want to rearrange my listview and put the selected items on top, i just want all the items that the user have clicked to have a different background color which is why i want to put the coding inside onItemClick. Currently i am using the default sample_list_item_1 for my ListView.
我的应用程序包含一个 ListView,当用户单击其中一个项目时,所选项目的颜色会更改为不同的颜色。我通过将它添加到android:listSelector="@android:color/darker_gray"
位于我的 activity_main.xml 文件中的 ListView来完成此操作。这正是我正在寻找的,但我遇到了问题,因为当用户握住该项目但不单击它时,背景颜色也会发生变化。所以我想知道这是否是一种非常简单的方式,我可以放置这段代码:android:listSelector="@android:color/darker_gray"
在我的 OnItemClick 方法中。这样当我的列表视图中的一个项目被点击时,我希望所选项目的背景发生变化。我也不想重新排列我的列表视图并将所选项目放在顶部,我只想让用户单击的所有项目具有不同的背景颜色,这就是为什么我想将编码放在 onItemClick 中。目前我正在为我的 ListView 使用默认的 sample_list_item_1。
Additional Info about App: This is kinda like a tutorial app where the user is presented with a list of tutorial names. when the user clicks on a names it loads a stream-able link so the user can just stream it inside the app (this is already done). i have also added a toast message that shows when the user clicks on the stream-able links (this is kinda like what i want eccept instead of a toast message, it changes the background color of the selected item to darker_grey). i was able to do all this inside if-statements. for example:
关于应用程序的附加信息:这有点像一个教程应用程序,用户会看到一个教程名称列表。当用户点击一个名字时,它会加载一个可流式传输的链接,这样用户就可以在应用程序中流式传输它(这已经完成了)。我还添加了一条 toast 消息,显示用户何时单击可流式传输的链接(这有点像我想要的东西,而不是 toast 消息,它将所选项目的背景颜色更改为 darker_grey)。我能够在 if 语句中完成所有这些。例如:
Inside OnCreate()
OnCreate() 内部
String[] Tutorial = getResources().getStringArray(R.array.Tutorial);
spinner1 = (ListView) findViewById(R.id.spinner1);
spinner1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Tutorial));
spinner1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{
if(pos==0)
{
// Do nothing
}
else if(pos==1)
{
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("https://download846.mediafire.com/8cfxi4026iog/x4ywqe8dc8u2r4f/Hetalia+Season+1+Episode+1.mp4"); /* this is a random link to show you that my programing so far works(Enter link to the selected tutorial here)*/
i.setDataAndType(uri, "video/*");
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
// Handle exception
} catch (Exception e) {
// Handle exception
}
Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
// android:listSelector="@android:color/darker_gray"
}
}
}
if you still dont understand what i meant, there is an app in the googlePlay store called Anime Plus TV that does something similar to what i meant. in the app, when you click on the episode of a serie (example: naruto) it loads the episode's stream-able link, and also change the text color of that selected episode so that when the user finishes watching the episode, he wont be confused about which episode he was watching since some stream-able links doesnt realy give you the real title of the video you where watching. but unlike them i just want the background of the selected Tutorial to change
如果您仍然不明白我的意思,googlePlay 商店中有一个名为 Anime Plus TV 的应用程序,其功能与我的意思类似。在应用程序中,当您单击连续剧的剧集(例如:火影忍者)时,它会加载该剧集的可流式传输链接,并更改所选剧集的文本颜色,以便用户看完剧集后,他不会对他正在观看的剧集感到困惑,因为一些可流式传输的链接并不能真正为您提供您正在观看的视频的真实标题。但与他们不同的是,我只想改变所选教程的背景
回答by Aerox
You have two options doing that .
你有两种选择。
1.progamatically:In this case when you set the OnItemClickListener for your ListView , get the child and setBackground for it .
1.progamatically:在这种情况下,当您为 ListView 设置 OnItemClickListener 时,获取子项并为其设置背景。
String[] inputs = {"test1","test2","test3"};
final ListView l = (ListView)findViewById(R.id.listview);
l.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,inputs));
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
l.getChildAt(position).setBackgroundColor(android.R.color.black);
}
});
You can define colors in color.xml file under folder res/values ( create it if you dont have it ) and use them like R.color.gray (example) instead of android.R.color.black .
您可以在文件夹 res/values 下的 color.xml 文件中定义颜色(如果没有,则创建它)并像 R.color.gray (示例)而不是 android.R.color.black 一样使用它们。
2.Using Custom listSelector
2.使用自定义列表选择器
under folder drawable create the listselector.xml . In listselector you can handle different states just like this :
在文件夹 drawable 下创建 listselector.xml 。在 listselector 中,您可以像这样处理不同的状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/test1" />
<item android:state_pressed="true"
android:drawable="@drawable/test2" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/test3" />
</selector>
for your case i think using first way is the best . ask if you had any other questions :)
对于您的情况,我认为使用第一种方式是最好的。问你是否还有其他问题:)