I'm right now trying to set up an ExpandableListView inside my fragment with following behaviour. I have three Groups with CheckedTextViews:
- Brands (VW,AUDI, SEAT ...)
- Types (Polo,Golf, A3, A2, Ibiza ...)
- Colors (Red, Green, Blue ...)
On expanding one group the other groups should close (already managed). On click on one child of one group it is shown as selected (already managed).
This is were I'm stuck right now
On click on another child of the same group the other item should be deselected. Also automatically the next group should open after for example
- If child 1 in group 1 is selected and child 2 in group 1 is clicked
- Child 1 should be deselected and
- Group 2 should expand
- Now you have to select child in group 2 (which also deselects the other selected child in this group)
- Group 3 opens
- Select child of group 3 --> all groups collapse
In future the Arraylists will be retrieved dynamically from the server. After this there should be a behaviour to show only cartypes of the selected brand in the first group. Perhaps you also have an idea how to solve this.
My code is
package de.tubs.cs.ibr.androidlab.flexcarpool.ui;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import de.tubs.cs.ibr.androidlab.flexcarpool.R;
public class ConfigureCarFragment extends Fragment implements View.OnClickListener {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
private int lastExpandedPosition = -1;
private String[] mBrand;
private String[] mTypes;
private String[] mColors;
private Button configure_car;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_car_configure, container, false);
setHasOptionsMenu(true);
mColors = getResources().getStringArray(R.array.colors);
mBrand = getResources().getStringArray(R.array.brands);
mTypes = getResources().getStringArray(R.array.types);
expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);
((MainActivity) getActivity())
.setActionBarTitle(getString(R.string.profil));
expandableListDetail = ConfigureCarData.getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new ConfigureCarAdapter( (MainActivity)getContext(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
long childcount = expandableListAdapter.getChildrenCount(groupPosition);
CheckedTextView checkbox = (CheckedTextView) v.findViewById(R.id.expandedListItem);
if (checkbox.isChecked()) {
checkbox.setCheckMarkDrawable(null);
checkbox.setChecked(false);
} else {
checkbox.setCheckMarkDrawable(R.drawable.ic_check_black_24dp);
checkbox.setChecked(true);
}
return true;
}
});
configure_car = view.findViewById(R.id.configure_car);
configure_car.setOnClickListener(this);
((MainActivity) getActivity())
.setActionBarTitle(getString(R.string.configure_car));
return view;
}
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.configure_car:
((MainActivity) getActivity()).getSupportFragmentManager().popBackStack();
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
getActivity().setTitle(R.string.login);
}
}
The code of the Adapter is
package de.tubs.cs.ibr.androidlab.flexcarpool.ui;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import de.tubs.cs.ibr.androidlab.flexcarpool.R;
public class ConfigureCarAdapter extends BaseExpandableListAdapter{
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public ConfigureCarAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.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);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.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) {
String listTitle = (String) getGroup(listPosition);
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);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
Thanks for your help, I'm quit a newbie so please be patient!
Try below code:
fragment_car_configure.xml:
list_group1.xml:
list_item1.xml:
ConfigureCarFragment.java:
ConfigureCarAdapter.java:
Hope it helps!