HackyView Pager is not showing the correct position - android

https://github.com/chrisbanes/PhotoView
i am using this library to show some photos in viewpager (HackyViewPager)
it works fine to show the pictures
but when i want to show name of proper photo that is showing it shows the wrong position name...
i'm wondering of this error
mViewPager.setAdapter(new SamplePagerAdapter());
public class SamplePagerAdapter extends PagerAdapter {
final String[][] sDrawables = {{"Chrysanthemum.jpg","0"},{"Desert.jpg","1"},{"Hydrangeas.jpg","2"},{"Jellyfish.jpg","3"}, {"Koala.jpg","4"},{"Lighthouse.jpg","5"},{"Penguins.jpg","6"},{"Tulips.jpg","7"} };
#Override
public int getCount() {
return sDrawables.length;
}
#Override
public View instantiateItem(ViewGroup container,int position) {
PhotoView photoView = new PhotoView(container.getContext());
Log.i("xxx position ",position + " " + sDrawables[position][1]);
tvImageName.setText(sDrawables[position][1]);
Picasso.with(ViewPagerActivity.this)
.load("http://myserver/uploads/" + sDrawables[position][0])
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.into(photoView);
container.addView(photoView, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
return photoView;
}
#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;
}
}
would you please help me to find why this work like this?

Finally i found the answer myself
We have to add a listener (setOnPageChangeListener) for HackyViewPager instead of adding code inside the Adapter.
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.i("xxx position ",position + " " + sDrawables[position][1]);
tvImageName.setText(sDrawables[position][1]);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Related

Get Current item ViewPageAdapter Android

Im searching in all internet but I dont find any REAL solution about my problem.
I have a class with a Viewpager. (Class DescripcionRutinas)
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addNumDias(numDias);
adapter.setDias(dias);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(5);
}
And I have a ViewPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private int numDias;
private int count = 0;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
ArrayList<Dia> dias;
#Override
public Fragment getItem(int position) {
OneFragmentSeries fragmento = new OneFragmentSeries();
Dia ex = dias.get(**I NEED CURRENT ITEM HERE**);
nomDia = ex.getNombre();
fragmento.setSeries((ArrayList<Serie>)((ArrayList<Dia>)WorkoutYourself.dameWorkoutYourself().dameRutina(nomRutina).getDias()).get(position).getSeries());
return fragmento;
}
I need the current item position here.
NOTE: ViewPagerAdapter is INSIDE DescripcionRutinas.
Thanks guys!
Try to use yourViewPager.getCurrentItem() it should work to get your visible pager position.
Update
onPageSelected(int currentPage) should do the stuff
dia.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int currentPage) {
//here you get your current position
}
});
Read this documentation
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int positionCurrentPage) {
if(positionCurrentPage == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Android InvalidStateException on ViewPager when setting page limit too small

I have a problem with my ViewPager Image slider gallery. I am setting my page limit to 3 but then my app craches on the third image because the child has already a parent. I set the limit to this number because I want to avoid large memory leaks, event with 3 images it uses up like 150Mb of allocated memory which is incredibly huge ammount. Maybe if I resize the image before uploading I can get them to work with more pages ? Anyways, here is my code for the viewPager:
ViewPager mViewPager = (ViewPager) findViewById(R.id.imagepager);
ViewerAdapter adapter = new ViewerAdapter(this);
//adapter.notifyDataSetChanged();
mViewPager.setAdapter(adapter);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setCurrentItem(number);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
time.setText(caluculateTimeAgo(MainActivity.time[position]));
if (MainActivity.distances[position] < 1000) {
dist.setText(String.valueOf(MainActivity.distances[position]) + "m");
} else {
long f = (long) (MainActivity.distances[position] / 1000);
dist.setText(String.valueOf(f) + "km");
}
author.setText(MainActivity.users[position]);
text.setText(MainActivity.text1[position]+MainActivity.tags[position]);
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
and here is my adapter definition:
public class ViewerAdapter extends PagerAdapter {
Context mContext;
public ViewerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
LayoutInflater mLayoutInflater;
#Override
public int getCount() {
return MainActivity.imgLink.length;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (View) arg1;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Picasso.with(container.getContext()).cancelRequest((ImageView) object);
}
#Override
public CharSequence getPageTitle(int position) {
return (position + "/" + getCount());
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
final View itemView = mLayoutInflater.inflate(R.layout.pager_item,container,false);
final ImageView photo = (ImageView) itemView.findViewById(R.id.photoSlide);
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fullscreen = ((BitmapDrawable)photo.getDrawable()).getBitmap();
Intent intent = new Intent(ShowMarkerContent.this, FullScreenImageActivity.class);
startActivity(intent);
}
});
Picasso.with(container.getContext()).load("MyWebsite" + MainActivity.imgLink[position]).into(photo);
container.addView(photo);
return photo;
}
}
Well, I managed to make a work-around for this problem. I forgot to set the cropping of the photo and therefore the photos were too large for this. And also, I set the offScreenPageLimit to 30 to avoid this error (This is that work-around because with large quantity of image it will come to out of memory error) So now, here is my adapter code:
#Override
public Object instantiateItem(ViewGroup container, int position) {
final View itemView = mLayoutInflater.inflate(R.layout.pager_item,container,false);
final ImageView photo = (ImageView) itemView.findViewById(R.id.photoSlide);
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fullscreen = ((BitmapDrawable)photo.getDrawable()).getBitmap();
Intent intent = new Intent(ShowMarkerContent.this, FullScreenImageActivity.class);
startActivity(intent);
}
});
Picasso.with(container.getContext()).load("MyWebsite" + MainActivity.imgLink[position]).fit().centerCrop().into(photo);
container.addView(itemView);
return itemView;
}

Swipe image views left and right like a drag and drop

I have an image view. I want to touch the image and drag it to left or right. When the image is dragged to the left, the new image should appear from the array. When i drag it to right it should be saved in another activity. How can i possibly do this, if somebody could guide me to a tutorial or example. Thank-you
You can use ViewPager and PageAdapter.
protected class ImagePagerAdapter extends PagerAdapter {
public ImagePagerAdapter() {
}
#Override
public int getCount() {
return getImages().size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_page_view, null);
String url = getImages().get(position);
if (url != null) {
ImageLoader.getInstance().displayImage(url, (ImageView) view.findViewById(R.id.image_page_view));
}
container.addView(view, 0);
return view;
}
}
...
imagePager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
}
#Override
public void onPageSelected(int i) {
//logic to open activity or other action..;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
...
imagePager.setAdapter(new ImagePagerAdapter());

how to get item number in view pager?

how to get current item no in viewpager.
#Override
public Object instantiateItem(ViewGroup paramView, int paramInt) {
int itemno1= getItem(pager.getCurrentItem());
int itemno2= getItem(paramInt);
Toast.makeText(context, "postition"+itemno1, Toast.LENGTH_SHORT).show();
//Toast.makeText(context, "postition"+itemno2, Toast.LENGTH_SHORT).show();
}
but this methods are not working properly.when ever i swipe front and back it gives the wrong item no..
set ViewPager.setOnPageChangeListener on your viewpager.
Some thing like this-
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
}
#Override
public void onPageSelected(int i) {
// here you will get the position of selected page
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
try like this
ViewPager.getCurrentItem();
or
pageListener = new PageListener();
ViewPager.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}

How to force ViewPager to re-instantiate its items [duplicate]

This question already has answers here:
Why `PagerAdapter::notifyDataSetChanged` is not updating the View?
(43 answers)
Closed 10 years ago.
I am using ViewPager to allow user to swipe between its views. Is there a way how to force this ViewPager to reload/re-instantiate its views in case that they are no longer valid or needs to be refreshed? I tried to call notifyDataSetChanged() on its adapter but this does not invoke instantiateItem() method again.
EDIT
Here is the class that extends from ViewPager and its adapter definision. Bellow is the refresh() method that I call when I want to force to refresh items.
public class DayFlipper extends ViewPager {
public class FlipperAdapter extends PagerAdapter {
#Override
public int getCount() {
return DayFlipper.DAY_HISTORY;
}
#Override
public void startUpdate(View container) {
}
#Override
public Object instantiateItem(View container, int position) {
Log.d(TAG, "instantiateItem(): " + position);
Date d = DateHelper.getBot();
for (int i = 0; i < position; i++) {
d = DateHelper.getTomorrow(d);
}
d = DateHelper.normalize(d);
CubbiesView cv = new CubbiesView(mContext);
cv.setLifeDate(d);
((ViewPager) container).addView(cv, 0);
// add map
cv.setCubbieMap(mMap);
cv.initEntries(d);
return cv;
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((CubbiesView) object);
}
#Override
public void finishUpdate(View container) {
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((CubbiesView) object);
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
...
public void refresh() {
getAdapter().notifyDataSetChanged();
}
}
Had the same problem. For me it worked to call
viewPage.setAdapter( adapter );
again which caused reinstantiating the pages again.
I have found a solution. It is just a workaround to my problem but currently the only solution.
ViewPager PagerAdapter not updating the View
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Does anyone know whether this is a bug or not?
public class DayFlipper extends ViewPager {
private Flipperadapter adapter;
public class FlipperAdapter extends PagerAdapter {
#Override
public int getCount() {
return DayFlipper.DAY_HISTORY;
}
#Override
public void startUpdate(View container) {
}
#Override
public Object instantiateItem(View container, int position) {
Log.d(TAG, "instantiateItem(): " + position);
Date d = DateHelper.getBot();
for (int i = 0; i < position; i++) {
d = DateHelper.getTomorrow(d);
}
d = DateHelper.normalize(d);
CubbiesView cv = new CubbiesView(mContext);
cv.setLifeDate(d);
((ViewPager) container).addView(cv, 0);
// add map
cv.setCubbieMap(mMap);
cv.initEntries(d);
adpter = FlipperAdapter.this;
return cv;
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((CubbiesView) object);
}
#Override
public void finishUpdate(View container) {
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((CubbiesView) object);
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
...
public void refresh() {
adapter().notifyDataSetChanged();
}
}
try this.

Categories

Resources