0
我已經使用Viewpager創建了Tablayout。Tablayout有三個片段'Photo','Video','Docs'。每個片段都有一個gridview。在「照片」片段中,圖像以縮略圖形式加載到網格視圖中,在「視頻」中,片段視頻以縮略圖的形式加載到網格視圖等。當一個圖像加載到Gridview的'Photo'片段中,然後當應用程序被終止並再次打開時,Gridview已經失去了它沒有上傳圖像的狀態。我知道這是因爲onCreateView再次創建整個UI。但是如何在死亡之前拯救碎片的狀態?這裏是一個加載viewpager如何在應用程序被終止後保存Viewpager中片段的狀態
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);
}
}
//照片片段
的代碼..// MainFragment
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];
}
}
你能告訴我該怎麼做嗎? – AmeyaG