Android PagerAdapter,获取当前位置

时间:2011-12-16 17:41:10

标签: android arraylist pager android-viewpager

我想获取我的PagerAdapter

的可见视图的当前位置

我没有看到像getPosition()这样明显的功能,我想要一个。

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

6 个答案:

答案 0 :(得分:45)

你会使用:

int position = mViewPager.getCurrentItem()

答案 1 :(得分: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. 
    }

});

PageAdaper中执行此操作对我来说不起作用,因为我想预加载不可见的图像。传递给PageAdapter的instantiateItem(View collection, int position)的位置是初始化的下一个项目的位置。这与显示的项目无关。

答案 2 :(得分:3)

https://github.com/Shereef/ViewPagerPlusExpandableList/blob/master/src/net/shereef/vewpagerplusexpandablelistexample/ViewPagerPlusExpandableListActivity.java#L204

如果我在该行之后写

    Log.i("pager",myPager.getCurrentItem()+"");

它将在logcat中显示当前项目页面,而oncreate正在运行,始终为0

noteice我已经使用了viewpager的对象而不是适配器。

答案 3 :(得分:1)

这是我用过的更新的解决方案。由于setOnPageChangeListener已被弃用,因此您必须使用addOnPageChangeListener

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int counterPosition;
            if (position == 0 || position <= currentInventory.size()){
                counterPosition = position + 1;
            } else {
                counterPosition = position;
            }

            viewPagerHeader.setText("Prize " + counterPosition + " of " + currentInventory.size());

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

以上实现在我的TextView中显示了正确的索引,因此位置0和列表currentInventory.size()中的最后一项正确显示。 希望这对寻求更新解决方案的人有所帮助。

答案 4 :(得分:0)

尝试一下:

  imageSlider.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
                        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
                        override fun onPageSelected(position: Int) {Log.d(TAG, "Page No.: $position")}
                        override fun onPageScrollStateChanged(state: Int) {}
                    })

答案 5 :(得分:0)

我昨天刚遇到这个问题。我需要在页面对用户可见时开始在每个页面中播放动画,而不是在此之前。

我的适配器继承自 PagerAdapter,我发现有一个函数 setPrimaryItem(),每次向用户显示页面作为当前页面时都会触发该函数。

<块引用>

调用以通知适配器当前被认为是“主要”的项目,即作为当前页面显示给用户的项目。当适配器不包含任何项时,不会调用此方法。

不仅您有页面可见时的回调,而且此回调还提供当前页面的位置。

class MyAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        ...
    }

    @Override
    public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) {
        ...
    }

    @Override
    public void setPrimaryItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
        super.setPrimaryItem(container, position, object);

        // `position` gives you the position of the current page

        // And this is how I managed to play the animation (Lottie library) when the page is visible to the user
        LottieAnimationView animation = ((ViewGroup) object).findViewById(R.id.my_animation_view);
        animation.playAnimation();
    }
}
相关问题