gpt4 book ai didi

android - 在从 AsyncTaskLoader 完成 onLoadFinished 后动态更新 Fragment 的 UI - 无法获取 ViewGroup

转载 作者:太空宇宙 更新时间:2023-11-03 13:29:12 25 4
gpt4 key购买 nike

以前,我有一个 Fragment,它根据耗时的获取数据更新其 UI。

public class HomeMenuFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Infos arg1 = performTimeConsumingDataStructureLoading();

// Add in various UI views in v, according to information in loaded
// data structure.
View v = inflater.inflate(R.layout.home_menu, container, false);
// ...
// Adding several LinearLayout and TextView into v, based on
// information from loaded data structure (arg1)
// ...
return v;
}
....
}

这真的不是一个好的设计。有时,performTimeConsumingDataStructureLoading 可能需要超过 5 秒,我会收到 ANR(应用程序无响应)

引用http://developer.android.com/reference/android/content/AsyncTaskLoader.html ,我最终得到了更好的解决方案。我遇到的示例是,它通过 ArrayAdapter 间接更新其 UI。

public class HomeMenuFragment extends SherlockFragment implements LoaderManager.LoaderCallbacks<HomeMenuFragment.Infos> {
@Override
public void onLoadFinished(Loader<Infos> arg0, Infos arg1) {
LayoutInflater inflater = this.getLayoutInflater(null);
ViewGroup container = (ViewGroup)this.getView().getParent();
// Remove previous old views, as onLoadFinished might be triggered
// by onContentChanged too.
container.removeAllViews();

View v = inflater.inflate(R.layout.home_menu, container, false);
// ...
// Adding several LinearLayout and TextView into v, based on
// information from loaded data structure (arg1)
// ...
container.addView(v);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
....
}

我没有 ArrayAdapter,因为我没有使用 ListFragment。我想要做的是将几个 TextView 添加到几个 LinearLayout 中。然后,我会将 LinearLayout 添加到 Fragment 的容器中。

但是,下面的代码给我空指针异常。

ViewGroup container = (ViewGroup)this.getView().getParent();

据我所知,在 AsyncTaskLoaderonLoadFinished 之后动态更新 Fragment UI 的正确方法是什么?

最佳答案

However, most example I encounter is, they update their UI indirectly through an ArrayAdapter.

使用 Loader 并不要求您必须使用 ListView,您可以更新应用程序中的任何目标 View 。

What I know what is the correct way to update Fragment UI dynamically, after onLoadFinished from AsyncTaskLoader?

Fragment 的 View 中获取对容器的引用,您将在其中放置额外的 View 并添加新 View 。

@Override
public void onLoadFinished(Loader<Infos> arg0, Infos arg1) {
// for some reason the fragment's view isn't built yet so abort things
if (getView() == null) {
ViewGroup container = (ViewGroup) getView().findViewById(R.id.containerId);
ViewGroup container = getViewcontainer.removeAllViews();
// ...
// Adding several LinearLayout and TextView into v, based on
// information from loaded data structure (arg1)
// ...
container.addView(v);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_menu, container, false);;
}

关于android - 在从 AsyncTaskLoader 完成 onLoadFinished 后动态更新 Fragment 的 UI - 无法获取 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15517228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com