simulate the view still being present by providing an image of view

        sometime,we can not  avoid a view disappearing,for example: nested fragment will be disappeared when the parent fragment is removed/replaced,

       in order to avoid the user seeing the nested fragment disappearing,you could simulate those fragments still being present by providing an image of them,

       as they appeared on the screen. this image will be used as a background for the nested fragments containerso even if the views of the nested fragment go away the image will simulate their presence.

the following code was a little example with setting up the background image

 
 
public static class FragmentA extends Fragment {
 
private Bitmap b = null ;
 
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
getChildFragmentManager (). beginTransaction ()
. add ( android . R . id . list , new FragmentA1 ())
. add ( android . R . id . list , new FragmentA2 ()). commit ();
}
 
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState ) {
LinearLayout layout = new LinearLayout ( container . getContext ());
layout . setBackgroundColor ( Color . RED );
layout . setOrientation ( LinearLayout . VERTICAL );
layout . setId ( android . R . id . list );
return layout ;
}
@Override
public void onPause () {
b = loadBitmapFromView ( getView ());
super . onPause ();
}
 
public static Bitmap loadBitmapFromView ( View v ) {
Bitmap b = Bitmap . createBitmap ( v . getWidth (),
v . getHeight (), Bitmap . Config . ARGB_8888 );
Canvas c = new Canvas ( b );
v . layout ( 0 , 0 , v . getWidth (), v . getHeight ());
v . draw ( c );
return b ;
}
 
@Override
public void onDestroyView () {
BitmapDrawable bd = new BitmapDrawable ( b );
getView (). findViewById ( android . R . id . list ). setBackground ( bd );
b = null ;
super . onDestroyView ();
}
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值