简体   繁体   English

如何在Android中完成AsyncTask后更新片段?

[英]How to update a Fragment after an AsyncTask finishes in Android?

There is probably a very simple way to do this that will only require one or two lines of code, but I'm not sure how to go about this. 可能有一种非常简单的方法,只需要一行或两行代码,但我不知道如何解决这个问题。

I want to have fragments that initially display a loading gif in the center of the screen, then it will start an AsyncTask which downloads information from my server. 我希望最初在屏幕中央显示加载gif的片段,然后它将启动AsyncTask,从我的服务器下载信息。 After the AsyncTask is complete, I will need to set the content view of the Fragment to a different xml file, then display some of what I got from the server. AsyncTask完成后,我需要将Fragment的内容视图设置为不同的xml文件,然后显示我从服务器获得的一些内容。

I was thinking there should be some type of function like Fragment.invalidate() or Fragment.refresh() that could help update the Fragment, but I can't seem to find any such function. 我认为应该有一些类型的功能,如Fragment.invalidate()Fragment.refresh() ,可以帮助更新片段,但我似乎无法找到任何这样的功能。

Also, if I update the Fragment in a different function outside of the onCreateView function, do I need to create variables to hold the LayoutInflater and ViewGroup objects that are initially passed to it, or can I access these using function calls? 另外,如果我在onCreateView函数之外的其他函数中更新Fragment,是否需要创建变量来保存最初传递给它的LayoutInflaterViewGroup对象,还是可以使用函数调用来访问它们? Below is roughly what I want to do... 以下是我想做的事情......

public class HomeFragment extends Fragment
{
    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.loading, container, false);

        (new GetDataTask()).execute(1);

        return view;
    }


    private class GetDataTask extends AsyncTask<Integer, Void, ServerResponse>
    {
        @Override
        protected void onPreExecute(){}

        @Override
        protected ServerResponse doInBackground(Integer... args)
        {
            int page_num = args[0];
            return Server.getPosts(page_num);
        }

        @Override
        protected void onPostExecute(ServerResponse result)
        {
            /*
                1. Update the content view to R.layout.multi_post_list
                2. Change specific text views and image views (I know how to do this part)
                3. Notify that changes have been made to the Fragment, so it needs redrawn
            */
        }
    }
}

Note: ServerResponse is a class that I created, and Server.getPosts is a static function that returns a ServerResponse. 注意: ServerResponse是我创建的类,Server.getPosts是一个返回ServerResponse的静态函数。 I know that these work in my case. 我知道这些都适用于我的情况。 I need help with points 1 and 3 in my onPostExecute part of my GetDataTask class. 我需要帮助我的GetDataTask类的onPostExecute部分中的第1点和第3点。

Thanks! 谢谢!

You can use findFragmentByTag 您可以使用findFragmentByTag

DetailsFragment df = getFragmentManager().findFragmentByTag("details");
    if (df != null) {
        df.setShownIndex(getSelectedIndex());
    } else {
        df = DetailsFragment.newInstance(getSelectedIndex());
    }
    fragmentTransaction.replace(R.id.frame, df, "details").commit();

Three things: 三件事:

  1. Use AsyncTaskLoader instead of AsyncTask to prevent reloading your data after a screen rotation. 使用AsyncTaskLoader而不是AsyncTask来防止在屏幕旋转后重新加载数据。 Or call setRetainInstance(true) on the Fragment to prevent the Fragment from being destroyed on rotation. 或者在Fragment上调用setRetainInstance(true)以防止片段在旋转时被破坏。 (just be careful to handle detached state) (只是要小心处理分离状态)
  2. You can get the Activity's root view by calling getActivity().findViewById(android.R.id.content) and manipulate it however you need to. 您可以通过调用getActivity().findViewById(android.R.id.content)来获取Activity的根视图,并根据需要对其进行操作。 You could also just create one layout that has both states and show/hide parts of them has needed. 您还可以创建一个具有两种状态并显示/隐藏部分状态的布局。
  3. You can get the inflater at any time by calling LayoutInflater.from(getActivity()) 您可以通过调用LayoutInflater.from(getActivity())随时获取LayoutInflater.from(getActivity())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM