我的想法是在 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();
}
}