Reputation: 1589
I am developing an android application, in which I am using Expandablelistview
to show some data. Currently the list view will expand on both clicking on group and on releasing after long click. But, I need to prevent from expanding the Expandablelistview
on long click.
My code segment looks like this:
elvItemList = (ExpandableListView) root.findViewById(R.id.elv_item_list);
elvItemList.setOnGroupClickListener(this);
elvItemList.setAdapter(smListAdapter);
elvItemList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
Utils.logit("SMLOG", "Long button pressed");
//
}
return false;
}
});
can any one help me?
Upvotes: 1
Views: 690
Reputation: 5763
This works for me.
expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return true; // Change return false; to return true;
}
});
Upvotes: 0
Reputation: 28
public void stopExpandOnLongClick()
{
expListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
boolean checkClick=false;
long packedPosition = expListView.getExpandableListPosition(position);
int itemType = ExpandableListView.getPackedPositionType(packedPosition);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
/* if group item clicked */
if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
// ...
if(expListView.isGroupExpanded(groupPosition))
{
Toast.makeText(MainActivity.this, "It's normal group collaspe", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "This Grouo is not going to expand on long click", Toast.LENGTH_LONG).show();
//you can put your logic when you long press on Group header
checkClick=true;
}
}
return checkClick;
}
});
}
Upvotes: 0
Reputation: 22945
ExpandableListView.PACKED_POSITION_TYPE_GROUP
is the id of a group, change it to
ExpandableListView.PACKED_POSITION_TYPE_CHILD
and you can manipulate with longclicks on group childs.
Something like that:
elvItemList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
// Your code with group long click
return true;
}
return false;
}
});
Upvotes: 1