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

来源:互联网 发布:为什么脸会变宽 知乎 编辑:程序博客网 时间:2024/06/14 00:16

        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();
}
 
}

0 0
原创粉丝点击