android - ListView scroll position not maintained after loading content using CWAC Endless Adpater -
update
basically, bbrakenhoff has answered question there 1 more thing left fix. how can update contents of endlessfeedadapter (mendlsfidadptr)? need clear item , reload. i'm using cwac endlessadapater. there trick clear contents or easier program method? after done scroll position should maintaind.
i getting data server , updating endlessfeedadapter when content changes. each time updating adapter , reloading content. problem after reloading list jumps right top scroll position not maintained. have tried setselection , setselectionfromtop extensively, without positive results.
how maintain scroll position after adapter has been updated?
i have been going through forums searching answer nothing seems working.
i have tried these: maintain/save/restore scroll position when returning listview
this didn't work:
int index = mlist.getfirstvisibleposition(); view v = mlist.getchildat(0); int top = (v == null) ? 0 : v.gettop(); // restore index , position mlist.setselectionfromtop(index, top);
nor this:
// save listview state parcelable state = listview.onsaveinstancestate(); // set new items listview.setadapter(adapter); // restore previous state (including selected item index , scroll position) listview.onrestoreinstancestate(state);
nor other solutions such setting runnable or setting scrollpositiony. setting notifydatachanged didn't work loading different lists.
my code:
private void showfeed() { if (mfeedactivity.minfeed) { mquickreturnview.setvisibility(view.visible); } else { mquickreturnview.setvisibility(view.gone); } activity actvt= getactivity(); if (actvt == null || mfeedlistview == null) return; actvt.invalidateoptionsmenu(); mfeedlistview.setvisibility(view.visible); //updated response if (madapter == null){ madapter = new feedadapter(actvt, 0, mfeed.getitems().getfeeditemlist(), this); } else { madapter.clear(); madapter.addall(mfeed.getitems().getfeeditemlist()); madapter.notifydatasetchanged(); } mendlsfidadptr = new endlessfeedadapter(actvt, madapter, r.layout.progress_row, mfeed.isshowmorebar(), mendlsfidadptr.setruninbackground(false); //parcelable state = mfeedlistview.onsaveinstancestate(); mfeedlistview.setadapter(mendlsfidadptr); //mfeedlistview.onrestoreinstancestate(state); mfeedlistview.setselectionfromtop(mfirstvisibleitem, mvisibleitemoffset); if(!(mfeedscope.equalsignorecase(feedscope.bookmarks.xmlvalue()) || mfeedscope.equalsignorecase(feedscope.document.xmlvalue()) || mfeedscope.equalsignorecase(feedscope.notifications.xmlvalue()) || mfeedscope.equalsignorecase(feedscope.received_tasks.xmlvalue()) || mfeedscope.equalsignorecase(feedscope.send_tasks.xmlvalue()))) { mfeedlistview.getviewtreeobserver().addongloballayoutlistener(mgloballayoutlistener); mfeedlistview.setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscrollstatechanged(abslistview view, int scrollstate) { mcanshowhide = scrollstate == scroll_state_fling; } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { view v = mfeedlistview.getchildat(0); //view v = null; if(!mfeedactivity.minfeed || v == null) return; int top = v.gettop(); if(misanimating) { mvisibleitemoffset = top; mfirstvisibleitem = firstvisibleitem; return; } boolean hide = false; boolean show = false; float stickyheight = getresources().getdimension(r.dimen.sticky_height); if(firstvisibleitem == mfirstvisibleitem) { if((top + stickyheight) < mvisibleitemoffset) { // content scrolled down // if shown hide quickactionview if(mquickreturnshown) { hide = true; } } else if (top > mvisibleitemoffset) { // content scrolled // if hidden show quickactionview if(!mquickreturnshown) { show = true; } } } else if(firstvisibleitem > mfirstvisibleitem) { // content scrolled down // if shown hide quickactionview if(mquickreturnshown) { hide = true; } } else if (firstvisibleitem < mfirstvisibleitem) { // content scrolled // if hidden show quickactionview if(!mquickreturnshown) { show = true; } } if((show && mcanshowhide) || (top == 0 && !mquickreturnshown)) { mtranslateanimation = new translateanimation(0, 0, -mquickreturnheight, 0); mtranslateanimation.setduration(duration_millis); mtranslateanimation.setanimationlistener(new animation.animationlistener() { @override public void onanimationstart(animation animation) { misanimating = true; } @override public void onanimationend(animation animation) { misanimating = false; mquickreturnshown = true; mquickreturnview.setvisibility(view.visible); } @override public void onanimationrepeat(animation animation) { } }); mquickreturnview.startanimation(mtranslateanimation); } if(hide) { mtranslateanimation = new translateanimation(0, 0, 0, -mquickreturnheight); mtranslateanimation.setduration(duration_millis); mtranslateanimation.setanimationlistener(new animation.animationlistener() { @override public void onanimationstart(animation animation) { misanimating = true; } @override public void onanimationend(animation animation) { misanimating = false; mquickreturnshown = false; mquickreturnview.setvisibility(view.gone); } @override public void onanimationrepeat(animation animation) { } }); mquickreturnview.startanimation(mtranslateanimation); } mvisibleitemoffset = top; mfirstvisibleitem = firstvisibleitem; } }); } else { mfeedlistview.setonscrolllistener(null); } mfeedlistview.setselectionfromtop(mfirstvisibleitem, mvisibleitemoffset); //mfeedlistview.scrollto(mcurrentx,mcurrenty); if(mfeed.getitems().getfeeditemlist().size() == 0) { memptyfeedview.setvisibility(view.visible); } }
any appreciated!
are calling method showfeed() everytime received new data? if yes, maybe try refill adapter instead of assigning new 1 every time.
i don't know doing in adapter, i'll show how did in 1 of apps.
in activity/fragment when want update list new items:
private void refreshcalendar(arraylist<calendarday> newcalendar) { if (madapter == null) { madapter = new calendaradapter(getactivity(), newcalendar); mexpandablelistview.setadapter(madapter); } else { madapter.refill(newcalendar); } restoreinstancestate(); }
and in adapter:
public void refill(arraylist<calendarday> newcalendar) { mcalendar.clear(); mcalendar.addall(newcalendar); notifydatasetchanged(); }
Comments
Post a Comment