I have this list with some hidden TextView.
When click on a item, I want to slide down smoothly and show the hidden content, and if click again, slide up.
How is that possible?
Hidden and not hidden:
You are looking for ExpandableListView.
Expandable ListView bydefault has two levels. First is called the "Group View" and the second level is called the "Child View". You can simply achieve by making use of the Custom Adapter sample from the very first link I have provided.
here are few links which will get you started,
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
http://about-android.blogspot.in/2010/04/steps-to-implement-expandablelistview.html
http://www.techienjoy.com/android-expandable-list-dynamically-created-example.php
To make only child expanded at a particular time, add this,
explist.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
for(int i=0; i<myExpAdapter.getGroupCount(); i++) {
if(i != groupPosition) {
explist.collapseGroup(i);
}
}
}
});
ExpandableListView solves your problem functionally wise.
However if you want to have the smooth animation i recommend using a library I created myself.
https://github.com/tjerkw/Android-SlideExpandableListView
It works with a normal ListView! And it got smooth animation.
Related
I am trying to achieve the following. My app will have some views. All of the views are simple textViews which when clicked will be redirected to another activity. But the last view needs to expand to show hidden views. the sub views can further have more sub categories. To be more clear i have attached an image to explain this more detail. 1
So here views 1 - 3 are normal text views. view 4 has sub categories sub view 4.1 till 4.3. these can have further sub categories like 4.1.1, 4.1.2. Also when 4.1 is expanded and user clicks on 4.2 to expand it, 4.1 should automatically collapse then 4.2 should expand. I tried using expandable list view for this purpose buy the rest of my views are not list items. Please help me acheive this. Right now m using text vies and using its visibility to hide or show the views. This is not getting me the desired result. Thanks in advance
It is expandable listview you can use following tutorial
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
and for to collapse automatically all except selected one use this code
private int lastExpandedPosition = -1;
private ExpandableListView lv; //your expandable listview
...
lv.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1
&& groupPosition != lastExpandedPosition) {
lv.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
Hopefully it will fullfil your requirements.
You can try with NLevelListView best example for multilevel parent and child listview , But need to make data list according below example of Github
https://github.com/triggs/NLevelExpandableListView/tree/master/src/com/twocentscode/nlevelexpandablelistview
I am trying to implement an ExpandableListView in Android that exhibits accordion type behavior (only one group can be open at a time). It is pretty simple to add code in the setOnGroupExpandListener to collapse one group when another is opened (see below).
getExpandableListView().setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedChapter != -1
&& groupPosition != lastExpandedChapter) {
getExpandableListView().collapseGroup(lastExpandedChapter);
}
lastExpandedChapter = groupPosition;
}
});
However, I've noticed that this seems to confuse the default scrolling logic in the ExpandableListView. When expanding a group, the default scroll will ensure the group's content is viewable (scrolling down as necessary to view it). This is essentially what I want.
When I add code to automatically collapse the previously opened group when a new one is expanded, the scroll gets a little wonky if the group being opened is below the one that is programmatically closed. The list view is scrolled too far up. It works fine if I click to close the groups, but when I do it programmatically in the handler, the scrolling is off.
Any suggestions how to either (A) change the automatic collapse code so it doesn't confuse the default scroll or (B) programmatically adjust the scroll of the list view to the correct location?
Im using nhaarman's ListviewAnimation library https://github.com/nhaarman/ListViewAnimations which works great.
Though I have difficulty tweaking one of his options, which is ExpandableListview. I want to tweak it so that only 1 child view (content view)is visible at a time. So when expanding a parent view (title view) item it should close the previous one. I can't seem to update (notify) my adapter when a child view is visible (is expanded). I have a custom adapter which extends ExpandableListItemAdapter.
This is the class here .
Each item is set with the TitleViewOnClickListener, which handles the expanding ad collapsing of the content view.
Now I would like to collapse all visible child views and keep the selected open. Could anyone here help me or guide me in the right direction?
Next to that I can't seem to get an onlistitem click.
Thank you in advance
I've added a setLimit(int) function to the ExpandableListItemAdapter class. When the (limit+1)th item is expanded, the first expanded item will collapse.
In your case, you could call setLimit(1).
You can use:
#Override
public int getChildrenCount(final int groupPosition) {
return 1;
}
I would like my ExpandableListView to only allow one group to be expanded at a time. I found a solution that does this by overriding the ListViewAdapter's onGroupExpanded to method to collapse the previously expanded group:
#Override
public void onGroupExpanded(int groupPosition)
{
//collapse the old expanded group, if not the same
//as new group to expand
if(groupPosition != mLastExpandedGroup && mLastExpandedGroup != -1)
{
epView.collapseGroup(mLastExpandedGroup);
}
mLastExpandedGroup = groupPosition;
super.onGroupExpanded(groupPosition);
}
By default, when a new group is expanded the ListView is scrolled such that the GroupView for the new group remains visible. This scrolling doesn't work properly for my customized ListView (often the GroupView for the newly expanded group ends up off screen).
Based on the android source, the reason this doesn't work is because the post-expansion scroll position is determined prior the onGroupExpanded method being called, so the position is inaccurate after I force a group to collapse.
So, any suggestions for a custom ListView which allows only one group expanded at a time, and properly scrolls so that the GroupView is visible when a group is exapnded?
try calling the scroll function inside a post() or postDelayed() to make sure the scroll appears later than the default one.
I'm working on an app that uses an expandable listview as its parent, and in each child view there is a horizontal scroll view that is loaded dynamically based on DB information.
All of the buttons in the HSV are compound images added dynamically using an implementation very similar to lazy loading in listview. I was able to add arrows to my HSV, but now I would like to take it one step further and only display arrows when there are enough items to fill the space, such that if only one item is showing then no arrows should be displayed.
Since a list starts in the beginning, I can hide the 'back' arrow by default, but I am unsure where to test whether or not to show the 'forward' arrow.
public Object getGroup(int groupPosition) {
Object o;
o = groups.get(groupPosition).getName();
if(relativeLayout != null && horizontalScrollView != null && forward != null) {
if(horizontalScrollView.getWidth() < relativeLayout.getWidth()) {
forward.setVisibility(View.VISIBLE);
} else {
forward.setVisibility(View.INVISIBLE);
}
}
// }
return o;
}
Originally I used this test in getChildView, where the HorizontalScrollView was populated, but it happened too soon and bot the HSV and relativelayout displayed a width of zero.
Right now I am testing it in getGroup, because it is called multiple times AFTER the childview has started to load, but I think this only works because I have a lot of groups in my expandable list. If I had one group I would still have the same problem.
Is there an ideal place to check whether or not the child layout is larger than the horizontalscrollview?
Update: I set a timer that checks every 1000 ms to see if forward should be turned visible and then exits. Its not pretty, but it works without crashing the app, so I'm happy. Sorry for the bad question!
I am checking the scroll position via the onTouch event:
horizontalScrollView.setOnTouchListener(actualScrollViewTouch);