简体   繁体   中英

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.

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM