当父组已展开以显示其子项时,如何更改图标

时间:2017-02-13 08:34:47

标签: java android android-layout expandablelistview

我有一个可扩展的列表视图,当我点击父组并显示子项时,我想将图标从右向箭头改为向下箭头。目前,当我单击父组以展开并显示子图标时,图标不会从右箭头更改为向下箭头,它只是保留为rigth箭头图标。这就是我目前在我的exapndable适配器中所拥有的。

@Override
   public View getGroupView(int groupPosition, boolean isExpanded,
                            View convertView, ViewGroup parent) {
      String headerTitle = getGroup(groupPosition).toString();
      if (convertView == null) {
         LayoutInflater infalInflater = (LayoutInflater) this._context
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = infalInflater.inflate(R.layout.list_group, null);



         ImageView arrowImageView = (ImageView) convertView.findViewById (R.id.group_header_arrow);
         if (isExpanded) {
            arrowImageView.setImageResource(R.drawable.arrow_down);
           (ApplicationContext.Activity.Resources.GetColor (Resource.Color.standard_blue));
         }


      }
      TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);

      lblListHeader.setText(headerTitle);
      TextView Header = (TextView) convertView
            .findViewById(R.id.lblListHeader);

      lblListHeader.setText(headerTitle);
      return convertView;
   }

组标题的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="8dp"
              android:background="#f4f4f4">


   <ImageView
      android:id="@+id/group_header_arrow"
      android:src="@drawable/arrow_right"
      android:layout_centerVertical="true"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingRight="10dp" />

   <TextView
      android:id="@+id/lblListHeader"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
      android:textSize="17dp"
      android:textColor="#388C2A" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

使用此,

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

保存在Drawable中,

答案 1 :(得分:0)

在适配器类中添加此代码,

    final ExpandableListView mExpandableListView= (ExpandableListView) parent ;

    mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            arrowImageView.setImageResource(R.drawable.arrow_down);
        }
    });

    mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            arrowImageView.setImageResource(R.drawable.arrow_left);
        }
    });

    if(mExpandableListView.isGroupExpanded(groupPosition))
    {
       arrowImageView.setImageResource(R.drawable.arrow_down);
    }
    else
    {
       arrowImageView.setImageResource(R.drawable.arrow_left);
    }