【问题标题】:Update fragment replaced by FragmentTransaction with Asynctask用 Asynctask 更新 FragmentTransaction 替换的片段
【发布时间】:2013-11-18 12:56:50
【问题描述】:

大家好,我在这里是因为自从在 Asynctask 或新线程中运行片段事务以来,我已经尝试了几乎所有方法,似乎没有任何效果,我自己也搞不清楚。

我的尝试:

link

link

link

和其他人

我的问题:

  • 片段做了一些繁重的工作,所以我在里面实现了异步任务
  • 片段,但我无法更新片段
  • 用片段替换时我的 UI 冻结
  • 我以编程方式创建所有视图
  • 我的片段返回一个视图
  • 我假装在片段未准备好时显示进度条

我的主要:

 Bundle bundle = getIntent().getExtras();
        FragmentTransaction fragmentTransaction = getFragmentManager()
                .beginTransaction();
        FragTopics insideTopicsFrag = new FragTopics();
        insideTopicsFrag.setArguments(bundle);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.mainFragment, insideTopicsFrag);
        fragmentTransaction.commit();

我的片段: createTopics 返回一个包含所有内容的视图,换句话说,它是过去在 onCreateView 中返回的视图。

public class FragTopics extends Fragment {
View v;
public FragTopics() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    v = new View(getActivity());
    new TopicsAsyncTask().execute();
    return v;
}
private class TopicsAsyncTask extends AsyncTask<Void, Void, Boolean> {
    ManSession session;
    String courseId;
    Long topicId;
    String courseName;
    MoodleCourseContent[] courseTopics;
    MoodleCourseContent singleTopic;
        private final ProgressDialog dialog = new ProgressDialog(getActivity());
    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("loading...");
        this.dialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... params) {
        ObtainData();
        return true;
    }
    @Override
    protected void onPostExecute(Boolean result) {
        this.dialog.dismiss();
        // The createTopics returns the View with all contents
        v = createTopics(singleTopic, courseName, courseId, topicId);

    }
}

【问题讨论】:

    标签: android android-fragments android-asynctask android-progressbar


    【解决方案1】:

    将您的 onCreateView 更改为:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    v = new View(getActivity());
    new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
               new TopicsAsyncTask().execute();
            }
        }, 200);    
    return v;
    }
    

    希望它仍然有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      相关资源
      最近更新 更多