在Volley的OnResponse中没有替换片段()

时间:2016-07-20 08:51:58

标签: android android-fragments android-volley

我正在尝试替换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个frags的活动。除了这笔交易外,一切都与动画完美配合。

这是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();

这有相同的行为。通常在任何异步回调之外进行交易工作正常。

这是我想要替换

的frag的布局
<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>

2 个答案:

答案 0 :(得分:2)

问题是滑动刷新布局。当您尝试在刷新时替换片段时,之前的片段将冻结。这是SwipeRefreshLayout本身的一个问题。

您可以将swipeRefreshLayout包装在FrameLayout中。这可能有用。

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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>
</FrameLayout>

参考:When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work

答案 1 :(得分:0)

您无法替换静态放置在xml布局文件中的片段。静态片段是否静态放置?您应该在布局中创建一个容器(例如FrameLayout),然后使用FragmentTransaction以编程方式添加该片段。

还使用空的FrameLayout作为片段容器。避免使用LinearLayout或RelativeLayout。一旦使用LinearLayout并将LinearLayout替换为FrameLayout,我就遇到了这个问题,并且工作正常。

另一个建议是,避免在异步调用中进行片段事务。它可能会导致IllegalState异常。如果您仍想这样做,请使用commitAllowStateLoss而不是commit。