I have a layout where I have an expandable list in a fragment on the left and a details fragment on the right. This all works fine.
Now I would like to indicate what
The next solution help you make different list selector for group and child.
First of all, you need disable list selector for ExpendableListView:
...
<ExpandableListView
...
android:listSelector="@android:color/transparent"
...
/>
...
After, you need copy an empty listSelector. You can do you like this
...
private Drawable empty;
...
// binding view here. Name of ExpandableListView will be elv
empty = elv.getSelector();
Now, we need know when we will enable list selector or disable. You should add follow changes to your adapter for that:
my adapter here {
...
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent)
{
...
// after, we've got view of goup
view.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
elv.setSelector(empty);
return false;
}
});
...
}
...
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent)
{
...
// after, we've got view of child
view.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
elv.setSelector(R.drawable.my_custom_list_selector);
return false;
}
});
...
}
...
}
That's all. I hope this solution keeps your time.
This simple solution has helped me when I was faced with the same question as the author
I have a layout where I have an expandable list in a fragment on the left and a details fragment on the right. This all works fine. Now I would like to indicate what item on the left is having it's details shown on the right and here I'm having a problem.
To indicate what item in expandable list is selected go to ExpandableListView properties (in layout) and chose color for listSelector.
Do the following on ExpandableListView:
Step 1. Set choice mode to single (Can be done in xml as android:choiceMode = "singleChoice")
Step 2. Use a selector xml as background (android:listSelector = "@drawable/selector_list_item")
<?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>
Step 3. Call expandableListView.setItemChecked(index,true) in onChildClick() callback.
index is a 0 based index of the child item calculated as follows
Group 1 [index = 0]
Group 2 [index = 4]
Group 3 [index = 8]
If we have list headers as well, they would also account for index values.
Here is a working example:
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
...
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
return true;
}
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick( ExpandableListView parent, View v,
int groupPosition, int childPosition,
long id) {
for (int i = 1; i < expListView.getChildCount(); i++) {
View child = (View) expListView.getChildAt(i);
child.setBackgroundColor(Color.BLACK);
}
if(childPosition==0){
initWebView("URL");
}
if(childPosition==1) {
initWebView("URL");
}
if(childPosition==2) {
initWebView("URL");
}
if(childPosition==3) {
initWebView("URL");
}
if(childPosition==4) {
initWebView("URL");
}
if(childPosition==5) {
initWebView("URL");
}
v.setBackgroundColor(Color.GRAY);
expListView.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
return false;
}
});
}
just use the below
<ExpandableListView
android:id="@+id/expandable_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:childDivider="#474043"
android:groupIndicator="@null"
android:listSelector = "@drawable/item_selector"
android:background="#555"
android:drawSelectorOnTop="true"
android:textFilterEnabled="true"
/>
and your item_selector.xml as follows
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/selection_color_for_ex_list" android:state_pressed="true"/>
<item android:drawable="@color/selection_color_for_ex_list" android:state_selected="true"/>
<item android:drawable="@color/selection_color_for_ex_list" android:state_focused="true"/>
</selector>