@BindView(R.id.expandableListView)
ExpandableListView expandableListView;
List<ExpandableGroupModel> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
ExpandableListAdapter expandableListAdapter;
private int lastExpandedPosition = -1;
add following code in onCreate or where u are getting data
expandableListDetail = new HashMap<String, List<String>>();
List<String> Attractions = new ArrayList<String>();
Attractions.add("Coachin");
Attractions.add("Munnar");
Attractions.add("Thekkadi");
Attractions.add("Alleppy");
List<String> football = new ArrayList<String>();
football.add("Brazil");
football.add("Spain");
football.add("Germany");
football.add("Netherlands");
football.add("Italy");
List<String> basketball = new ArrayList<String>();
basketball.add("United States");
basketball.add("Spain");
basketball.add("Argentina");
basketball.add("France");
basketball.add("Russia");
expandableListDetail.put("visit for", Attractions);
expandableListDetail.put("Seasonality", football);
expandableListDetail.put("Major Attractions", basketball);
expandableListDetail.put("Famous Shopping", football);
expandableListDetail.put("Cuisines", basketball);
expandableListDetail.put("Famous Festivals", basketball);
expandableListDetail.put("Hotels", basketball);
expandableListDetail.put("Price starts from", basketball);
// expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListTitle = new ArrayList<>();
ExpandableGroupModel expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("visit for");
expandableGroupModel.setImage(R.drawable.visit_for);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Seasonality");
expandableGroupModel.setImage(R.drawable.seasonality);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Major Attractions");
expandableGroupModel.setImage(R.drawable.major_attractions);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Famous Shopping");
expandableGroupModel.setImage(R.drawable.famous_shopping);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Cuisines");
expandableGroupModel.setImage(R.drawable.cuisines);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Famous Festivals");
expandableGroupModel.setImage(R.drawable.famous_festivals);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Hotels");
expandableGroupModel.setImage(R.drawable.hotels);
expandableListTitle.add(expandableGroupModel);
expandableGroupModel = new ExpandableGroupModel();
expandableGroupModel.setName("Price starts from");
expandableGroupModel.setImage(R.drawable.price_start);
expandableListTitle.add(expandableGroupModel);
//Set adapter
expandableListAdapter = new CustomExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail, "Dest_detail");
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity(),
expandableListTitle.get(groupPosition).getName() + " List Expanded.",
Toast.LENGTH_SHORT).show();
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity(),
expandableListTitle.get(groupPosition).getName() + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getActivity(),
expandableListTitle.get(groupPosition).getName()
+ " -> "
+ expandableListDetail.get(
expandableListTitle.get(groupPosition).getName()).get(
childPosition), Toast.LENGTH_SHORT
).show();
return false;
}
});
expandableListView.setOnTouchListener(new View.OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Adapter is as follows:
CustomExpandableListAdapter.java
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<ExpandableGroupModel> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
Typeface roboto;
String from;
public CustomExpandableListAdapter(Context context, List<ExpandableGroupModel> expandableListTitle,
HashMap<String, List<String>> expandableListDetail, String from) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
this.from = from;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition).getName())
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(" - " + expandedListText);
roboto = Typeface.createFromAsset(context.getAssets(), "fonts/roboto_regular.ttf");
expandedListTextView.setTypeface(roboto);
if (from.equals("Dest_detail")) {
expandedListTextView.setBackgroundResource(R.color.white);
} else {
expandedListTextView.setBackgroundResource(R.color.screen_bg_color);
}
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition).getName())
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ExpandableGroupModel listObj = (ExpandableGroupModel) getGroup(listPosition);
String listTitle = listObj.getName();
int listimage = listObj.getImage();
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
ImageView img_group_icon = (ImageView) convertView
.findViewById(R.id.img_group_icon);
ImageView img_group_arrow = (ImageView) convertView
.findViewById(R.id.img_group_arrow);
listTitleTextView.setText(listTitle);
img_group_icon.setImageResource(listimage);
roboto = Typeface.createFromAsset(context.getAssets(), "fonts/roboto_regular.ttf");
listTitleTextView.setTypeface(roboto, Typeface.BOLD);
if (isExpanded) {
img_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.up));
listTitleTextView.setTextColor(context.getResources().getColor(R.color.colorPrimary));
} else {
img_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.down));
listTitleTextView.setTextColor(context.getResources().getColor(R.color.textcolor_package));
}
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
ExpandableGroupModel model is as follows:
public class ExpandableGroupModel {
String name;
int image;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}