I've used selector to set indicator icon for my ExpandableListView but my collapse and expand icon are resized to match the size of the original 开发者_开发百科indicator icon(which is bigger than my icons). Anyone knows how to set size (width and height) for the indicator icon? Thanks!
This will be working definitely:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true">
<layer-list>
<item
android:left="0dp"
android:right="3dp"
android:top="20dp"
android:bottom="20dp"
android:drawable="@drawable/down_indication">
</item>
</layer-list>
</item>
<item android:state_expanded="true">
<layer-list>
<item
android:left="0dp"
android:right="3dp"
android:top="20dp"
android:bottom="20dp"
android:drawable="@drawable/up_indication">
</item>
</layer-list>
</item>
</selector>
No matter what you set the dimensions of your indicator as, the listview Layout's group indicator container has a match_parent for height, and hence your indicator will always be stretched.
I got this working by :
(1) Setting the group indicator to null
(2) Including my custom group indicator in the group Layout.
(3) Having an groupClickListener
that changes the state of you indicator.
Code Snippets :
(1) mListView.setGroupIndicator(null);
(2) Included my indicator in the group layout.
<ImageView
android:id="@+id/help_group_indicator"
android:layout_width="@dimen/help_group_indicator_dimension"
android:layout_height="@dimen/help_group_indicator_dimension"
android:layout_marginTop="@dimen/help_group_indicator_dimension"
android:src="@drawable/down_icon" />
(3)
mListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View clickedView, int groupPosition, long rowId) {
ImageView groupIndicator = (ImageView) clickedView.findViewById(R.id.help_group_indicator);
if (parent.isGroupExpanded(groupPosition)) {
parent.collapseGroup(groupPosition);
groupIndicator.setImageResource(R.drawable.down_icon);
} else {
parent.expandGroup(groupPosition);
groupIndicator.setImageResource(R.drawable.up_icon);
}
return true;
}
});
The answer suggested by @Satheesh will work, but only if all groupViews have the same height. If your group layouts have say, wrap text and wrap_content for height, the height might vary according to your content and the padding you set might no longer work.
Did you set the background drawable to be the correct height for your icons? If it's using the default background drawable, you'll get the height/width of the default icon, even if your drawables are smaller.
If you have TextView next to the indicator icon in group layout, you can also do the following:
(1) in XML layout for ExpandableListView, remove groupIndicator:
android:groupIndicator="@null"
(2) in getGroupView(...) method of your custom ExpandableListAdapter, set left drawable for TextView:
int leftIcon=R.drawable.ic_noexpand;
if (isExpanded) leftIcon=R.drawable.ic_expand;
lblListHeader.setCompoundDrawablesWithIntrinsicBounds(
ContextCompat.getDrawable(_context,leftIcon),null,null,null);
(if min API is >=21, you may use Context instead of ContextCompat)
(3) set android:paddingLeft="0dp"
in XML group layout, since space for the indicator icon will now be empty
To set width and height for indicator icon of an ExpandableListView and change color of icon programatically in android.
ExpandableListView expandableList;
Drawable d = getResources().getDrawable(R.drawable.custom_expandable);
d.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
expandableList.setGroupIndicator(d);
custom_expandable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true">
<layer-list>
<item
android:left="12dp"
android:right="12dp"
android:top="15dp"
android:bottom="15dp"
android:drawable="@drawable/ic_arrow_down">
</item>
</layer-list>
</item>
<item android:state_expanded="true">
<layer-list>
<item
android:left="12dp"
android:right="12dp"
android:top="15dp"
android:bottom="15dp"
android:drawable="@drawable/ic_arrow_up">
</item>
</layer-list>
</item>
</selector>
精彩评论