Expandable listview not to select rows - android

i checked the documentation & tried few ways to disable the click of group items of expandable listview. None came to use.
There is a method isChildSelectable in Expandablelistadapter. But not isGroupSelectable. I tried manyways like disabling the click of list item etc.. no luck. I want to disable the click of certain listview group items.

expandableList.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent,
View childView, int groupPosition, long id) {
if (groupPosition == desirePosition) {// give your condition here
//temporary condition is given
return true; // the expander cannot be collapsed
}
return false;
}
});

Related

Expandable list view - collapse an item on expanding another item (Android)

I have used expandable list view to implement dropdown functionality in my navigation drawer items. Right now, if I expand a navigation drawer item, and then expand another one - both item remains expanded. What I want is that when I expand an item, and then expand another one, the previous one collapses.
I have used the below code to try to do that:
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
for (Map.Entry mapElement : expandabilityMap.entrySet()) {
int groupPos = (int) mapElement.getKey();
boolean isExpanded = (boolean) mapElement.getValue();
if (isExpanded) {
parent.collapseGroup(groupPos);
}
expandabilityMap.put(groupPosition, true);
}
return false;
}
});
I have a hashmap named 'expandabilityMap' in which I store the navigation drawer item position as key, and 'true' or 'false' as values - true if the item at that position is expanded, and false otherwise.
Above code is supposed to do the following things on clicking an item in navigation drawer:
Get keys and values from hashmap.
If there is 'true' value then collapse the item at the corresponding position.
Put the currently clicked item position in hashmap along with 'true' value.
Expand the clicked item.
But after running my app, when I expand an item, it expands. But when I try to expand another item, my app crashes.
I get the following exception:
E/MessageQueue-JNI: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
I'm unable to fix this. Please help!
Try this:
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
#Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});

ExpandableListView duplicating checkbox check

hi I am using ExpandableListview in android. My requirement is, at a time one group plus its child group must selected(using check boxes). If I go for another group then the previously selected group should get unchecked.
But From my code when I check first group automatically the last group will check. This is my issue and below is my code. Thanks in advance.
listView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
CheckBox last = holder.groupCHeck;
CheckBox current = (CheckBox) v
.findViewById(R.id.groupCheckBox);
last.setChecked(false);
current.setChecked(true);
if (listExpandedPosition.size() >= 1) {
if (listExpandedPosition.get(0) != groupPosition) {
listView.collapseGroupWithAnimation(listExpandedPosition
.get(0));
last.setChecked(false);
}
} else {
}
if (listView.isGroupExpanded(groupPosition)) {
listView.collapseGroupWithAnimation(groupPosition);
} else {
listView.expandGroupWithAnimation(groupPosition);
}
if (!listExpandedPosition.isEmpty())
listExpandedPosition.clear();
listExpandedPosition.add(groupPosition);
holder.groupCHeck = current;
return true;
}
});
This is a common issue that usually happens with listview,expandable listview and gridview.These widgets doesn't maintain states of checked checkbox.
Have a look at this example,you will get the idea how to deal with that...
http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html
Make a SparseBooleanArray or Arraylist and hold Checked and Unchecked states in the array itself and set it accordingly, Or if you are using a custom Java Pojo/Object add another field for getting setting a boolean for checked/unchecked state. This is a common scenario because AdapterView recycle views and items for saving memory.

How to disable all elements (also the ones not currently visible in the screen) in a listview?

I have a ListView with a set of elements. When I click one of the I would like to disable all the others. If I click again on the selected item all the other items are enabled again.
I tried the different proposed solution without success. I hope someone can help me.
This is my code:
//action to take when a presentation is selected
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
if(position!=selectedPresentation){
for(int i=0;i<parent.getCount();i++){
if(i!=position)//disable all the items except the select one
parent.getChildAt(i).setEnabled(false);
}
selectedPresentation=position;
}else if(selectedPresentation==position){
//enable again all the items
for(int i=0;i<parent.getCount();i++){
parent.getChildAt(i).setEnabled(true);
}
selectedPresentation=-1;
}
Where selectedPresentation is a global variable storing the selected item. If no item is selected its value is -1.
Thank you for your help!
Make your own subclass of ArrayAdapter that has AreAllItemsEnabled() return false, and define isEnabled(int position) to return false for a given item in your the ones you want to disable.
In your custom ArrayAdapter overide isEnabled method as following
#Override
public boolean isEnabled(int position) {
return false;
}
other option
Don't implement onItemclickListener. This will not give you any update of item click. Only register onClick listener to views.
You may take another object of the List(Arraylist) which is populating elements in listview then on click copy the corresponding position data to new arraylist you made and then notifyDataSet and when you click again you populate listview with original list so it will apear again....Just do this trick it might work for you
parent.getChildeAt() only work for visible Item.
you should change something in adapter.
if make custom adapter then you can do some thing like #ṁᾶƔƏň ツ answer, but if you use default adapter you can change adapter's arraylist that before you pass it to adapter.
put boolean in it (in arraylist) and in on item click true/false it for all item.
I wrote this solution and seems it works fine!
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
//disable the other items when one is selected
Toast.makeText(context, "onClick called",Toast.LENGTH_SHORT).show(); // this will flash up twice
if(position!=selectedPresentation){
selectedPresentation=position;
for(int i=0;i<adapter.getCount();i++){
if(i!=position)//disable all the items except the select one
adapter.isEnabled(i);
}
}else if(selectedPresentation==position){
//enable again all the items
selectedPresentation=-1;
for(int i=0;i<adapter.getCount();i++){
adapter.isEnabled(i);
}
}
And in my adapter I wrote:
public boolean isEnabled(int position) {
if(selectedPresentation==-1 || selectedPresentation==position)
return true;
return false;
Now my concern is how to show the items in the listView as disabled

How to highlight multiple items in a list view?

Hello guys I want to highlight multiple items in a list view.
So I tried SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); but it didn't help
I am using a custom adapter and extending BaseAdapter
I am using ListView and not AbsListView
I don't want to use CAB because it doesn't go well with the design of my app
I don't want to use the getView method of the adapter as well.
I don't want to use checkboxes either, I guess I will use a boolean for each item and pass it to getviews if I don't get a solution here, but that doesn't seem too elegant and neat. I believe there is a proper built-in way of doing it without using getview() of the adapter
I tried:
android:drawSelectorOnTop="false"
android:listSelector="#android:color/darker_gray"
in the xml but it is highlighting only one of the items and as soon as I click on another item, it highlights it instead...
So is there any proper way of doing it?
Here is how my app looks:
You can make the same logic as CAB but avoid using CAB.
Your list item should have the FrameLayout at root like
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:attr/activatedBackgroundIndicator">
....
Set onItemClickListener to change choice mode on long press
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
// if already in multi choice - do nothing
return false;
}
mInMultiChoiceMode = true;
// set checked selected item and enter multi selection mode
final AbsListView list = (AbsListView) arg0;
list.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
list.setItemChecked(arg2, true);
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (mInMultiChoiceMode) {
//exit multi choice mode if number of selected items is 0
if (((AbsListView) arg0).getCheckedItemCount() == 0) {
((AbsListView) arg0).setChoiceMode(AbsListView.CHOICE_MODE_NONE);
mInMultiChoiceMode = false;
}
} else {
// do whatever you should as in normal non-multi item click
System.out.println("CLICK");
}
}
});
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
It should be enough, but you have to use getView, to differentiate the selected and unselected state.
You can use isItemChecked() method to determine weather the item is selected or not, so you don't have to introduce a boolean variable for each items.
Edit:
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListView list = (ListView) parent;
if(list.isItemChecked(position)){
//...
}
else{
//...
}
use
SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then manually do it in the adapter.

ListView ItemClick ItemLong Click Issue android

I have a list view in which I have implemented both onItemClick and onItemLongClick.
The list view row is custom where there is checkbox which is selected when ItemClick happens.
Now WHen User does a long click, I am still getting a ItemClick which selects the checkbox and this looks odd as the user is trying to do something else.
How to fix this
Set an OnItemLongClickListener for your list view.
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
/*Make sure to return true so that the event will be consumed here, and not
propogated to the onListItemClick listener.*/
return true;
}
});
See similar answer here.

Categories

Resources