SupportMapFragment恢复后主要滞后减速,从另一个Fragment onBackPressed返回

时间:2016-08-31 14:52:20

标签: android performance google-maps android-fragments

我有一个SupportMapFragment,在我用另一个片段替换这个片段后,然后按下android上的后退键,地图突然变慢,几乎没有响应或可移动。但当我(停留在慢速地图上)向下拖动系统通知栏,然后再将其拖走时,地图正常运行。

有谁知道为什么会这样?我找不到解决这个问题的方法。 当我向下拖动通知栏时会触发什么?

以下是我管理片段的一些代码片段:

ContentFragment是GoogleMaps的主要片段,OverlayFragment是屏幕大小的一半,显示在地图的顶部(当我点击标记时)。使用打开的OverlayFragment,我点击一个按钮,显示确认用户操作的AlertDialog,然后我通过eventbus将事件发送到管理片段的MainActivity,此事件关闭OverlayFragment并加载新的ContentFragment,返回后我有地图减速

 void setContentFragment(Fragment fragment, boolean addBackStack) {
    String fragmentTag = fragment.getClass().getSimpleName();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.content_main, fragment, fragmentTag);
    if (addBackStack) {
        fragmentTransaction.addToBackStack(fragmentTag);
    }
    fragmentTransaction.commit();
}

  void setOverlayFragment(@Nullable Fragment fragment) {
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.disallowAddToBackStack();
    final Fragment currentFragment = fragmentManager.findFragmentById(R.id.content_overlay);

    if (currentFragment == null && fragment != null) {
        // Add a fragment
        fragmentTransaction.add(R.id.content_overlay, fragment);
    } else if (currentFragment != null && fragment != null) {
        // Replace the fragment
        //ToDo Animation if the heights between layouts are different
        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        fragmentTransaction.replace(R.id.content_overlay, fragment);
    } else if (currentFragment != null && fragment == null) {
        // Remove fragment
        collapse(frameLayoutOverlay, new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                fragmentTransaction.remove(currentFragment);
                fragmentTransaction.commit();
                getSupportFragmentManager().executePendingTransactions();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    } else if (currentFragment == null && fragment == null) {
        return;
    }

    if (currentFragment == null || fragment != null) {
        fragmentTransaction.commit();
        getSupportFragmentManager().executePendingTransactions();
    }

    if (currentFragment == null && fragment != null) {
        expand(frameLayoutOverlay);
    }
}

@Nullable
Fragment getContentFragment() {
    return getSupportFragmentManager().findFragmentById(R.id.content_main);
}

@Nullable
Fragment getOverlayFragment() {
    return getSupportFragmentManager().findFragmentById(R.id.content_overlay);
}



  /**
 * Smoothly expands the height of a given View
 *
 * @param v view as instance of ViewGroup
 */
public static void expand(final View v) {
    if (v instanceof ViewGroup) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.getLayoutParams().height = 1;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }
}

/**
 * Smoothly colapses the height of a given View
 *
 * @param v view as instance of ViewGroup
 */

public static void collapse(final View v, Animation.AnimationListener animationListener) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    a.setAnimationListener(animationListener);
    v.startAnimation(a);
}

0 个答案:

没有答案