我使用片段来实现viewpager,viewpager也是一个片段,我正在实现一个后退按钮并点击它,片段返回到前一个片段,按下后退按钮,前一个片段再次加载并获取再次回应。我怎么能防止这种情况。我在下面添加了我的代码。
照片碎片: -
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.layout_photo, container, false);
ButterKnife.inject(this, view);
sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
editor = sharedPref.edit();
if (sharedPref.contains("isViewPagerStarted")) {
isViewPagerStarted = sharedPref.getBoolean("isViewPagerStarted",false);
}
Log.e("pagerstart", "" + isViewPagerStarted);
if (isViewPagerStarted) {
backButton.setVisibility(View.VISIBLE);
Animation RightSwipe = AnimationUtils.loadAnimation(getActivity(),
R.anim.slide_out_left);
backButton.startAnimation(RightSwipe);
backButton.setVisibility(View.INVISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
editor.clear();
editor.commit();
// sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
//
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.plc_gallery).cacheOnDisk(true)
.build();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
mContext = getActivity();
FontHelper.setFontFace(galleryHeader, FontType.FONT5);
getGalleryInfo();
onClickGridView();
}
private void onClickGridView() {
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// String backStateName = fragment.getClass().getName();
callViewPagerStarted();
ViewPagerFragment viewPagerFragment = new ViewPagerFragment();
Bundle bun = new Bundle();
bun.putInt("position", position);
bun.putStringArray("fullImage", imageUrls);
// bun.putStringArray("desc", description);
viewPagerFragment.setArguments(bun);
transaction = getActivity().getSupportFragmentManager()
.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.frameLayoutFragment,
viewPagerFragment, "viewFrag");
transaction.commit();
/*
* // Send intent to SingleViewActivity Intent intent = new
* Intent(getActivity(),ViewPagerActivity.class); // Pass image
* index Bundle bundle=new Bundle();
* bundle.putInt("position",position);
* bundle.putStringArray("imageArray",imageUrls);
* bundle.putStringArray("description",description);
* intent.putExtras(bundle); startActivity(intent);
*/
}
});
}
public void callViewPagerStarted() {
editor.putBoolean("isViewPagerStarted", true);
editor.commit();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
/* getGalleryInfo(); */
}
private void getGalleryInfo() {
GetMesaGallery getTempleInfo = new GetMesaGallery();
// getTempleInfo.setAppID("8eb24199-6838-43ac-b9ca-40031ab37e65");
ProgresBar.start(mContext);
ServiceManager.instance().enqueue(getTempleInfo, new ServiceListener() {
@Override
public void onComplete(Response arg0) {
ProgresBar.stop();
GetMesaGallerysResponse response = (GetMesaGallerysResponse) arg0;
if (response.isValid()) {
response.getImageDto();
final ArrayList<ImageDto> galleryImageList = response
.getImageDto();
thumbnailUrls = new String[galleryImageList.size()];
// description = new String[galleryImageList.size()];
imageUrls = new String[galleryImageList.size()];
for (int i = 0; i < galleryImageList.size(); i++) {
try {
thumbnailUrls[i] = galleryImageList.get(i)
.getthumbnailURL();
/*
* description[i] = galleryImageList.get(i)
* .getdescription();
*/
imageUrls[i] = galleryImageList.get(i).getimageURL();
loadGridView();
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
}
}
});
}
private void loadGridView() {
try {
GridViewAdapter adapter = new GridViewAdapter(getActivity(),
thumbnailUrls);
// Set the adapter to the gridview
gridView.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Add variable to outState here
super.onSaveInstanceState(outState);
}
ViewPager片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_viewpager, container,
false);
ButterKnife.inject(this, view);
backButton.setVisibility(View.VISIBLE);
Animation RightSwipe = AnimationUtils.loadAnimation(getActivity(),
R.anim.slide_in_right);
backButton.startAnimation(RightSwipe);
Log.e("insdie", "viewpager");
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Bundle arguments = getArguments();
position = arguments.getInt("position");
Log.e("position", "" + position);
fullImageUris = arguments.getStringArray("fullImage");
txtDesc = arguments.getStringArray("desc");
FontHelper.setFontFace(description, FontType.FONT6);
description.setPaintFlags(description.getPaintFlags()
| Paint.UNDERLINE_TEXT_FLAG);
/*
* List<Fragment> imageFragments = getFragments();
*/
pageAdapter = new MyPagerAdapter(getActivity()
.getSupportFragmentManager(), fullImageUris);
mPager.setAdapter(pageAdapter);
mIndicator.setViewPager_Text(mPager,description,txtDesc);
mPager.setCurrentItem(position);
}
/*
* @Override public void onResume() { // TODO Auto-generated method stub
* super.onResume(); pageAdapter = new MyPagerAdapter(getActivity()
* .getSupportFragmentManager(),fullImageUri);
* mPager.setCurrentItem(position); mPager.setAdapter(pageAdapter);
* mIndicator.setViewPager_Text(mPager, description, txtDesc); }
*/
@OnClick(R.id.left_arrow)
public void tapLeftArrow() {
mPager.setCurrentItem(getItem(-1), true);
}
private int getItem(int i) {
return mPager.getCurrentItem() + i;
}
@OnClick(R.id.right_arrow)
public void tapRightArrow() {
mPager.setCurrentItem(getItem(+1), true);
}
@OnClick(R.id.imgBackBtn)
public void moveToPreviousFragment() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
Log.i("MainActivity", "popping backstack");
fm.popBackStack();
} else {
Log.i("MainActivity", "nothing on backstack, calling super");
}
// getActivity().onBackPressed();
}
答案 0 :(得分:1)
我只是检查了我的片段的实例并解决了问题
public static PhotoFragment instance() {
if (photoFragment == null) {
photoFragment = new PhotoFragment();
}
return photoFragment;
}