java - Pull to refresh returns the first position of the list -
i have 1 problem pull refresh, use com.handmark.pulltorefresh. , need add new elements list when see last element of list. make this:
view = inflater.inflate(r.layout.fragment_generalnews, container, false); mpullrefreshlistview = (pulltorefreshlistview) view .findviewbyid(r.id.pull_refresh_list2); .... // add end-of-list listener mpullrefreshlistview .setonlastitemvisiblelistener(new onlastitemvisiblelistener() { @override public void onlastitemvisible() { if (generalactivity.razr <= 7.0) { pagenumber += 1; log.d("menu", "page2 = " + pagenumber); newpage = "page/" + pagenumber + "/"; final int position = mpullrefreshlistview.getrefreshableview().getfirstvisibleposition()+1; new mytask().execute(); log.d("list", "position of list = " + position); mpullrefreshlistview.onrefreshcomplete(); mpullrefreshlistview.post(new runnable() { @override public void run() { // todo auto-generated method stub mpullrefreshlistview.getrefreshableview().setselection(position); kfulist.setselection(position); } }); log.d("list", "list " + kfunews.tostring()); } } });
in mytask() in onpostexecute refresh adapter:
kfulist = mpullrefreshlistview.getrefreshableview(); kfulist.setadapter(newsadapter); kfulist = mpullrefreshlistview.getrefreshableview(); // need use actual listview when registering // context menu registerforcontextmenu(kfulist); newsadapter.notifydatasetchanged();
when end of list, updated , add new elements, returns first element of list. , can not understand why.
we talking asynctask here. it's asynchronous, when mpullrefreshlistview.onrefreshcomplete()
appends afer execution of task, task in fact not yet finished.
you should add in onpostexecute
want affecting ui (onpostexecute
executed in ui thread)
try :
public void onpostexecute(**whatever parameter**)
{ kfulist = mpullrefreshlistview.getrefreshableview(); kfulist.setadapter(newsadapter); // useless // kfulist = mpullrefreshlistview.getrefreshableview();
// need use actual listview when registering // context menu registerforcontextmenu(kfulist); newsadapter.notifydatasetchanged(); log.d("list", "position of list = " + position); mpullrefreshlistview.onrefreshcomplete(); mpullrefreshlistview.post(new runnable() { @override public void run() { mpullrefreshlistview.getrefreshableview().setselection(position); kfulist.setselection(position); } }); log.d("list", "list " + kfunews.tostring()); // [...] }
Comments
Post a Comment