Android ExpandableListView:在点击时设置所选项目的背景颜色

Posted

技术标签:

【中文标题】Android ExpandableListView:在点击时设置所选项目的背景颜色【英文标题】:Android ExpandableListView : set background color of selected item on click 【发布时间】:2015-04-06 16:42:15 【问题描述】:

当用户单击我的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 声明:

 <ExpandableListView
            android:id="@+id/nouvelle_annonce_categorie"
            android:layout_
            android:layout_
            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 的 coe:

<?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>

【问题讨论】:

你的选择器出了什么问题? 当我点击项目时,不应用背景颜色。 【参考方案1】:

这对我有用:

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() 
        @Override
        public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) 
            int index = parent.getFlatListPosition(ExpandableListView
                    .getPackedPositionForGroup(groupPosition));
            parent.setItemChecked(index, true);

            return false;
        
    );
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() 
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 
            /*Toast.makeText(scanTemplateFragment.getActivity(),
                    expandableListTitle.get(groupPosition)
                            + " -> "
                            + expandableListDetail.get(
                            expandableListTitle.get(groupPosition)).get(
                            childPosition), Toast.LENGTH_SHORT).show();*/
            int index = parent.getFlatListPosition(ExpandableListView
                    .getPackedPositionForChild(groupPosition, childPosition));
            parent.setItemChecked(index, true);

            return false;
        
    );

并从 list_item.xml 设置背景:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_
              android:layout_>
    <TextView
            android:id="@+id/tvListItem"
            android:layout_
            android:layout_
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:paddingTop="10dp"
            android:background="@drawable/row_highlighter"
            android:paddingBottom="10dp" />
</LinearLayout>

row_highlighter.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@color/std_color_1_bright" android:state_activated="true"/>
        <item android:drawable="@android:color/transparent"/>
    </selector>

【讨论】:

【参考方案2】:

首先删除属性:

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

并从ExpandableListView 中删除background 选择器。

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

 <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>

【讨论】:

现在,点击时,只有 expandableListView 的网格会改变颜色。点击的项目没有改变.. 试试这个

以上是关于Android ExpandableListView:在点击时设置所选项目的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本

Android逆向-Android基础逆向(2-2)

【Android笔记】android Toast

图解Android - Android核心机制

Android游戏开发大全的目录