1

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.

Amol Sawant
  • 13,842
  • 2
  • 20
  • 27

3 Answers3

2

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 .

Ravikant Paudel
  • 2,228
  • 2
  • 17
  • 28
1

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tony
  • 2,242
  • 22
  • 33
1

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);
  • descrease duration to make scrolling faster
Adam
  • 1,018
  • 1
  • 9
  • 20