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
I finally figured out how to make this work (for me). It seems a bit of the hack, but it's the only thing I've managed to get working for this.
Basically I create a two dimensional array to hold the selected state of the children, initialize it in the adapter constructor, then I reference that in my getChildView
method (in case the currently selected child scrolls out of view) and my onChildClick
method (to change the current selection and turn off the old one).
private selectedStatus[][]; // array to hold selected state
private View oldView; // view to hold so we can set background back to normal after selection of another view
Constructor initialize the array
public MyExpandableListAdapter(Cursor cursor, Context context,
int groupLayout, int childLayout, String[] groupFrom,
int[] groupTo, String[] childrenFrom, int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
selectedStatus = new boolean[cursor.getCount()][];
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
Cursor childCheckCursor = mDbHelper.fetchChildren(mGroup,
cursor.getString(cursor
.getColumnIndex(AttendanceDB.EVENT_ROWID)));
getActivity().startManagingCursor(childCheckCursor);
selectedStatus[i] = new boolean[childCheckCursor.getCount()];
for (int j = 0; j < childCheckCursor.getCount(); j++) {
selectedStatus[i][j] = false;
}
}
}
getChildView needed for when the child scrolls out of view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
if(selectedStatus[groupPosition][childPosition] == true){
v.setBackgroundResource(R.color.blue);
} else {
v.setBackgroundResource(R.color.black);
}
return v;
}
onChildClick change selected item
lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
mRowId = id;
EventDisplayFragment eventdisplay = new EventDisplayFragment();
getFragmentManager().beginTransaction()
.replace(R.id.rightpane, eventdisplay).commit();
if(oldView != null){
oldView.setBackgroundResource(R.color.black); // change the background of the old selected item back to default black }
oldView = v; // set oldView to current view so we have a reference to change back on next selection
for (int i = 0; i < selectedStatus.length; i++) {
for (int j = 0; j < checkedStatus[i].length; j++) {
if(i == groupPosition && j == childPosition){ // set the current view to true and all others to false
selectedStatus[i][j] = true;
} else {
selectedStatus[i][j] = false;
}
}
}
v.setBackgroundResource(R.color.blue);
return true;
}
});
After spending 3-4 hours it is made it its so simple no need to create extra XML files just check if the group item is expanded if yes then select color and in else statement make them as they are ....
@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
if(b){
view.setBackgroundResource(R.color.waterblue);
}else{
view.setBackgroundColor(color.transparent);
}
//return the entire view
return view;
}
In case you need only the child item color to be changed than in the onchildclick
method set the background color to the View v.
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
v.setBackgroundColor(Color.RED);
}
to do so set isChildSelectable
to true in the adapter.
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;
}
});
}
I was having the same problem I fixed this by adding
v.setSelected(true);
here's a sample of my code :
expandableListDetailsLevel.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
expandableListDetailsLevel
.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
if(groupPosition!=1){
expandableIsThere = false;
}
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
expandableListDetailsLevel.collapseGroup(i);
}
}
}
});
expandableListDetailsLevel
.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent,
View v, int groupPosition, int childPosition,
long id) {
v.setSelected(true);
}
my item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/selector" >
<TextView
android:id="@+id/TVChild"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textColor="#000"
android:textSize="12dp"/>
</LinearLayout>
attribute android:drawSelectorOnTop="true"
in the ExpandableListView element in the xml file (not the item child or group ) like this :
<ExpandableListView
android:id="@+id/expandableViewDetailsBriefing"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#fff"
android:drawSelectorOnTop="true"
android:dividerHeight="1px"
android:childDivider="@color/Gris"
android:indicatorRight="690dp"
android:textFilterEnabled="true" >
</ExpandableListView>
You can save the current view and change the background color of view within your click listeners:
View currentView;
ExpandableListView elv;
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if(currentView != null) {
currentView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
v.setBackgroundColor(Color.parseColor("#EEEEEE"));
currentDrawerView = view;
//do something
return false;
}
});
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if(currentView != null) {
currentView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
v.setBackgroundColor(Color.parseColor("#EEEEEE"));
currentDrawerView = view;
//do something
return false;
}
});