Android Don';t重新加载片段';反演后的数据
我有一个片段(MyListFragment),它是一个片段,我有一个叫做myList的列表 此列表将在onCreateView()上填充json数据 myList->onItemClick->打开详细信息片段 在我按下后退按钮后,myList列表再次被重新加载,我不希望发生这种情况,相反,我希望第一个片段保留加载数据的初始状态,我对两个list/Details片段都使用了SlidingPaneLayout,但是现在我想用fragmenttransaction来代替,因为我需要用于菜单使用的SlidingPaneLayout,而不是list | details幻灯片 这是我的一点剧本Android Don';t重新加载片段';反演后的数据,android,android-fragments,Android,Android Fragments,我有一个片段(MyListFragment),它是一个片段,我有一个叫做myList的列表 此列表将在onCreateView()上填充json数据 myList->onItemClick->打开详细信息片段 在我按下后退按钮后,myList列表再次被重新加载,我不希望发生这种情况,相反,我希望第一个片段保留加载数据的初始状态,我对两个list/Details片段都使用了SlidingPaneLayout,但是现在我想用fragmenttransaction来代替,因为我需要用于菜单使用的Sli
public class MyListFragment extends Fragment {
private AsyncCallBack callback;
public static RelativeLayout loadingListLayout;
public static RelativeLayout listCarsLayout;
public DetailsFragment detailsFragment;
public ArrayList<String> loadedCars = new ArrayList<String>();
public static String loadedCarsString = "";
public static Boolean jsonCarsLoading = false;
public String latestDate;
public static long selectedItem = -1;
public static Boolean isRefreshing = false;
public PullToRefreshListView myList;
public CarsAdapter carsAdapter;
public ArrayList<CarsItems> carsItems;
public ArrayList<CarsItems> results = new ArrayList<CarsItems>();
public static int ON_LOAD_ITEM_COUNT = 20;
public static int ON_REFRESH_ITEM_COUNT = 10;
public static int ON_LOADMORE_ITEM_COUNT = 3;
int currentFirstVisibleItem, currentVisibleItemCount, currentTotalItemCount, currentScrollState;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.list_fragment, container, false);
loadingListLayout = (RelativeLayout) view.findViewById(R.id.LoadingListLayout);
listCarsLayout = (RelativeLayout) view.findViewById(R.id.ListLayout);
myList = (PullToRefreshListView) view.findViewById(R.id.CarsList);
detailsFragment = new DetailsFragment();
carsAdapter = new CarsAdapter(getActivity(), results);
myList.setAdapter(carsAdapter);
...
// Get Cars List if online
if (isOnline()) {
Api jsonCars = new Api();
jsonCars.setLimit(ON_LOAD_ITEM_COUNT);
jsonCars.setCallBack(callback);
jsonCars.getLatestItemDate = true;
jsonCars.setExcludeCars(loadedCarsString);
jsonCars.execute("latestCars");
}
公共类MyListFragment扩展了片段{
私有异步回调;
公共静态相对加载列表布局;
公共静态相对列表;
公共细节片段细节片段;
public ArrayList loadedCars=new ArrayList();
公共静态字符串loadedCarsString=“”;
公共静态布尔值jsonCarsLoading=false;
公共字符串最晚日期;
公共静态long selectedItem=-1;
公共静态布尔值isRefreshing=false;
公共PullToRefreshListView myList;
公共汽车旅馆;
公共ArrayList网站;
public ArrayList results=new ArrayList();
公共静态int ON_LOAD_ITEM_COUNT=20;
公共静态整型更新项目计数=10;
加载更多项目时的公共静态整数\u计数=3;
int currentFirstVisibleItem、currentVisibleItemCount、currentTotalItemCount、currentScrollState;
私人视野;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
super.onCreateView(充气机、容器、保存状态);
视图=充气机。充气(R.layout.list\u碎片,容器,假);
loadingListLayout=(RelativeLayout)view.findViewById(R.id.loadingListLayout);
listCarsLayout=(RelativeLayout)view.findViewById(R.id.ListLayout);
myList=(PullToRefreshListView)view.findViewById(R.id.CarsList);
detailsFragment=新的detailsFragment();
carsAdapter=新的carsAdapter(getActivity(),results);
myList.setAdapter(carsAdapter);
...
//如果在线,获取车辆列表
if(isOnline()){
Api jsonCars=新Api();
jsonCars.setLimit(装载物品计数时);
setCallBack(回调);
jsonCars.getLatestItemDate=true;
jsonCars.SETEXCLUDECHARS(装载的车辆管柱);
jsonCars.execute(“最新车辆”);
}
我试图在onAttach()中实现代码,但无论我在其中键入什么,我都会发现onLaunchActivity错误……我这样解决了它,将这些方法添加到我的片段中
public int dismiss() {
try {
if (mBackStackId != -1) {
FragmentManager fm = getFragmentManager();
fm.popBackStack(mBackStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
mBackStackId = -1;
FragmentTransaction ft = fm.beginTransaction();
ft.remove(this);
ft.commit();
}
} catch (IllegalArgumentException e) {
return mBackStackId;
}
return mBackStackId;
}
public int show(FragmentManager fm) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(android.R.id.content, this, TAG);
ft.addToBackStack(null);
mBackStackId = ft.commit();
return mBackStackId;
}
我在savedInstanceState()的帮助下解决了这个问题 我曾经在onCreateView中加载dataList,没有任何条件,所以不管怎样,当我放入此代码,并在按下back按钮后检查片段时,片段似乎保存了它的实例状态,如位置和数据,但它一直在加载数据(一次又一次地运行AsyncTask) 所以我必须提出一些条件:
- 首先
我在fragment类的顶部添加了这一行
在第一次运行后更新为“true”public Boolean initialized = false;
- 第二
然后在加载数据之前将它们添加到onCreateView()中
加载列表布局只是加载数据时带有进度条的覆盖if (initialized == false) { loadList(); } else { loadingListLayout.setVisibility(View.GONE); listLayout.setVisibility(1); }
- 第三
然后在loadlist()中,我添加了:
public void loadList() { initialized = true; ... ran the AsyncTask ...
通过这种方式,我确保了数据异步任务不会在每次按下backbutton时执行,这样我就可以继续使用addToBackStack来处理我的fragmentTransaction事务。在片段中实现
onBackPressed
。我不清楚您的代码,您在哪里声明了mBackStackId?如果我想把args发送到第二个fragmnet呢?
public void loadList() {
initialized = true;
...
ran the AsyncTask ...