【发布时间】:2016-07-20 08:51:57
【问题描述】:
我正在尝试替换 Volley 的 onResponse() 中的片段。它正在被替换,但方式很奇怪。
- 旧碎片仍然可见,而新碎片不可见。
- 旧片段是非交互式的,点击事件由 新的碎片。
就像新的碎片在旧碎片的下面。
如果我尝试替换 Volley 的 onResponse() 之外的片段,那么一切都会按应有的方式进行:旧片段消失,显示新片段。
getNetworkManager().jsonGETRequest(new NetworkManagerRequestData(getActivity(), this,
orderListUrl,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frame, IdleFragment.newInstance());
transaction.commit();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
..
}
}, NetworkManager.getDefaultPolicyForNonTransactions(), false));
片段是使用 FrameLayout 动态添加的。我有一个有 5-6 个碎片的活动。除了这次交易之外,一切都与动画完美配合。
这是 R.id.main_frame,它是一个空的 FrameLayout。
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
..
/>
更新 所以我尝试在 AsyncTask 中实现片段事务
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frame, IdleFragment.newInstance());
transaction.commit();
}
}.execute();
这具有相同的行为。通常,在任何异步回调之外进行事务处理都可以正常工作。
这是我要替换的碎片的布局
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pitch_grey"
android:clickable="true"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
【问题讨论】:
-
不建议在异步调用中发起分片事务。它可能导致 IllegalStateException。
-
我已经进行了必要的检查。
标签: android android-fragments android-volley