我的想法是在 AsyncTask 中创建新 fragment ,然后在 onPostExecute 我将它与显示的进度条 fragment 交换。我的问题是 fragment 永远不会交换,因此它与进度条 fragment 保持一致。这是不可能的吗?这是一个糟糕的方法吗?我应该在 DetailFragment 中有进度条吗? (会有很多带有进度条的 fragment )

@Override
protected void onStart() {
    super.onStart();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new ProgressBarFragment()).commit();
    TestSwapFragment d = new TestSwapFragment();
    d.execute(data);
}

class TestSwapFragment extends AsyncTask<FragmentData, Void, MyFragment> {

FragmentData data = null;

    @Override
    protected MyFragment doInBackground(FragmentData... params) {

        try {
            Thread.sleep(15000); // Simulate the long running method below
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        this.data = params[0];
        DetailFragment df= new DetailFragment();
        df.ExecuteFragment(this.data); // Long running

        return df;
    }

    @Override
    protected void onPostExecute(MyFragment result) {
        super.onPostExecute(result);

        if (this.data.Position == FragmentPosition.PaneOne)
                getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, result).commit();
        else
                getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame2, result).commit();
    }

}

最佳答案

我没有看到任何理论原因为什么这不应该起作用。但话说回来,这对我来说是一种很奇怪的做法。
AsyncTask.doInBackground() 一般用于创建/下载/处理数据,AyncTask.onPostExecute() 用于更新UI。但是您要在这两种方法上分发 UI 部件。

此外,Fragment 是一个 UI 类,您使用它来处理数据,再次混合每个类的用途。作为一般规则,在我的项目中,我倾向于遵循类似的东西(就像显示一些通用名称的示例,但在真实的东西上,使用有意义的名称,我也可能会忽略一些构造函数,只是为了保持简短)

MyActivity.java
public class MyActivity extends Activity{


      private class MyWorker extends AsyncTask<SomeDataObject, Integer, SomeDataListObject>{
            @Override
            protected MyFragment doInBackground(SomeDataObject... params) {
                 // in here we use SomeDataObject to generate the SomeDataListObject
                 return MyDataCruncher.processData(someDataObject);
                 // see it's a static method, process A and generates B, that's all.
                 // but very important that it's its own class, separate from Activity and Fragment
            }

            @Override
            protected void onPostExecute(SomeDataListObject result){
                if(results != null){
                     // Here we create the fragment, pass the data to it and do the transaction
                     getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, MyFrag.newInstance(result)).commit();

                }else{ /* do some error handling or whatever */ }
            }
}

然后在你的 fragment 上你做:
public class MyFrag extends Fragment{
    private static String KEY_DATA = "_key_data_"
    public static MyFrag newInstance(SomeDataListObject data){
        MyFrag frag = new MyFrag();
        Bundle b = new Bundle();
        frag.setArguments(b);
        // it's important to make your data parcelable and pass it with a Bundle as part of the fragment life-cycle
        b.putParcelableArrayList(KEY_DATA, data);
        return frag;
    }

    // then inside onCreateView
     SomeDataListObject data = getArguments.getParcelableArray(KEY_DATA);
    // ---

}

希望能帮助到你

关于java - 是否可以在后台线程内交换 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14090395/

10-10 18:33
查看更多