Android PagerAdapter,获取当前位置

CQM*_*CQM 28 android arraylist pager android-viewpager

我想得到我的PagerAdapter的可见视图的当前位置

我没有看到一个明显的功能getPosition(),我想要一个.

我想在那个位置向它的arraylist添加一个对象,但我需要先知道它

Jon*_*ona 48

你会用:

int position = mViewPager.getCurrentItem()
Run Code Online (Sandbox Code Playgroud)

  • 我正在使用兼容包,它扩展了PagerAdapter,也没有getCurrentItem方法.我该怎么做?http://pastebin.com/LabjNvqL (2认同)

phi*_*ipp 23

我有这个问题,无法得到这个getCurrentItem()方法.

我最终得到了来自ViewPager而不是来自的位置PageAdapter.该onPageSelected(int currentPage)梅索德越来越当前显示的页面.

//custom PageAdapter implementation
mAdapter = new AwesomePagerAdapter();

//Our custom view pager that extends from ViewPager
mPager = (CustomViewPager) findViewById(R.id.preview_gallery);

mPager.setAdapter(mAdapter);

// get the item that we should be showing from the intent
mCurrentPage = extra.getInt("currentIndex");

// show the item the user picked
mPager.setCurrentItem(mCurrentPage);

// listen for page changes so we can track the current index
mPager.setOnPageChangeListener(new OnPageChangeListener() {

    public void onPageScrollStateChanged(int arg0) {
    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    public void onPageSelected(int currentPage) {
        //currentPage is the position that is currently displayed. 
    }

});
Run Code Online (Sandbox Code Playgroud)

PageAdaper我这样做不起作用,因为我想预加载不可见的图像.instantiateItem(View collection, int position)PageAdapter` 传递的位置是初始化的下一个项目的位置.这与显示的项目无关.