How to maintain ListView position when using the new Loader APIs?

前端 未结 2 736
情深已故
情深已故 2021-02-14 08:13

In Honeycomb the Loader APIs were introduced as the proper way to provide data to an application by doing the heavy lifting on a background thread. In my application I\'m worki

2条回答
  •  失恋的感觉
    2021-02-14 09:03

    You must just do not create a new adapter in each Loaders loading; I mean..

    @Override
        public void onLoadFinished(Loader> loader, List notifications) {
            // We must do it in this way to not losing listview scroll position after a reload.
            if(mAdapter==null) {
                mAdapter = new HomeUserActivityListAdapter(getActivity(), notifications);
                mListView.setAdapter(mAdapter);
    
            }else{
                mAdapter.refill(notifications);
            }
        }
    

    And in you adapter create a method to items refill

    public void refill(List notifications) {
        mNotificationsList.clear();
        mNotificationsList.addAll(notifications);
        notifyDataSetChanged();
    }
    

    And that's all, it will maintain your scroll position exactly :-)

提交回复
热议问题