与AsyncTask的更新后的片段重叠查看片段、AsyncTask

由网友(几人尽弃i)分享简介:我pretend是什么来显示一个不确定的进度条(暂时仅用于测试目的的对话),而我的第二个片段获取数据,构建视图不结冰的用户界面。 What i pretend is to display an indeterminate progress bar (for the moment is only an dialog f...

我pretend是什么来显示一个不确定的进度条(暂时仅用于测试目的的对话),而我的第二个片段获取数据,构建视图不结冰的用户界面。

What i pretend is to display an indeterminate progress bar (for the moment is only an dialog for test purposes) while my second fragment is getting data and constructing the view without freezing the UI.

我已经试过:

的AsyncTask 装载机装载机AsyncTaskLoader 适配器(没试过,但也许它的一个选项,但我不知道如何使用适配器与自定义布局)

随着AsyncTask的做法几乎一切都是对的,但onPostExecute我已经更新到做到这一点我创建了一个接口的片段,并在主要活动我删除视图,并添加新的,但有了这个,我创建了另一个问题我的背部栈搞砸所以我'没办法了。

With the asynctask approach almost everything is right, but onPostExecute I've to update the fragment to do this i created an interface and in the main activity i remove the view and add the new one, but with this i created another problem my back stack is messed up so i'am out of options.

一个图来帮助理解它是如何工作:

A diagram to help understand how it works:

我的片段2:

public class FragTopics extends Fragment {

Object course;
ManSession session;
String courseId;
Long topicId;
String courseName;
private LinearLayout mainLayout;
private ScrollView contentScrollable;
private LinearLayout contentsLayout;
private View myView;

public FragTopics() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    session = new ManSession(getActivity().getApplicationContext());

    courseId = getArguments().getString("courseId");
    topicId = Long.parseLong(getArguments().getString("topicId"));
    courseName = getArguments().getString("courseName");

    new HeavyWork().execute();

    // Create empty view because i need to return something
    myView = new View(getActivity());
    return myView;
}

private class HeavyWork extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;
    final FragmentUpdater activity = (FragmentUpdater) getActivity();

    // Do the long-running work in here
    @Override
    protected Void doInBackground(Void... params) {

        MoodleCourseContent[] courseTopics = new ManContents(getActivity()
                .getApplicationContext()).getContent(courseId);

        MoodleCourseContent singleTopic = new ManContents(getActivity()
                .getApplicationContext()).getTopic(topicId, courseTopics);

        // This createTopics call another methods from the fragment class to
        // get the data and create views
        myView = createTopics(singleTopic, courseName, courseId, topicId);
        return null;

    }

    protected void onPreExecute() {
        dialog = new ProgressDialog(getActivity());
        dialog.show();
    }

    // This is called when doInBackground() is finished
    @Override
    protected void onPostExecute(Void ignore) {
        activity.updater(myView);
        dialog.dismiss();
    }

}

我的界面:

    public interface FragmentUpdater {

    public void updater(View param);

}

我的主要活动,其中的接口被实现:

My main activity where the interface is implemented:

@Override
    public void updater(View param) {
         ViewGroup vg = (ViewGroup) findViewById (R.id.main_content);
         vg.removeAllViews();
         vg.addView(param);
    }

我的布局创建程序,这是结构:

My layouts are created programmatic and this is structure:

->LinearLayout
  ->TextView
  ->ScrollView
    ->LinearLayout 

我的主要问题是如何,而片段2不冻结UI,如果我的上述做法是正确的,heavyWork在换句话说后完成后如何更新片段视图初始化显示进度条AsyncTask的完成做工作背景

删除毕竟视图,并添加新的onBack pressed将重叠我用previous片段的观点看法

After remove all view and add the new one onBackpressed will overlap my view with the previous fragments views

推荐答案

您片段是无头。所以,不实现onCreateView方法。此举code到的onCreate。然后在异步任务,使用onPostExecute信号与更新的信息为它显示父活动。然后该片段的父活动可以决定用户界面应如何将此信息作出反应。确保你避免与任何地方比其他onPostExecute父活动通信(或onPublishProgress,如果您​​要更新)的方法,因为一)活动可以为空值有许多原因,包括设备的旋转和b)这些方法gauranteed到UI线程上运行

Your fragment is headless. So don't implement the onCreateView method. Move that code to onCreate. Then in the async task, use onPostExecute to signal the parent activity with updated information for it to display. The parent activity of this fragment can then decide how the UI should react to this information. Make sure you avoid communicating with the parent activity anywhere other than the onPostExecute (or onPublishProgress, if you're updating) method, since a) the activity can be null for a number of reasons, including a device rotation and b) these methods are gauranteed to run on the UI thread

阅读全文

相关推荐

最新文章