Remove ListView items after 3 clicks - java

How to remove ListView items from ListView on Android?
But, I want to set a click on each item to 3 before removing it.
So if the item at the first position is clicked once and the second item clicked twice, do not remove any item until the first item click reaches 3. Then remove that item only and for other items in the ListView, each has to be clicked 3 times.
listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
final PopupMenu pop = new PopupMenu(Danger.this, listi);
pop.getMenuInflater().inflate(R.menu.menu_location, pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Remove:
items.remove(position);
}//swithc
return false;

Create an ArrayList of integers and initialize it with exactly with same element count of your list view and set value of all elements of the list = 0
ArrayList<integers> counterList = new Arraylist();
for(int i = 0; i < listi.getAdapter.getChildrenCount(); i++){ // get total elements in adapter
counterList.add(0); // set each element of array list to 0
}
Then here:
listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
final PopupMenu pop = new PopupMenu(Danger.this, listi);
pop.getMenuInflater().inflate(R.menu.menu_location, pop.getMenu());
pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Remove:
if(counterList.get(position) >= 2){
items.remove(position); // remove current position item from arraylist adapter and notify data set changed
counterList.remove(position); // remove the current position element from counter list too
} else {
counterList.set(position, counterList.get(position) + 1); // if 3 clicks have not happened then increase the counter.
}
}//swithc
return false;

Use static variable and hold it with null,Include the static variable inside ClickListener , Once the variable reaches 3,you can remove the items from list view

I would suggest adding 2 fields: int timesClicked and int lastItemId, then in your onMenuItemClick method, check if the Item needs to be removed:
int timesClicked = 0;
int lastItemId;
...
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() == lastItemId) {
if(timesClicked == 3) {
timesCliked == 0;
items.remove(item);
} else timesClicked++;
} else {
lastItemId = item.getItemId();
timesClicked = 0;
}
}
This should work.

Make a bean (Model) class for having record of clicked item according to
their corresponding position and every time on item clicked first check that
how many time this item is clicked if it's returning 2 then remove the item
else not.

Related

RecyclerView radio button won't register only the latest checked item (remember previous too)

I have RecyclerView which has multiple items, those items contain radio buttons, course name (TextView) and hole number (TextView).
What supposes to happen is that when I click the radio button, it should select only that 1, and if there are the previous check it should uncheck it (single selection). Well at the moment It checks only 1 at a time which is ok (so frontend works as it should), but what happens "under the hood"... example:
There are currently 3 items in RecyclerView. I click the third item to be checked, then I click first, and again third. now I click "start game" button, what should happen is that it should take that lastly checked item (in this case the third left lastly selected) and intent its course name and hole number to the next Activity, but instead what happens now is that it intent the first items course name and hole number... Also if I do the same as previous, but instead of clicking first item, I click second, then even tho lastly I clicked that third item, instead intent its course name and hole number, it intents that second items... So it always intents that item which is clicked (at some point) and from those clicked items it checks the first 1 (from top to bottom of a list) and take its intents.
Here is my Adapter where I'm checking which 1 is selected:
public class NewGameCourseAdapter extends RecyclerView.Adapter<NewGameCourseAdapter.NewGameCourseViewHolder> {
private ArrayList<NewGameCourseItem> mCourseList;
private NewGamePlayerAdapter.OnItemsCheckStateListener checkStateListener;
private NewGameCourseAdapter.OnItemClickListener itemClickListener;
public void setOnItemClickListener(NewGameCourseAdapter.OnItemClickListener listener) {
itemClickListener = listener;
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemsCheckStateListener(NewGamePlayerAdapter.OnItemsCheckStateListener checkStateListener) {
this.checkStateListener = checkStateListener;
}
public static class NewGameCourseViewHolder extends RecyclerView.ViewHolder {
public RadioButton mRadioButton;
public NewGameCourseViewHolder(#NonNull View itemView, final NewGameCourseAdapter.OnItemClickListener listener) {
super(itemView);
mRadioButton = itemView.findViewById(R.id.radioButton);
}
}
onBindViewHolder:
#Override
public void onBindViewHolder(#NonNull final NewGameCourseViewHolder holder, final int position) {
final NewGameCourseItem currentItem = mCourseList.get(position);
/** This can prevent some unwanted actions in some cases **/
holder.mRadioButton.setOnCheckedChangeListener(null);
holder.mRadioButton.setChecked(selectedPosition == position);
holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
notifyItemChanged(selectedPosition);
selectedPosition = holder.getAdapterPosition();
notifyItemChanged(selectedPosition);
if (itemClickListener != null) {
itemClickListener.onItemClick(position);
}
}
});
This is the Activity where I do the intent:
mStartGame = findViewById(R.id.button_start_game);
mStartGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 0; i < mCourseList.size(); i++) {
/** If radio button is selected, then intent to ActivityGame **/
if (mCourseList.get(i).getRadioButton() == true) {
Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
/** Also intent selected items course name and hole number **/
intent.putExtra("COURSENAME", mCourseList.get(i).getCourseName());
intent.putExtra("HOLESNM", mCourseList.get(i).getHolesNm());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
break;
}
}
}
});
Summary: in frontend it looks correct, it selects only that radio button which is lastly clicked and uncheck the previous (as it should), but inside, it won't "forget" the previous selections as it should do...
You are doing something weird, if possible make that simple.
Step 1: Create a method to get the selected item in your adapter class
public NewGameCourseItem getSelectedItem() {
return mCourseList.get(selectedPosition);
}
Step 2: Then modify your on click method like below
mStartGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NewGameCourseItem item = adapter.getSelecteditem();
Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
/** Also intent selected items: course name and hole number **/
intent.putExtra("COURSENAME", item.getCourseName());
intent.putExtra("HOLESNM", item.getHolesNm());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});
Step 3: Now modify your onCheckedChanged as below
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
notifyItemChanged(selectedPosition);
selectedPosition = holder.getAdapterPosition();
notifyItemChanged(selectedPosition);
}
Create a class variable in adapter
private int selectedPosition = -1; //change -1 to any default position, need to be selected.
Modify the below function in adapter
#Override
public void onBindViewHolder(#NonNull final NewGameCourseViewHolder holder, final int position) {
final NewGameCourseItem currentItem = mCourseList.get(position);
holder.mRadioButton.setChecked(selectedPosition == position);
holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked) {
int tempSelected = selectedPosition;
selectedPosition = position;
notifyDataSetChanged();
}
}
});
}
Create a new method in adapter as below-
public int getSelectedItemIndex() {
return selectedPosition;
}
I assume adapter is the variable of NewGameCourseAdapter in the Activity class. Modify the button click in the Activity:
mStartGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selectedRecyclerItemPosition = adapter.getSelectedItemIndex();
//Calling intent and pass selected item info
Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
/** Also intent selected items course name and hole number **/
intent.putExtra("COURSENAME", mCourseList.get(selectedRecyclerItemPosition).getCourseName());
intent.putExtra("HOLESNM", mCourseList.get(selectedRecyclerItemPosition).getHolesNm());
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
});

Getting the selected item's position in Android popup menu class

I want to store some data into every item in a popup menu. All of the items are inflated programatically in a for loop based on results returned from a feed.
In the following example, I use a HashMap storedOption to store each item's data with the loop indices as keys. But I need to find a way to get the position of the selected item in onMenuItemClick so that I can retrieve the data from storedOption. Can anyone tell me how to do that? Besides the following attempt, I have also tried item.getOrder() but it always returns 0 regardless of how many items it has in the menu.
public DynamicPopUpMenu{
private Map<String,FeatureList> storedOption = new HashMap();
public void main(final Context context,List<FeatureList> featureList){
int count = 0;
PopupMenu menu = new PopupMenu(context, featureList);
for(FeatureList f:featureList) {
MenuItem item = menu.getMenu().add(f.key);
storedOption.put(count, f);
count++;
}
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
new ShowToast(context,Integer.toString(position)); // show position in a toast
return true;
}
});
menu.show();
}
}
You could use featureList.key as the key of your storeOption and them use item.getItemId(); to get the value from storeOption.
Like this:
public DynamicPopUpMenu{
private Map<String,FeatureList> storedOption = new HashMap();
public static void main(final Context context,List<FeatureList> featureList){
int count = 0;
PopupMenu menu = new PopupMenu(context, featureList);
for(FeatureList f:featureList) {
MenuItem item = menu.getMenu().add(f.key);
storedOption.put(f.key, f);
count++;
}
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int id = item.getItemId();
FeatureList mFeatureList = (FeatureList)storedOption(id)
new ShowToast(context,Integer.toString(value)); // show position in a toast
return true;
}
});
menu.show();
}
}

Less than or Equal Condition Android

I have one quote application which has a "Load more" button visible only if the quote list size is 15. Now I want change the condition so that it must show the button only if the quote list size is more than 15. My current code is like below and I have tried to change it to:
if(c.getCount()<=15){
// Not Showing Load More Button
}
but it's not showing my button.
My code for that button is below:
final Button btnLoadMore=new Button(this);
btnLoadMore.setBackgroundColor(Color.parseColor("#351802"));
btnLoadMore.setTextColor(Color.parseColor("#e8d8a7"));
btnLoadMore.setTypeface(btnLoadMore.getTypeface(), Typeface.BOLD);
btnLoadMore.setText("Load More Quotes");
if(c.getCount()<15){
// Not Showing Load More Button
}
else {
list.addFooterView(btnLoadMore);}
list.setAdapter(adapter);
anifadein=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slidedown);
list.startAnimation(anifadein);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
map = quotesList.get(position);
Intent intent = new Intent(QuotesActivity.this,
QuoteDialogActivity.class);
int itemPosition = position;
if(startingPoint>=30){
intent.putExtra("Pos", itemPosition+1);
intent.putExtra("LstCount", list.getCount()-1);
}else{
intent.putExtra("Pos", itemPosition+1);
intent.putExtra("LstCount", list.getCount());}
intent.putExtra("QuoteId", map.get(KEY_ID));
intent.putExtra("quotesType", quType);
intent.putExtra("startFrom", getIntent().getStringExtra("startFrom"));
intent.putExtra("Quotes", quotesList);
// Log.i("COUNT",""+(itemPosition+1)+"-"+list.getCount());
intent.putExtra("Fav", map.get(KEY_FAVORITE));
startActivity(intent);
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
});
btnLoadMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(btnLoadMore.getVisibility()==View.VISIBLE){
Cursor newC = null;
if (quType != 0) {
switch (quType) {
case 1:
newC = db.getQuotes(""+startingPoint);
break;
case 2:
newC = db.getFavoriteQuotes(""+startingPoint);
//page.setVisibility(View.GONE);
break;
case 3:
newC = db.getAuthorQuotes(getIntent().getStringExtra("AuthorId"),""+startingPoint);
// page.setVisibility(View.GONE);
break;
}
}
// Starting a new async task
if(newC.getCount()<15){
btnLoadMore.setVisibility(View.INVISIBLE);
}
startingPoint+=15;
do{
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, newC.getString(newC.getColumnIndex(KEY_ID)));
map.put(KEY_TEXT, newC.getString(newC.getColumnIndex(KEY_TEXT)));
map.put(KEY_AUTHOR, newC.getString(newC.getColumnIndex(KEY_AUTHOR)));
map.put(KEY_PICTURE, newC.getString(newC.getColumnIndex(KEY_PICTURE)));
map.put(KEY_PICTURE_SDCARD, String.valueOf(newC.getInt(newC
.getColumnIndex(KEY_PICTURE_SDCARD))));
map.put(KEY_WEB_ID,
String.valueOf(newC.getInt(newC.getColumnIndex(KEY_WEB_ID))));
//Log.i("web_id",String.valueOf(newC.getInt(newC.getColumnIndex(KEY_ID))));
map.put(KEY_FAVORITE, newC.getString(newC.getColumnIndex(KEY_FAVORITE)));
// adding HashList to ArrayList
quotesList.add(map);
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
} while (newC.moveToNext());
adapter.notifyDataSetChanged(); }}
});
}
}
I not sure from where Object "c" is coming but i m assuming it list object and getCount is return the current count of list. So, If you want to show "Show more" button when the size of list is greater than or equals to 15 then use this condition.
if(c.getCount() >= 15){
// Will show Load More Button
}
Put the Button in a layout XML File. Inflate it, then:
if (c.getCount() > 15) {
btnLoadMore.setVisibility(View.INVISIBLE);
}
If you want the Button to be invisible.

Handle checked and then unchecked checkbox's value in listview android

In my activity I have 4 items in a list view with checkboxes in each row. After I leave activity I want all the checked item position numbers in an arraylist. This code works fine when user clicks all the checkboxes ones and leave the activity. But when user clicks a checkbox and then unchecks it, item position is arraylist doesnt able to clear. This methood is in my CustomAdapter class.
class GameAdapter extends BaseAdapter{
private ArrayList<Integer> positions = new ArrayList<Integer>();
public ArrayList<Integer> getPositions() {
return positions;
}
getView(....){
final ViewHolder holder;
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(holder.checkBox.isChecked()) {
positions.add(position);
}
}
});
}
}
I am handling this adapter class in the activity where my listview resides. There I am taking this arraylist values like this
ArrayList<Integer> gamePositions = mGameAdapter.getPositions();
String itemNumbers = gamePositions.toString();
in itemNumber string I want all the checked item number values.
Thank you in advance for guidance....
Replace this
if(holder.checkBox.isChecked()) {
positions.add(position);
}
with this
if(holder.checkBox.isChecked()) {
if(!positions.contains(position))
positions.add(position);
} else {
if(positions.contains(position))
positions.remove(position);
}
Follow below approach.
Use HashMap instead of Arraylist, like below
HashMap<Integer,Boolean> mapOfSelection = new HashMap<>();
before onClick method, assign position to the checkbox, like below.
holder.checkbox.setTag(position);
In onClick, do the following.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mapOfSelection.put((Integer)buttonView.getTag(),isChecked); // True if this position/value is checked, false otherwise.
}
Now when you need the selected items, just loop through the hashmap and check the positions/Key which have True value.

ExpandableListView open collapse problem

I am using a custom expandable list adapter. I highlight a child when the user clicks on it. This works fine until the user opens/collapses a group. Say the user touches Group 2 Item 1. This highlights Group 2 Item 1. Then the user opens Group 1. Now Group 3 Item 2 is highlighted. I have done some testing choosing different items and I can not find a pattern where the highlighted row will jump to. Sometimes its up in the list, sometimes its down. I'm having trouble figuring out the logic to put in my activity's onGroupExpandListener and onGroupCollapseListener to rehighlight the correct view. Any ideas?
EDIT: Current code inside my onChildClickListener
if (groupPosition == 0){
switch(childPosition) {
case 0:
previouslySelectedView.setBackgroundResource(R.color.transparent);
currentlySelectedView.setBackgroundResource(R.color.blue);
break;
Same code for all the groups/children
Item slection in ExpandableListView is made through flat list (absolute positiond). Thus if the newlly opened group is before the currenet selection and has fewer children then the selection will move up and vice versa. I suggest to set choice mode to none and implement onclick/expand logic to handle focus on your own - implement tags for views, for example, and set the currently highlighted item via the tag.
Here are few suggestions:
In the ExpandableListView.OnChildClickListener first you perform ExpandableListView.findViewWithTag(theTag) to check for views with such tag and unmark them (also setTag(null)) and restore the background. Then for the item clicked setTag(theTag) and change the background to selected. Of course you can have some other logic and have multiple items marked. Note that once the view is destroyed you will lose the selection (for example during expands).
Have some custom map or something that will hold a unique ID of the view and the (un)marked state. That's the best solution that will allow to maintain persistent selection across scrolls and expands.
In the backing adapter introduce a "marked" state. Thus the marking will be persistent even between application start/stop. Not a good approach, though, because selection is more of a UI behaviour.
I am currently doing an ExpandableListView selection with Choice Mode Multiple of the list. But since, as I said, the selection is per position I had to sacrifice in terms of functionality - namely, I clear the selection whenever a manipulation is made. The previous implementation was with custom Map holding the selected IDs and - to honest - it was the better approach.
This is how I get selected IDs (remember I use choice mode multiple):
final SparseBooleanArray checkedPositions = expList.getCheckedItemPositions();
final ExpandableListAdapter adapter = expList.getExpandableListAdapter();
List<Long> checkedIds = new ArrayList<Long>();
if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
for (int i = checkedPositions.size() - 1; i >= 0; i--) {
if (checkedPositions.valueAt(i)) {
checkedIds.add(adapter.getGroupId(checkedPositions.keyAt(i)));
}
}
}
In your case, though, you will want to check for CHILD packed positions. Also note that my adapter has stable (unique) ids. If you do not have stable IDs then you can rely on ExpandableListView's getPackedPositionForChild() method and store the packed position as marked.
Right way to do it (Will solve jumping highlight upon group collapse)
Do the following on ExpandableListView:
Step 1. Set choice mode to single (Can be done in xml as android:choiceMode = "singleChoice")
Step 2. Use a selector xml as background (android:listSelector = "#drawable/selector_list_item")
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#android:color/holo_blue_bright" android:state_pressed="true"/>
<item android:drawable="#android:color/holo_blue_light" android:state_selected="true"/>
<item android:drawable="#android:color/holo_blue_dark" android:state_activated="true"/>
</selector>
Step 3. Call expandableListView.setItemChecked(index,true) in onChildClick() callback.
index is a 0 based index of the child item calculated as follows
Group 1 [index = 0]
Child item 1
Child item 2
Child item 3 [index = 3]
Group 2 [index = 4]
Child item 1
Child item 2
Child item 3 [index = 7]
Group 3 [index = 8]
Child item 1
Child item 2
Child item 3 [index = 11]
If we have list headers as well, they would also account for index values.
Here is a working example:
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
...
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
return true;
}
My solution for the collapse issue :
mDrawerExpandableList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
if(ExpandableListView.getPackedPositionGroup(selectedFragmentId) == groupPosition) {
mDrawerExpandableList.setItemChecked(mDrawerExpandableList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition)), true);
}
}
});
mDrawerExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if(ExpandableListView.getPackedPositionGroup(selectedFragmentId) == groupPosition) {
mDrawerExpandableList.setItemChecked(((ExpandableListView)drawerListView).getFlatListPosition(selectedFragmentId), true);
}
}
});
//MY LIST A FOLLOWS
(0)SALES
(0)SALES
(1)SALES VIEW
(2)SALES RETURN
(3)SALES RETURN VIEW
(1)STOCK
(0)ADD STOCK
(1)VIEW STOCK
(2)SETTINGS
(3)NOTIFICATION
//declare tow integer variable globali as follows
private int lastCheckedGroupPosition=-1;
private int lastCheckedChildPosition=-1;
mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if(groupPosition==2){
lastCheckedGroupPosition=groupPosition;
lastCheckedChildPosition=-1;
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
parent.setItemChecked(index, true);
//TODO Write here the code for opening settings page
...............
...............
...............
return true;
}
else if(groupPosition==3){
lastCheckedGroupPosition=groupPosition;
lastCheckedChildPosition=-1;
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
parent.setItemChecked(index, true);
//TODO Write here the code for opening settings page
...............
...............
...............
return true;
}
return false;
}
});
mDrawerList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if(groupPosition==0){
if(childPosition==0){
//TODO Write here the code for opening SALES page
...............
...............
...............
}
else if(childPosition==1){
//TODO Write here the code for opening SALES VIEW page
...............
...............
...............
}
else if(childPosition==2){
//TODO Write here the code for opening SALES RETURN page
...............
...............
...............
}
else if(childPosition==3){
//TODO Write here the code for opening SALES RETURN VIEW page
...............
...............
...............
}
}
else if(groupPosition==1){
if(childPosition==0){
//TODO Write here the code for opening ADD STOCK page
...............
...............
...............
}
else if(childPosition==1){
//TODO Write here the code for opening VIEW STOCK page
...............
...............
...............
}
}
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
lastCheckedGroupPosition=groupPosition;
lastCheckedChildPosition=childPosition;
return false;
}
});
mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if(groupPosition==lastCheckedGroupPosition){
if(lastCheckedChildPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForChild(lastCheckedGroupPosition, lastCheckedChildPosition));
mDrawerList.setItemChecked(index, true);
}
else{
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
}
else{
if(mDrawerList.isGroupExpanded(lastCheckedGroupPosition)){
if(lastCheckedChildPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForChild(lastCheckedGroupPosition, lastCheckedChildPosition));
mDrawerList.setItemChecked(index, true);
}
else{
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
}
else{
mDrawerList.setItemChecked(-1, true);
}
}
}
});
mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
if(groupPosition==lastCheckedGroupPosition){
if(lastCheckedGroupPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
else{
mDrawerList.setItemChecked(-1, true);
}
}
if(mDrawerList.isGroupExpanded(lastCheckedGroupPosition)){
if(lastCheckedChildPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForChild(lastCheckedGroupPosition, lastCheckedChildPosition));
mDrawerList.setItemChecked(index, true);
}
else{
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
}
else{
mDrawerList.setItemChecked(-1, true);
}
}
});

Categories