我用Viewpager创建了一个tablayout.Tablayout有三个3片段'Photo','Video','Docs'。每个片段都有一个gridview。在“照片”片段中,图像作为缩略图加载到gridview,“视频”片段视频以缩略图的形式加载到gridview,依此类推。当图像加载到“照片”片段中的Gridview时,之后当应用程序被杀死并再次打开时,Gridview已经失去了它没有上传图像的状态。我知道这种情况正在发生,因为onCreateView正在再次创建整个UI。但是如何在它被杀之前保存碎片的状态?这是代码..
//加载viewpager的MainFragment
public class MainFragment extends Fragment {
static FragmentManager fManager;
static ViewPager viewPager;
View view;
static ViewPagerAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(1);
fManager = getChildFragmentManager();
adapter = new ViewPagerAdapter(fManager);
viewPager.setAdapter(adapter);
return view;
}
public static MainFragment newInstance() {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
List<Fragment> listOfFragments = getChildFragmentManager().getFragments();
if (listOfFragments.size() >= 1) {
for (Fragment fragment : listOfFragments) {
if (fragment instanceof PhotoFragment) {
fragment.onActivityResult(requestCode, resultCode, data);
}
else if (fragment instanceof VideoFragment) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
// Photo Fragment
public class PhotoFragment extends Fragment {
private GridView gridView;
private View view;
private Dialog dialog = null;
private ImageView btnCamera;
TextView btnCameradialog, btnGalleryDialog;
private final int CLICK_PHOTO = 1;
private final int SELECT_PHOTO = 2;
private Uri imageUri;
private String path, picturePath, pathlist;
private List<PictureBeans> pictureDisplayList = null;
private ImageAdapter adapter;
private TextView textViewtitle;
private boolean isVisibleToUser = false;
public PhotoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(isVisibleToUser == false) {
view = inflater.inflate(R.layout.fragment_photo, container, false);
initUI();
setListener();
}
return view;
}
private void initUI() {
btnCamera = (ImageView) view.findViewById(R.id.fabPicture);
gridView = (GridView) view.findViewById(R.id.grid_view_photoFragment);
pictureDisplayList = new ArrayList<>();
}
private void setListener() {
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showappwindow();
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), FullImageActivity.class);
i.putExtra("id", position);
i.putExtra("path", pictureDisplayList.get(position).getPicturePath().toString());
startActivity(i);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == Activity.RESULT_OK) {
try {
imageUri = imageReturnedIntent.getData();
path = Util.getRealPathFromURI(getActivity(), imageUri);
Log.v("Uri", "" + imageUri);
picturePath = path.toString();
Log.e("Picture Path in Frag ", "" + picturePath);
PictureBeans bean = new PictureBeans();
bean.setPicturePath(picturePath);
pictureDisplayList.add(bean);
Log.e("List size", "" + pictureDisplayList.size());
addToAdapter(pictureDisplayList);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case CLICK_PHOTO:
if (resultCode == Activity.RESULT_OK) {
try {
imageUri = imageReturnedIntent.getData();
path = Util.getRealPathFromURI(getActivity(), imageUri);
Log.e("PATH",""+path);
picturePath = path.toString();
PictureBeans bean = new PictureBeans();
bean.setPicturePath(picturePath);
pictureDisplayList.add(bean);
addToAdapter(pictureDisplayList);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
private void addToAdapter(List<PictureBeans> pictureDisplayList) {
adapter = new ImageAdapter(getActivity(), pictureDisplayList);
gridView.setAdapter(adapter);
}
// Pop Up window Code
public void showappwindow() {
dialog = new Dialog(getActivity(), R.style.mydialogstyle);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_photo);
textViewtitle = (TextView) dialog.findViewById(R.id.tvTitle);
textViewtitle.setText("Upload Image");
btnCameradialog = (TextView) dialog.findViewById(R.id.btnCameradialog);
btnGalleryDialog = (TextView) dialog.findViewById(R.id.btnGalleryDialog);
dialog.show();
btnCameradialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getParentFragment().startActivityForResult(cameraIntent, CLICK_PHOTO);
dialog.dismiss();
}
});
btnGalleryDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
getParentFragment().startActivityForResult(photoPickerIntent, SELECT_PHOTO);
dialog.dismiss();
}
});
}
}
//适配器
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 3;
private PhotoFragment photoFragment;
private VideoFragment videoFragment;
private ThreeFragment threeFragment;
private String[] titles = new String[]{"Photo", "Video", "Documents"};
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putInt("request.code", position + 1);
switch (position) {
case 0:
photoFragment = new PhotoFragment();
return photoFragment;
case 1:
videoFragment = new VideoFragment();
return videoFragment;
case 2:
threeFragment = new ThreeFragment();
return threeFragment;
}
return null;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
答案 0 :(得分:0)
你可以在onDestroyView()方法中保存片段状态,与Fragment一起使用 参数