什么是旋转最佳实践片段中的无尽适应?

时间:2012-06-18 18:08:25

标签: android android-fragments rotation commonsware-cwac

所以我在片段上使用来自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);

的问候,

1 个答案:

答案 0 :(得分:1)

将此设为动态片段(即通过FragmentTransaction设置)并在设置过程中在某处调用setRetainInstance(true)(例如onActivityCreated())。这将使适配器实例保持在配置更改之间。

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