当 ListItem 被选中时,如何修改它的背景?

发布于 2024-09-25 13:50:51 字数 330 浏览 7 评论 0原文

我有一个带有一堆 ListItem 的 ListView 。当用户选择一个项目时,我想将该 ListItem 的背景更改为图像。我怎样才能做到这一点?

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    // how do I change the ListItem background here?
    }
});

I have a ListView with a bunch of ListItem's. When the user selects an item, I would like to change that ListItem's background to an image. How can I accomplish this?

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    // how do I change the ListItem background here?
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2024-10-02 13:50:51

您可以在 ListView 的适配器中进行设置。您所要做的就是在您返回的View 上使用setBackgroundResource。让我给您一个简短的示例:

// lets suppose this is your adapter (which obviously has
//to be a custom one which extends from on of the main
//adapters BaseAdapter, CursorAdapter, ArrayAdapter, etc.)

// every adapter has a getView method that you will have to overwrite
// I guess you know what I'm talking about
public View getView( args blah blah ){
    View theView;
    // do stuff with the view

    // before returning, set the background
    theView.setBackgroundResource(R.drawable.the_custom_background);

    return theView;
}

请注意,我正在使用 R.drawable.the_custom_background,这意味着您必须编写一些 XML 选择器。别担心,这比听起来容易。您在 res/drawable 文件夹中创建一个名为 the_custom_background.xml 的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:state_enabled="true"
        android:drawable="@drawable/the_background_color" />
</selector>

再次注意,我使用的是 @drawable/the_background_color,因此最后在 res/drawable 文件夹中创建另一个名为 the_background_color 的可绘制对象:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FF0000" />
</shape>

我知道这看起来很混乱,但这是 Android 的方式。您还可以尝试修改 setOnItemClickListener 内的 View,但我认为这是不可取的且更难实现。

You can set that in the adapter of your ListView. What you have to do is use the setBackgroundResource on the View that you are returning. Let me give you a brief example:

// lets suppose this is your adapter (which obviously has
//to be a custom one which extends from on of the main
//adapters BaseAdapter, CursorAdapter, ArrayAdapter, etc.)

// every adapter has a getView method that you will have to overwrite
// I guess you know what I'm talking about
public View getView( args blah blah ){
    View theView;
    // do stuff with the view

    // before returning, set the background
    theView.setBackgroundResource(R.drawable.the_custom_background);

    return theView;
}

Notice that I'm using R.drawable.the_custom_background, which means that you have to write a little XML selector. Don't worry, it's easier than it sounds. You create an XML file called the_custom_background.xml inside the res/drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:state_enabled="true"
        android:drawable="@drawable/the_background_color" />
</selector>

Notice again, I'm using @drawable/the_background_color, so finally create another drawable in the res/drawable folder called the_background_color:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FF0000" />
</shape>

I know it seems to be very messy, but this is the Android way. You could also try to modify the View inside the setOnItemClickListener, but I think that's undesirable and harder to implement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文