片段事务(替换)冻结 UI



我有一个片段,在其布局中,我有一个用于两个视图的区域。 一个用于列表视图。另一个用于地图视图。

列表视图

只是一个列表视图。地图视图是我在单击按钮后提交的片段。因此,当我第一次按下该按钮时,它大约需要 2-3 秒才能替换专用视图中的片段。在这段时间里,所有的 UI 都卡住了。之后,列表到映射之间的过渡是平滑的,反之亦然。

如何克服这种情况?

布局

的布局:

<RelativeLayout  
        ....
        <RelativeLayout
            android:id="@+id/mapViewLayout"
            android:layout_below="@id/restaurantListFragTxtNoResults"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible">
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/listViewLayout"
            android:layout_below="@id/restaurantListFragTxtNoResults"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@color/app_theme_color"
                android:dividerHeight="4px">
            </ListView>
             ...
        </RelativeLayout>   
     ...
</RelativeLayout>   

单击按钮以切换到地图视图:

  mFragmentManager.beginTransaction()
                            .replace( R.id.mapViewLayout,
                                    new MapFragment() ).commit();
layoutView.findViewById(R.id.restViewLayout).setVisibility(View.GONE);

尝试使用处理程序包装片段事务。

new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
             mFragmentManager.beginTransaction()
                         .replace(R.id.mapViewLayout,
                                 new MapFragment()).commit();
           }
       }, 50);

最新更新