public class CustomExpandableListAdapter extends BaseExpandableListAdapter
implements View.OnClickListener {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
private Button cancel;
private Drawable resId;
private static ClaimActivity cA = new ClaimActivity();
public static Button selectedButton;
public static ProgressBar loadingCircle;
public CustomExpandableListAdapter(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);
cancel = (Button) convertView.findViewById(R.id.CancelButton);
cancel.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
try {
ClaimActivity.setUpConfirmationAlertDialog(ClaimActivity.context);
} catch (Exception e) {
e.printStackTrace();
}
}
});
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(final int listPosition, boolean isExpanded, // getGroupView sets the title for the expandableListView ( Tag Id, Store Room, Room Number)
View convertView, ViewGroup parent) {
Log.w("Convert View", String.valueOf(convertView));
String listTitle = (String) getGroup(listPosition);
GroupHolder mHolder;
ExpandableListView exListView = (ExpandableListView) parent;
//Create new View is one does not exist
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
mHolder = new GroupHolder();
mHolder.searchButton = (Button) convertView.findViewById(R.id.SearchButton);
mHolder.retrieveButton = (Button) convertView.findViewById(R.id.RetreivedButton);
mHolder.deliverButton = (Button) convertView.findViewById(R.id.deliveredButton);
mHolder.loadingCircle = (ProgressBar) convertView.findViewById(R.id.loadingCircle);
mHolder.listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);
convertView.setTag(mHolder);
}
mHolder = (GroupHolder) convertView.getTag();
// Setting all variables in
mHolder.listTitleTextView.setText(listTitle);
mHolder.searchButton.setFocusable(false);
mHolder.retrieveButton.setFocusable(false);
mHolder.deliverButton.setFocusable(false);
mHolder.searchButton.setOnClickListener(this);
mHolder.deliverButton.setOnClickListener(this);
mHolder.retrieveButton.setOnClickListener(this);
return convertView;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
//Getting Button position
View parentRow = (View) v.getParent();
ExpandableListView listView = (ExpandableListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
int expandedRowsCount = 0;
for (int i = 0; i < position; i++) {
if (listView.isGroupExpanded(i)) {
expandedRowsCount++;
}
}
int newPosition = position - expandedRowsCount;
loadingCircle = (ProgressBar) parentRow.findViewById(R.id.loadingCircle);
int randomNum = ThreadLocalRandom.current().nextInt(0, 1 + 1);
switch (v.getId()) {
case R.id.SearchButton:
selectedButton = (Button) parentRow.findViewById(R.id.SearchButton);
try {
if (randomNum == 0) {
resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_green);
buttonCheck(1);
} else {
resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_grey);
}
} catch (Exception e) {
e.printStackTrace();
}
selectedButton.setBackgroundDrawable(resId);
/*else {
try {
ClaimActivity.showTagErrorDialog(ClaimActivity.context);
} catch (Exception e) {
e.printStackTrace();
}
}*/
break;
case R.id.deliveredButton:
selectedButton = (Button) parentRow.findViewById(R.id.deliveredButton);
try {
if (randomNum == 0) {
resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_green);
buttonCheck(1);
} else {
resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_grey);
}
} catch (Exception e) {
e.printStackTrace();
}
selectedButton.setBackgroundDrawable(resId);
break;
case R.id.RetreivedButton:
try {
selectedButton = (Button) parentRow.findViewById(R.id.RetreivedButton);
if (randomNum == 0) {
resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_green);
buttonCheck(2);
} else {
resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_grey);
}
selectedButton.setBackgroundDrawable(resId);
} catch(Exception e){
e.printStackTrace();
}
break;
default:
break;
}
}
static class GroupHolder {
public Button searchButton, retrieveButton, deliverButton;
public ProgressBar loadingCircle;
public TextView listTitleTextView;
}
public static void showTagErrorDialogw(Context a) {
try {
final AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(a, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(a);
}
LayoutInflater inflater = LayoutInflater.from(a);
View dialogView = inflater.inflate(R.layout.custom_dialog_error, null);
builder.setView(dialogView);
TextView tv = (TextView) dialogView.findViewById(R.id.Confirmation_Texts);
tv.setText("Please stop the current search\nBefore starting a new one.");
final AlertDialog alertDialog = builder.create();
Button cancelButton = (Button) dialogView.findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
alertDialog.dismiss();
}
});
WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
lp.dimAmount = 0.1f;
alertDialog.show();
} catch (Exception ex) {
}
}
}
The above would be the code for my custom ExpandableListAdapter. It consists of the group view having a title and 3 separate buttons (search, deliver and retrieve), and a child view consisting of just text. For now the buttons should turn from green to red and vice versa randomly on clicks, which is working. Pre-Expand:
Post-Expand:
But the problem comes when I expand the List View, and the color on the buttons switch with the buttons on another row. It seems that it switches with the same row everytime, but I am not sure why... Any help would be appreciated!
Well in the end I decided to try out idunnololz's AnimatedExpandableListView in the project and it worked! I still don't know why the original ExpandableListView was bugging out, though I think it is probably has something to do with the recycling of the listview messing with the buttons.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.