在viewpager vertically and even looping
中顯示圖像,如:A> B> C> D> A並點擊特定圖像需要打開瀏覽器中的相關網站。圓形垂直視圖打印機
然而,我實現縱向看尋呼機,與this tutorial的幫助和循環也在努力,但圖像的onClick它沒有返回正確的位置,因爲它的getCount將()我內的instantiateItem增加每次。 If I wont do so then it stop looping.
需要取得正確的位置,而我點擊特定的圖像,看看邏輯和建議我。
適配器
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));
}
});
謝謝,它的工作原理! – RobinHood
這可行,但是酒吧遠不是理想的解決方案。整個'ViewPager'應該重做,只能保持數量+2項!如果用戶滾動了很多,你的應用會崩潰,因爲ViewPager中有很多項目。 – Leandros