Reputation: 14322
I seen SimpleExpandableListAdapter
example when i clicked expanded group item at moving top of the screen. i created NewAdapter
which extends
BaseExpandableListAdapter
. I want to do same thing but dont know how to do. i searched lot of things which is not worked for me. Please let me know how to do.
Thank you in Advance.
Upvotes: 1
Views: 5013
Reputation: 2451
What you are looking for is,
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
myListview.setSelectionFromTop(position, 5);
}
This will position your selected list item as the first visible item on screen. However, it does so without any smooth scroll animation, the moment you tap the item, it becomes the first item visible.
If you want the scroll animation, you could use,
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
int offset = position - myListview.getFirstVisiblePosition();
if(myListview.getFirstVisiblePosition() > 0)
offset -= 1;
myListview.smoothScrollByOffset(offset);
}
Note that smoothScrollByOffset is available from API-Level 11 onward.
However, both these methods will not work if you select an item close to the bottom of the list, as the list will not scroll further upwards if the last list item is visible. To overcome this, you could convert your listview into a circular listview, as described here.
Upvotes: 1
Reputation: 2286
This one is working for me
expandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (!parent.isGroupExpanded(groupPosition)) {
parent.expandGroup(groupPosition);
} else {
parent.collapseGroup(groupPosition);
}
parent.setSelectedGroup(groupPosition);
return true;
}
});
As the main working part for scroll is
parent.setSelectedGroup(groupPosition);
may this solve your problem .
Upvotes: 2
Reputation: 1028
I think using duration gives a better user experience. So you can use this, with duration added. Will scroll the item in position smoothly to the top of the listview.
int duration = 500; //miliseconds
int offset = 0; //fromListTop
listview.smoothScrollToPositionFromTop(position,offset,duration);
Upvotes: 1