Displaying images in viewpager vertically and even looping
, like: A>B>C>D>A and on click of particular image need to open related site within browser.
Yet I achieved vertical view pager, with the help of this tutorial and looping is also working but onClick of image its not returning correct position its because of getCount() which I increment everytime within on instantiateItem. If I wont do so then it stop looping.
need to fetch correct position while I click on particular image, have a look at logic and suggest me.
Adapter
public class FullScreenImageAdapter extends com.xxxxxx.util.PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
private int mFakeCount = 0,newPosition;
// constructor
public FullScreenImageAdapter(Activity activity, ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
mFakeCount = _imagePaths.size()+1;
}
@Override
public int getCount() {
return this.mFakeCount;
// return this._imagePaths.size();
}
public int getNewPosition(){
return newPosition;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
// image lenght =6
//
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
if (position >= _imagePaths.size()-1) {
newPosition = position%_imagePaths.size();
Log.d("####", "new position="+newPosition);
position = newPosition;
mFakeCount++;
}
Log.d("####", "default position="+position);
inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position));
imgDisplay.setImageBitmap(bitmap);
((VerticalViewPager) container).addView(viewLayout);
viewLayout.setTag(_imagePaths.get(position));
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((VerticalViewPager) container).removeView((RelativeLayout) object);
}
}
OnPageChangeListener
_viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
browser_url = LockScreen.db.getURL("browser_url",_viewPager.getCurrentItem() + 1); //_viewPager.getCurrentItem() return wrong position.
Log.i("browser_url dynamic", browser_url);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels)
{
}
@Override
public void onPageScrollStateChanged(int position) {
Log.v("onPageScrollStateChanged", String.valueOf(position));
}
});