Amol Sawant
Amol Sawant

Reputation: 14322

Move/scroll clicked listitem view at top of screen in the expandablelistview

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

Answers (3)

Tony
Tony

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

Ravikant Paudel
Ravikant Paudel

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

Adam
Adam

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);
  • descrease duration to make scrolling faster

Upvotes: 1

Related Questions