4

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

@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();
    }

}
4

1 回答 1

2

我没有看到任何理论上的理由为什么这不应该起作用。但是说,这对我来说是一种非常奇怪的方法。

通常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 */ }
            }
}

然后在你的片段上你做:

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);
    // ---

}

希望能帮助到你

于 2012-12-30T12:14:47.793 回答