How to Customized Expandable List View? - android

I want to customize expandable list view such a way that when user clicks on any group view of that expandable list view then a dialog will be shown but list view will not expand.After selecting some thing from the dialog when user click on ok of the dialog the list view will expand and will show data on the base on what selected from dialog.
Can anybody give me any idea how can I implement it?

Try the following, it should do the trick:
mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
boolean shouldIExpandGroups = doSomething();
if (shouldIExpandGroups)
return false; // This will cause ListView to expand
else
return true; // This will cause ListView to ignore click
}
});

Related

How to perform ExpandableListView child click

I create an Expandable ListView which contains 3 group and each group have several child.
In OnCreate i expand group one with exLv.expandGroup(0); then i want to click child of this group without user touch it (dynamically).
I tried exLv.setSelectedChild and it didn't work.
I just want to do this one time at the onCreate. How to do this?
In your activity class override the OnChildClickListener() method from list view.
expandbleList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//do something here.
}
});

How to make an ExpandableListView in Android always show child items

I want to make an ExpandableListView which should always be showing its children elements
even after clicking the header, and it should not collapse. How could I implement this concept?
you can expand all groups one by one as below as mentioned in this post
ExpandableListView explst;
explst.expandGroup(0);
explst.expandGroup(1);
for disabling collapse you can define a OnGroupClickListener which returns true, like so: (as mentioned in this post)
explst.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return true; // This way the expander cannot be collapsed
}
});

android OnItemLongClickListener listview to display listview checkboxes

I would like to create an android app which displays a list in a listview and then allow the user to delete an item when on onItemlongclick and then displays checkboxes to select which items to delete.
I know it have to call the OnItemLongClickListener but do not know how to implement that. Any ideas to do this?
lv.setOnItemLongClickListener(this);
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO Auto-generated method stub
return false;
}
I'm so lost in ideas how to do this. I googled but unfortunately didn't find any relevant tutorials.
Help is much appreciated. Thanks.
I did this just recently, but it was an ad hoc fix, so this might not be the best way.
In the layout for my ListView items (rows), I included a CheckBox whose visibility will be toggled, but is initially not visible, i.e., View.GONE.
Then, in my Adapter, I included a member boolean variable, selectable, and a public method to set it and refresh:
private boolean selectable = false;
public void setSelectable(boolean selectable)
{
this.selectable = selectable;
notifyDataSetChanged();
}
In the getView() method of the Adapter, selectable is checked and the CheckBox's visibility is set accordingly.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
...
cbx.setVisibility(selectable ? View.VISIBLE : View.GONE);
...
}
I used a ToggleButton to change the selection mode, but in your case, you'll need to do something a little different. I would add an additional method to the Adapter:
public boolean isSelectable()
{
return selectable;
}
Then, you can toggle the selectable state on long clicks:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int pos, long id)
{
...
yourAdapter.setSelectable(!yourAdapter.isSelectable);
...
}
To respond to clicks on list items, you need to put this in onCreate():
ListView lv = (ListView) findViewById (R.id.list);
lv.setOnItemLongClickListener (this);
lv.setLongClickable (true);
Then, onItemLongClickListener will be called whenever the user long-touches a list item. Put a breakpoint there and be satisfied you understand this much.

need help to custom expandableListView

I am new to android, and I want to implement a ListView like the image below. It seems expandableListView may meet my request, but what I want is only Item 4 has children and can be expanded, 'Item 1' is just an option user can click, and can't be expanded. I read the document of Expandable List View, and it's not clear enough how to implement this. Can anyone gave some suggestion on implementing this? Thank you so much
but what I want is only Item 4 has children and can be expanded
If Item doesn't have children, it won't be exanded. You can image that like:
public class Item {
private String title;
private List<Option> children; // if size = 0 won't be expanded
}
It's native behaviour of ExpandableListView so if any Item doesn't have children, implicitly won't be expanded but you need to set OnClickListener for both groups (Items) and children (Options):
public boolean onChildClick(ExpandableListView parent, View row,
int groupPosition, int childPosition, long id) {
// callback method after click for Options
return false;
}
public boolean onGroupClick(ExpandableListView parent, View row,
int groupPosition, long id) {
/** if you are using BaseAdapter subclass implementation **/
adapter.getGroup(groupPosition)).getChildren() == null) {
// item doesn't have children
}
else {
// has children
}
Generally if you want to get more control over your ListView i pretty recommend you to implement own subclass of ListAdapter in your case you can use BaseExpandableListAdapter.
And an appearance of List is only issue about proper styled layouts for example removing group indicator with
android:groupIndicator="#null"
Update:
Issue mentioned by #Roberto Lombardini:
A little problem with this solution is that the little grey triangle
indicating the state of the groupview (expanded or not) will appear
even if group has no children. Am i wrong?
If you want to have gray triangle only for Items with children, you need to fix it in your custom adapter in getGroupView() method:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// inflating View etc.
Item item = items.get(groupPosition);
if (item != null) {
// if Item has children
if (item.getChildren() != null && !item.getChildren().isEmpty()) {
// set children indicator to VISIBLE
imageView.setVisibility(ImageView.VISIBLE);
}
// if Item doesn't have children
else {
// set children indicator to GONE
imageView.setVisibility(ImageView.GONE);
}
}
}
Try to implement some custom Adapter, I think could be the better way to do what you need
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
switch (position) {
case 0: return true;
case 2: return true; // returning true wont expand
default:
return false;// if you return false then only that position will be expanded
}
}
}
);
just return false on the which parent position you want to expand and in which you dont want to expand you can return true.

How to keep the ExpandableListView Opened?

I am working on the ExpandableListView I have completed the work, now only one thing that I want to do is I don't want the ListView to be DropDown on click of the Expandable List View rather I want to show it opened with all the Items displayed inside without performing any click on them.
Can anyone tell me how can i do that particularly.
Do this for every one of the groups to expand them:
listView1.expandGroup(int groupPosition);
If you want to prevent group collapse, then do this:
listView1.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
{
public boolean onGroupClick(ExpandableListView arg0, View itemView, int itemPosition, long itemId)
{
listView1.expandGroup(itemPosition);
return true;
}
});
Solution:- To Keep The Expandable list in Expanded mode all the times
It is actually very simple you dont need to do anything but just put this one line in your getGroupView of your adapter and your expandablelistview always be in opened/expanded state:--
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
your_expandableListview.expandGroup(groupPosition);
return v;
}
This works for sure... Enjoy..!
Open ExpandableListView Group:
listView.expandGroup(itemPosition);
Collapse ExpandableListView Group:
listView.collapseGroup(itemPosition);

Categories

Resources