2

我想爲用戶點擊我的expandableListView中的子項目時設置項目的背景顏色。Android 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_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的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> 
+0

並與您的選擇有什麼錯? – GIGAMOLE 2015-02-06 14:05:08

+0

當我點擊該項目時,不應用背景顏色。 – wawanopoulos 2015-02-06 14:06:00

回答

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

我嘗試你的代碼,但同樣的事情,顏色不會改變。 – wawanopoulos 2015-02-06 14:13:09

+0

外觀更新。必須工作。 – GIGAMOLE 2015-02-06 14:16:56

+0

現在,點擊後,只有expandableListView的網格改變了顏色。點擊的項目不會改變.. – wawanopoulos 2015-02-06 14:26:42

2

這個工作對我來說:

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_width="match_parent" 
       android:layout_height="wrap_content"> 
    <TextView 
      android:id="@+id/tvListItem" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      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>