Implemented ExpandableListview, I have ImageView on the childView and it should be VISIBLE on the group first child and for other ChildView should be Hidden And after reloading the listView how can I hold the Image status of childView.
Find the below code which I implemented
filterChild.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/lblListItemFilter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/check"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="left"
android:layout_marginEnd="16dp"
android:background="@drawable/checkmark"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Listeners in Activity
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
System.out.println("childPosition"+listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition));
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return true;
}
});
BaseExpandableListAdapter Class
package com.conduent.conduentebi;
import android.content.Context;
import android.graphics.Typeface;
import android.icu.util.ULocale;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class FilterExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private HashMap<Integer, boolean[]> mChildCheckStates;
private List<ULocale.Category> mGroupCollection;
public FilterExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
mChildCheckStates = new HashMap<Integer, boolean[]>();
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
ChildViewHolder viewHolder = null;
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.filter_child, null);
System.out.println("mChildPosition"+mChildPosition);
viewHolder = new ChildViewHolder();
// viewHolder.checkBox1 = (CheckBox) convertView.findViewById(R.id.check);
// viewHolder.checkBox1.setFocusable(false);
viewHolder.mChildText = (TextView) convertView
.findViewById(R.id.lblListItemFilter);
//viewHolder.checkBox1 = (CheckBox)convertView.findViewById(R.id.check);
convertView.setTag(R.layout.filter_child, viewHolder);
//viewHolder.checkBox2 = (CheckBox)convertView.findViewById(R.id.check);
}else {
viewHolder = (ChildViewHolder) convertView
.getTag(R.layout.filter_child);
}
// TextView txtListChild = (TextView) convertView
// .findViewById(R.id.lblListItemFilter);
viewHolder.mChildText.setText(childText);
convertView.setClickable(false);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.filter_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeaderFilter);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
convertView.setClickable(false);
return convertView;
}
class ChildViewHolder{
TextView mChildText;
ImageView checkBox1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
If you want to hold the old state of the view and show the view accordingly when you reload the screen i would suggest you construct a model to hold your data and make a list of that.
And for any change you should use the list of that model for updated data. This will help you to maintain the state of the different views in your list item.