其他分享
首页 > 其他分享> > Activity和Fragment交互加载地图 闪黑屏解决方案

Activity和Fragment交互加载地图 闪黑屏解决方案

作者:互联网

有两种解决方案:

  1,使用 add 添加到Activity的Fragment中,

    


private FragmentTransaction transaction;
private FragmentManager fragmentManager;
 

 fragmentManager = getSupportFragmentManager();
 transaction = fragmentManager.beginTransaction();

if (myMainFragmentBtn == null) { myMainFragmentBtn = new MainFragmentBtn(); myMainFragmentBtn.setBtnFrag(petInstance.get()); transaction.add(R.id.fragment_main, myMainFragmentBtn);//fragment_main是Activity中的Fragment 的ID
} else {
  transaction.show(myMainFragmentBtn);
}
transaction.commit();

使用  .hide隐藏,这样下次显示的时候 transaction.show(myMainFragmentBtn);就行

 private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (myMainFragmentRun != null) {//-1
            fragmentTransaction.hide(myMainFragmentRun);
        }
        if (myFragmentAlert != null) {//-110
            fragmentTransaction.hide(myFragmentAlert);
        }
        if (myFragmentGangAlert != null) {//-120
            fragmentTransaction.hide(myFragmentGangAlert);
        }
}

 

 

2.使用 FragmentPagerAdapter 去管理Fragment ,这种情况就需要出现闪黑屏;但是我们可以通过动画效果来避免这种情况

   

     // 设置ViewPager的切换动画,3.0以上版本有效
        vpHome.setPageTransformer(false, new ViewPager.PageTransformer() {
            private static final float MIN_SCALE = 0.2f;
            @Override
            public void transformPage(View page, float position){
                int pageWidth = page.getWidth();
                //左侧不可见Pager
                if(position < -1) {
                    page.setAlpha(0);
                    page.setRotationY(0);
                }
                //左侧可见Pager
                else if(position <= 0) {
                    page.setAlpha(1);
                    page.setTranslationX(0);
                    page.setScaleX(1);
                    page.setScaleY(1);
                    page.setRotationY(0);
                }
                //右侧可见Pager
                else if(position <= 1) {
                    page.setAlpha(1 - position);
                    page.setPivotX(page.getMeasuredWidth()/2);
                    page.setPivotY(page.getMeasuredHeight()/2);
                    page.setRotationY(position * 180);
                    page.setTranslationX(pageWidth * -position);
                    float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                }
                //右侧不可见Pager
                else {
                    page.setAlpha(0);
                    page.setRotationY(180);
                }
            }
        });
    }

 

 

标签:transaction,hide,Fragment,private,Activity,myMainFragmentBtn,fragmentTransaction
来源: https://www.cnblogs.com/finn21/p/11730789.html