I have a memory leak (I guess?), somewhere in my app. And i really have no idea where, i have been trying to follow several link explaining different ways to close OOM errors, but without luck.
The thing is, i have a viewpager with 209 pages (data loaded dynamically, and the same single fragment is being used for all 209 pages)
I can scroll through around 20 pages or so before i start getting any memory errors.
All images are loaded by Universal-Image-Loader and are only cached on disc, not in memory.
I am using a FragmentStatePagerAdapter.
Heres the code, i really hope you guys can find something i cant.. Also, if you find anything that should be different, can be optimized or anything, please do tell. :
Activity:
public class MovieInfo extends SherlockFragmentActivity{
MyAdapter mAdapter;
ViewPager mPager;
static ArrayList<Movie> items;
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public void destroyItem(View collection, int position, Object o) {
View view = (View)o;
((ViewPager) collection).removeView(view);
view = null;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Fragment getItem(int position) {
Fragment f = new MovieInfoFragment();
Bundle args = new Bundle();
args.putParcelable("item", items.get(position));
f.setArguments(args);
return f;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_info_pager);
int mIndex = getIntent().getExtras().getInt("index", 0);
DataBaseHelper db = new DataBaseHelper(this);
items = (ArrayList<Movie>) db.getAllMovies();
db.close();
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(mIndex);
}
}
Fragment:
public class MovieInfoFragment extends SherlockFragment{
private boolean clearArt = true;
private DisplayImageOptions options;
private Movie movie;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
movie = data.getParcelable("item");
options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.resetViewBeforeLoading()
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.movie_info, container, false);
if(movie != null){
final ImageView clearArtID = (ImageView) view.findViewById(R.id.Movie_InfoClearArt);
final TextView title = (TextView) view.findViewById(R.id.Movie_InfoTitle);
final TextView genre = (TextView) view.findViewById(R.id.Movie_InfoGenre);
if(clearArt && movie.getClearArt() != null){
clearArtID.setVisibility(ImageView.VISIBLE);
title.setVisibility(TextView.GONE);
genre.setVisibility(TextView.GONE);
}else{
clearArtID.setVisibility(ImageView.GONE);
title.setVisibility(TextView.VISIBLE);
genre.setVisibility(TextView.VISIBLE);
}
ImageView fanart = (ImageView) view.findViewById(R.id.Movie_InfoFanart);
ImageLoader.getInstance().displayImage(movie.getFanart(), fanart, options);
final TextView lineone = (TextView) view.findViewById(R.id.Movie_InfoLineOneText);
final TextView linetwo = (TextView) view.findViewById(R.id.Movie_InfoLineTwoText);
final ImageView cover = (ImageView) view.findViewById(R.id.Movie_InfoCover);
final Movie mov = movie;
ImageLoader.getInstance().displayImage(movie.getThumb(), cover, options);
if(clearArt && movie.getClearArt() != null){
ImageLoader.getInstance().displayImage(movie.getClearArt(), clearArtID, options, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) { }
#Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
clearArtID.setVisibility(ImageView.GONE);
title.setVisibility(TextView.VISIBLE);
genre.setVisibility(TextView.VISIBLE);
title.setText(mov.getTitle().toUpperCase(Locale.ENGLISH));
genre.setText(mov.getGenreString());
lineone.setText(String.valueOf(mov.getDirectorString()));
linetwo.setText("WRITER");
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { }
#Override
public void onLoadingCancelled(String imageUri, View view) { }
});
lineone.setText(movie.getGenreString());
linetwo.setText(movie.getDirectorString());
}else{
title.setText(movie.getTitle().toUpperCase(Locale.ENGLISH));
genre.setText(movie.getGenreString());
lineone.setText(String.valueOf(movie.getDirectorString()));
linetwo.setText("WRITER");
}
TextView rating = (TextView) view.findViewById(R.id.Movie_InfoRating);
rating.setText(movie.getRating() + " / 10");
TextView runtime = (TextView) view.findViewById(R.id.Movie_InfoRuntime);
runtime.setText(movie.getPlaytime()/60 + " min");
TextView year = (TextView) view.findViewById(R.id.Movie_InfoYear);
year.setText(String.valueOf(movie.getYear()));
HorizontalListView listview = (HorizontalListView) view.findViewById(R.id.Movie_InfoActorList);
listview.setAdapter(mAdapter);
TextView plot = (TextView) view.findViewById(R.id.Movie_InfoPlot);
plot.setText(movie.getPlot());
if(movie.getCdart() != null){
ImageView cdArt = (ImageView) view.findViewById(R.id.Movie_InfoCD);
ImageLoader.getInstance().displayImage(movie.getCdart(), cdArt, options);
}
}
return view;
}
}
EDIT:
I do know, that the reason my app is getting OutOfMemory errors is, that it doesn't release the bitmaps in my large viewpager for garbage collection, but how do i get my app to release them? (read: garbage collect) when the are not currently in view, single previous or single next "slide"?
Any help appreciated!
You should probably be calling the super implementation of the destroyItem method
Related
In my application i am using recycler view to show images, click on every image should open a view pager and show current image, but the problem is that when i click any of image for first time it shows blank fragment, after this i click back key it shows recycler view again, now my app works fine, click on any image shows fragment containing image perfectly. Thanks in advance.
Here is my code for recycler view
#Override
public void onBindViewHolder(ViewHolder holder, final int position){
CardView cv = holder.cardView;
// Skiped un neccessery code from here
cv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent;
if (is_image_file){
intent = new Intent(context, ImageOpener.class);
}
else {
intent = new Intent(context, VideoPlayer.class);
}
intent.putExtra("POSITION", position );
context.startActivity(intent);
}
});
}
Here is my code for ImageOpener.java class
public class ImageOpener extends AppCompatActivity {
Utils utils = new Utils();
private ArrayList<String> images_paths;
private int size_of_pager, position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opener);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
images_paths = utils.load_images();
position = getIntent().getExtras().getInt("POSITION");
size_of_pager = images_paths.size();
SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(size_of_pager);
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(position);
}
private class SectionsPagerAdapter extends FragmentPagerAdapter{
public SectionsPagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public int getCount(){
return size_of_pager;
}
#Override
public Fragment getItem(int pos){
return new OpenImageSlideFragment(pos);
}
}
}
Here is my code for OpenImageSlideFragment.java class
public class OpenImageSlideFragment extends Fragment {
Utils utils = new Utils();
ImageButton save_button, share_button;
private ArrayList<String> images_path;
private int position;
String current_file;
public OpenImageSlideFragment(int position) {
this.position = position;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_open_image_slide, container, false);
TouchImageView preview = (TouchImageView) rootView.findViewById(R.id.preview_image);
images_path = utils.load_images();
current_file = images_path.get(position);
File imgFile = new File(current_file);
Uri uri = Uri.fromFile(imgFile.getAbsoluteFile());
Picasso.get().load(uri).into(preview);
return rootView;
}
}
Code for Utils.load_image()
public ArrayList<String> load_images(){
System.out.println("in load image function");
File path = new File(Environment.getExternalStoragePublicDirectory(""), "some/path/");
File[] all_images = path.listFiles();
ArrayList<String > imagePaths = new ArrayList<>();
// for (int j=0; j<=2; j++) //for just testing purpose
for (int i = 0; i < all_images.length; i++) {
System.out.println(all_images[i].getAbsolutePath());
if (all_images[i].getAbsolutePath().endsWith(".jpg")) {
imagePaths.add(all_images[i].getAbsolutePath());
}
}
return imagePaths;
}
if you are using viewpager where you want to show multiple images then check this link it will help you
https://youtu.be/DBOIQP3lI_w
I am working on an image gallery, where I fetch all the images stored in the Android phone gallery into my application. Please find the link that I followed to develop that image gallery.
Get all images from Gallery into android application Programmatically
I have trying for hours to figure out how to create a full screen slideshow of images. I am able to achieve the full screen image, but it shows the same image all and always, even though the gallery has 3 images. I am using a ViewPagerAdapter to set the image in the adapter for showing the full screen preview. The image slider isn't working and I don't have an idea of why its not working. Please find the source code of what I have tried so far.
Model_images.java
public class Model_images{
String str_folder;
ArrayList<String> al_imagepath;
public String getStr_folder() {
return str_folder;
}
public void setStr_folder(String str_folder) {
this.str_folder = str_folder;
}
public ArrayList<String> getAl_imagepath() {
return al_imagepath;
}
public void setAl_imagepath(ArrayList<String> al_imagepath) {
this.al_imagepath = al_imagepath;
}
}
I followed the same exact tutorial, where I have given a link to the original source code. Even though I have given a link, I'm posting the source code of the ViewPager I tried.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
gridView = (GridView)findViewById(R.id.gv_folder);
int_position = getIntent().getIntExtra("value", 0);
adapter = new GridViewAdapter(this, GalleryFragment.al_images,int_position);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putInt("position", int_position);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
newFragment.setArguments(bundle);
newFragment.show(ft, "slideshow");
}
});
}
Here is the SlideshowFragmentDialog to show the images in a slideview.
SLideShowFragmentDialog.java
public class SlideshowDialogFragment extends DialogFragment {
private String TAG = SlideshowDialogFragment.class.getSimpleName();
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private int selectedPosition = 0;
static SlideshowDialogFragment newInstance() {
SlideshowDialogFragment frag = new SlideshowDialogFragment();
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewpager);
//images = (ArrayList<Image>) getArguments().getSerializable("images");
selectedPosition = getArguments().getInt("position");
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
setCurrentItem(selectedPosition);
return v;
}
private void setCurrentItem(int position) {
viewPager.setCurrentItem(position, false);
displayMetaInfo(selectedPosition);
}
// page change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
displayMetaInfo(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
};
private void displayMetaInfo(int position) {
Model_images imagePath = GalleryFragment.al_images.get(position);
Toast.makeText(getContext(), imagePath.toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
// adapter
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public MyViewPagerAdapter() {
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.activity_full_screen, container, false);
ImageView imageViewPreview = (ImageView) view.findViewById(R.id.fullScreenImageView);
Model_images image = GalleryFragment.al_images.get(position);
Glide.with(getContext()).load(image.getAl_imagepath().get(position))
.thumbnail(0.5f)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageViewPreview);
container.addView(view);
return view;
}
#Override
public int getCount() {
return GalleryFragment.al_images.size();
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
I am not posting the source code for the MainActivity and the Adapter classes, since I have given a link to the project directly. I followed the exact same link. If you still want me to post the source codes, I can edit the question and post the missing stuffs. I don't wanna make this post big with all the code base.
Thanks in advance. Any help is appreciated. Please help me on how to achieve full screen slideshow of images.
I've tried to post a similar questions without any answer, though of posting again.
The issue I am having is with viewing Fragment through Tabs, I have 3 Tabs in an AppBar displayed with ViewPager & FragmentPagerAdapter.
The problem is when the activity start, one of the tabs does not behave in a consistent way, upon logging in to the app & activity for the first time it would display the content of the Fragment ( Video clips ) but during switching between activities in the app, and going back to the Activity containing the 3 Tabs, choosing that specific Tab does not display anything, althou in the Logcat, i could tell it is retrieving Data related to the Vid Clip that should be displayed, also going back that Activity i see in the Logcat that even when i choose a different Tab, ( pictures ) Data from the 2 others Tab is being retrieved !?
The setup is as follows;
Main activity;
public class Member extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.member);
Firebase.setAndroidContext(this);
ButterKnife.bind(this);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new PicContentMMFragment(), "Pictures");
adapter.addFragment(new VidContentFragment(), "Videos");
adapter.addFragment(new LocationsContentFragment(), "Locations");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Well, as one of the users commented on the post pointed out that the problem could be from the specific fragemnt itself and not the way Tabs and Viewpager following is the VidContentFragment;
public class VidContentFragment extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
Log.i("MyTag_onCreate","vidContentFragment_Loaded");
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public VideoView video;
public TextView authorName;
public TextView ratingValue;
public TextView locationValue;
public RatingBar ratingB;
public Button submitRating;
public LinearLayout placeNameHolder;
public int newRating;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_card_vid, parent, false));
authorName = (TextView) itemView.findViewById(R.id.card_title);
ratingValue = (TextView) itemView.findViewById(R.id.ratingValue);
locationValue = (TextView) itemView.findViewById(R.id.locationValue);
video = (VideoView) itemView.findViewById(R.id.placeVid);
ratingB = (RatingBar)itemView.findViewById(R.id.ratingBarMM);
ratingB.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
newRating = (int) ratingB.getRating();
}
});
submitRating = (Button) itemView.findViewById(R.id.submitRatingMM);
submitRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
public_id = publicID[getPosition()];
Log.i("MyTag_Rating",public_id);
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.authorName.setText(author[position]);
holder.ratingValue.setText(ratingV[position]);
holder.locationValue.setText(locationV[position]);
Uri video = Uri.parse(mVideos[position]);
holder.video.setVideoURI(video);
holder.video.setMediaController(new MediaController(mContext));
holder.video.requestFocus();
holder.video.seekTo(1000);
holder.video.pause();
try{
String url1 = mVideos[position];
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getVibrantColor(mContext.getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
else
Log.e("MyTag_BMP","The Bitmap is NULL");
}catch (Exception e){
}
}
#Override
public int getItemCount() {
return LENGTH;
}
}
}
Some part of the code (related to retrieving data from firebase) is left out to shorten it.
Although the other 2 fragments are organized the same way, the behavior of this fragment is inconsistent ?
Here is the version of the adapter with viewholder that I have for a recyclerview you said would likely be helpful. You'll need to refactor to adjust for your situation. I have gutted it mostly to help show just the bare interactions.
public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalCardViewHolder>{
private List<Animals> animalList;
public AnimalAdapter adapter = this;
Context context;
public AnimalAdapter(List<Animals> animalList, Context context) {
this.animalList = animalList;
this.context = context;
this.setInterface((iAdapterInterface) context);
}
#Override
public int getItemCount() {
return animalList.size();
}
#Override
public void onBindViewHolder(AnimalCardViewHolder holder, int position) {
Animals animal = animalList.get(position);
holder.txtName.setText(animal.getName());
holder.txtType.setText(animal.getType());
}
#Override
public AnimalCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.animal_card_view, parent, false);
return new AnimalCardViewHolder(itemView);
}
public class AnimalCardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView txtName;
protected TextView txtType;
public AnimalCardViewHolder(final View itemView) {
super(itemView);
txtName = (TextView) itemView.findViewById(R.id.txtName);
txtType = (TextView) itemView.findViewById(R.id.txtType);
imgClose = (ImageView) itemView.findViewById(R.id.txtRemove);
listItem = itemView.findViewById(R.id.list_item);
listItem.setOnClickListener(this);
imgClose.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txtRemove:
break;
case R.id.list_item:
break;
}
}
}
}
am trying to hide and show a View when imageView is being tapped , i have 3 fragments in my ViewPager so the problem is that when am on my first View and i tap the imageView it works fine (my View got hided) but when am on Fragment no. 2 or three and i tap on the imageView it never hides the View its only working on my first fragment , this is my code :
public class MainActivity extends AppCompatActivity {
private static int[] imageArray;
static ImageLoader imageLoader;
static ArrayList<String> urlArray;
static DisplayImageOptions options;
static ProgressBar spinner;
static PhotoViewAttacher mAttacher;
static Boolean isToolBarShown = true;
static Button nextButton , previousButton;
static ViewPager viewPager ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlArray = new ArrayList<>();
urlArray.add("http://i.imgur.com/uLRgvM8.png");
urlArray.add("http://i.imgur.com/LUuJ4FO.png");
urlArray.add("http://i.imgur.com/7SSBNBA.jpg");
// Create global configuration and initialize ImageLoader with this config
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache()).build();
ImageLoader.getInstance().init(config);
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisk(true)
.resetViewBeforeLoading(true).build();
//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView);
ImageFragmentPagerAdapter imageFragmentPagerAdapter = new ImageFragmentPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(imageFragmentPagerAdapter);
}
public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter {
public ImageFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return urlArray.size();
}
#Override
public Fragment getItem(int position) {
//SwipeFragment fragment = new SwipeFragment();
return SwipeFragment.newInstance(position);
}
}
public static class SwipeFragment extends Fragment {
private int position;
private ImageView imageView;
private RelativeLayout relativeLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false);
imageView = (ImageView) swipeView.findViewById(R.id.imageView);
spinner = (ProgressBar) swipeView.findViewById(R.id.spinner);
relativeLayout = (RelativeLayout) swipeView.findViewById(R.id.tool_bar);
Bundle bundle = getArguments();
nextButton = (Button)swipeView.findViewById(R.id.next_button);
previousButton = (Button)swipeView.findViewById(R.id.button_previous);
position = bundle.getInt("position");
Toast.makeText(getContext(),"NEW PAGE CALLED",Toast.LENGTH_SHORT).show();
if (isToolBarShown){
relativeLayout.setVisibility(View.VISIBLE);
}else {
relativeLayout.setVisibility(View.GONE);
}
mAttacher = new PhotoViewAttacher(imageView);
// viewPager.OnPageChangeListener(new ViewPager.OnPageChangeListener() {
// #Override
// public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//
// }
//
// #Override
// public void onPageSelected(int position) {
//
// loadImageView(position,imageView);
//
// }
//
// #Override
// public void onPageScrollStateChanged(int state) {
//
// }
// });
previousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem()-1,true);
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1,true);
}
});
return swipeView;
}
public void loadImageView(int position, final ImageView imageView ){
imageLoader.displayImage(urlArray.get(position), imageView, options, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
#Override
public void onViewTap(View view, float x, float y) {
if (view == imageView){
if (relativeLayout.getVisibility() == View.VISIBLE){
isToolBarShown = false;
relativeLayout.setVisibility(View.GONE);
}else {
isToolBarShown = true;
relativeLayout.setVisibility(View.VISIBLE);
}
Log.d("OOPS","WHAT THE.., WHAT IS WRONG HERE");
}
}
});
if(mAttacher!=null){
mAttacher.update();
}else{
mAttacher = new PhotoViewAttacher(imageView);
}
spinner.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
spinner.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
spinner.setVisibility(View.VISIBLE);
spinner.setProgress((current*100)/total);
}
});
}
static SwipeFragment newInstance(int position) {
SwipeFragment swipeFragment = new SwipeFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position);
swipeFragment.setArguments(bundle);
return swipeFragment;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// load data here
// if (getView() != null) {
// isViewShown = true;
// // fetchdata() contains logic to show data when page is selected mostly asynctask to fill the data
// loadImageView(position , imageView);
// } else {
// isViewShown = false;
// }
}
}
}}
am trying to hide and show the RelativeView , if anybody knows whats wrong or missing in my code then please do correct me
i think you have to try fragmentstatepageradapter instead of FragmentPagerAdapter. it may help you. you can see the the difference betwwen them here :
Difference between FragmentPagerAdapter and FragmentStatePagerAdapter
You didn't provide all of code, but as you said in comments mAttacher is your static variable from outside of SwipeFragment. I think that
relativeLayout
spinner
imageLoader
are also static members from outside of SwipeFragment. If they are from your R.layout.swipe_fragment layout then you should use each relativeLayout, spinner and imageLoader instance per SwipeFragment.
Now you are swiping to another fragment and you see it. But instances of classes i mentioned above are from another fragment. And that is why it does not working for you as it should. Try to do that. And you can provide more of code.
Good luck!
Setting viewpager offscreenpagelimit mayt help you:)
mViewPager.setoffscreenpagelimit(2)
//2 is important over here.
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
//use above function to set fragment
I'm displaying images from resource/drawable folder in full screen using viewpager. It's working fine and are sliding well. now i want to save image name in sdcard,but i am not able to get image file name. if any one will please help me . I have google it, i didn't get correct solution for my expection.
code
public class MainActivity extends Activity{
//private static Context context;
ExtendedViewPager mViewPager;
private static Uri imageUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());
}
static class TouchImageAdapter extends PagerAdapter {
private static int[] images = { R.drawable.nature_1, R.drawable.nature_2, R.drawable.nature_3, R.drawable.nature_4, R.drawable.nature_5 };
}
#Override
public int getCount() {
return images.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
//Context context = container.getContext();
TouchImageView img = new TouchImageView(container.getContext());
img.setImageResource(images[position]);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int imagid=images[position];
System.out.println("Image name " + images[position]);
bm = BitmapFactory.decodeStream(v.getResources().getDrawable(imagid));// here i need to pass my resId, i can't able to pass
}
});
//img.setScaleType(TouchImageView.ScaleType.CENTER_CROP);
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
create a String[] which contains file name and set it's itme to img as tag and in onClick get the tag. do something like this
private static String[] imagesName = {"fiel1","file2",.....};
.
.
img.setImageResource(images[position]);
img.setTag(imagesName[position]);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String filename = v.getTag().toString();
}
}
Edit
If you don't want to create String[] then set the images[position] as Tag of img and in onClick get it from tag like getResources().getResourceName((Integer)v.getTag());
This helps me.