在ExpandableListView中更改可扩展指示器

Nag*_*ddy 46 android listview expandablelistview

我正在尝试创建一个ExpandableListView.群组的初始视图显示正常.但是,当我单击列表项时,我的箭头不会更改.见下图.

在此输入图像描述

如何更改箭头的方向?

我有布局XML:

  <ExpandableListView
        android:id="@+id/expandable_list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"            
        android:divider="@null"
        android:background="#ffffff"
        android:groupIndicator="@drawable/settings_selector"
        android:transcriptMode="alwaysScroll" />
Run Code Online (Sandbox Code Playgroud)

settings_selector.xml:

 <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/arrow_down"
        android:state_empty="true"/>
    <item
        android:drawable="@drawable/arrow_right"
        android:state_expanded="true"/>
</selector>

</animation-list>
Run Code Online (Sandbox Code Playgroud)

Nag*_*ddy 63

可扩展的列表视图

 <ExpandableListView
    android:id="@+id/expandable_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:groupIndicator="@drawable/group_indicator"
    android:transcriptMode="alwaysScroll" />
Run Code Online (Sandbox Code Playgroud)

setindicator 在这里使用像这样的setindicator代码这很好用

 DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels; 

 mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
 mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));  

   public int GetPixelFromDips(float pixels) {
    // Get the screen's density scale 
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
}
Run Code Online (Sandbox Code Playgroud)

RES /抽拉/ group_indicator

  <?xml version="1.0" encoding="utf-8"?>

     <selector xmlns:android="http://schemas.android.com/apk/res/android">            
           <item android:drawable="@drawable/arrow_right" android:state_empty="true">  </item>            
           <item android:drawable="@drawable/arrow_down" android:state_expanded="true"></item>                
           <item android:drawable="@drawable/arrow_right"></item>
     </selector>
Run Code Online (Sandbox Code Playgroud)

  • 此代码在android 4.3中不起作用.:( (9认同)
  • 在android 4.3中使用expListView.setIndicatorBoundsRelative (9认同)
  • `mExpandableList.setIndicatorBounds(width - 50,width - 10);`给出相同的输出..为什么????? (2认同)

Ben*_*fez 25

试试你的settings_selector.xml:

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

    <item
        android:drawable="@drawable/arrow_right"
        android:state_expanded="true" />

    <item
        android:drawable="@drawable/arrow_down" />

</selector>
Run Code Online (Sandbox Code Playgroud)


lil*_*ily 16

我已经走了以下的方式:根据isExpanded标志确定groupView的左/右drawable.
通过这种方式,我们更容易定制填充/背景和指标drawable的其他东西.

希望能帮助到你.

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView textView = (TextView) mLayoutInflater.inflate(R.layout.menu_group, null);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? 0 : android.R.drawable.ic_menu_more, 0);
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }
Run Code Online (Sandbox Code Playgroud)