什么是无尽的适配器在轮换的片段中最佳实践



所以我在片段上使用来自 commonsware 的 endlessadapter,我遇到的问题是每次我在适配器等待完成加载一些数据时旋转时,都会再次调用 cacheInBackground 方法。适配器的初始化是从片段在 onActivityCreated 方法上调用的,那么我怎样才能简单地恢复第一次调用 cacheInBackground 方法的结果呢?

IpdmsEndlessAdapter endAdapt= new IpdmsEndlessAdapter(getActivity(),adapter,R.layout.list_loading_row){

        @Override
        protected boolean cacheInBackground() throws RequestException{
            tempList.clear();
            List<MyProcessDTO> myPRocs;
            myPRocs = new MyprocessesManager(getActivity()).getMyProcessesFromServer(processListPage);
            if(myPRocs!=null){
                tempList.addAll(myPRocs);
            }else
                return false;
            //return true if there's more data do return
            //return false if there was an error or there's no more data
            return myPRocs.size()>=10;

        }
        @Override
        protected boolean onException(View pendingView, Exception e) {
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            return false;
        }
        @Override
        protected void appendCachedData() {
            @SuppressWarnings("unchecked")
            GenericMenuAdapter a=(GenericMenuAdapter)getWrappedAdapter();
            for(MyProcessDTO myProcessDTO:tempList){
                a.add(new IpdmsMobileMenuItemListDTO(myProcessDTO.getNrprocesso(), myProcessDTO.getEtapa(), 0, myProcessDTO));
            }
            processListPage++;
        }
    };
    actualListView.setAdapter(endAdapt);

问候

使其成为一个动态片段(即,通过FragmentTransaction设置它),并在设置过程中的某个地方调用setRetainInstance(true)(例如,onActivityCreated())。这将使适配器实例在配置更改之间保持。

在后台线程执行其工作时,配置更改可能仍然存在一些问题。如果您在应用上述建议后仍遇到问题,请对此答案发表评论。

最新更新