Android ExpandableListView:单击时设置所选项目的背景色 [英] Android ExpandableListView : set background color of selected item on click

查看:457
本文介绍了Android ExpandableListView:单击时设置所选项目的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击我的expandableListView中的子项目时,我正在尝试为项目设置背景色.

I am trying to set a background color for item when the user click on a child item in my expandableListView.

这是代码:

expListView.setOnChildClickListener(new OnChildClickListener() {

@Override
        public boolean onChildClick(ExpandableListView parent, View v, final int groupPosition, final int childPosition, long id) {

            AlertDialog.Builder builder = new AlertDialog.Builder(New_annonce_act_step2.this);
            builder.setMessage("Vous avez choisi la catégorie " +listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition) +", Continuer ?")
            .setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent myIntent = new Intent(New_annonce_act_step2.this, New_annonce_act_step22.class);
                    myIntent.putExtra("titre", titre);
                    myIntent.putExtra("categorie", categorie);
                    New_annonce_act_step2.this.startActivity(myIntent);
                    finish();

                    overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);

                }
            })
            .setNegativeButton("Non", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
            // Create the AlertDialog object and return it
            builder.create();
            builder.show();

            return false;
        }

    });

这是布局中的ExpandableListView声明:

Here is the ExpandableListView declaration in layout :

 <ExpandableListView
            android:id="@+id/nouvelle_annonce_categorie"
            android:layout_width="match_parent"
            android:layout_height="320dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:choiceMode = "singleChoice"
            android:listSelector = "@drawable/selector_categorie_item"
            android:layout_marginTop="15dp" />

这是selector_categorie_item.xml的代码:

And here is the coe of selector_categorie_item.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
    <item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
    <item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>

</selector>

推荐答案

首先删除属性:

 <ExpandableListView
                android:listSelector = "@drawable/selector_categorie_item" />

,并从ExpandableListView中删除background选择器.

然后在您的子布局项中放置下一个属性:

Then in your child layout item put next attribute:

 <YourLayout
         android:background = "@drawable/your_selector" />

也许您需要这样的selector:

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

这篇关于Android ExpandableListView:单击时设置所选项目的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆