Android PagerAdapter, get current position

前端 未结 5 1250
情话喂你
情话喂你 2021-01-01 09:54

I want to get the current position of the visible view of my PagerAdapter

I didn\'t see an obvious function like getPosition() and I want one.

I

相关标签:
5条回答
  • 2021-01-01 10:33

    You would use:

    int position = mViewPager.getCurrentItem()
    
    0 讨论(0)
  • 2021-01-01 10:38

    Here's an updated solution that I used myself. Since setOnPageChangeListener is now deprecated you must use 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) {
    
            }
        });
    

    The implementation above displays the correct index in my TextView so that positions 0 and the last item in the listcurrentInventory.size() display correctly. Hope this helps someone looking for an updated solution.

    0 讨论(0)
  • 2021-01-01 10:40

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

    if i write after that line

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

    it will show in the logcat the current item page while the oncreate is being run which is always 0

    noteice i have used the object for the viewpager it self not the adapter.

    0 讨论(0)
  • 2021-01-01 10:41

    I had this problem and could not get the getCurrentItem() methode.

    I ended up getting the position from the ViewPager and not from the PageAdapter. The onPageSelected(int currentPage) methode is getting the currently displayed page.

    //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. 
        }
    
    });
    

    Doing it in the PageAdaper didn't work for me as I want to preload images that are not visible. The position that is passed instantiateItem(View collection, int position) of the PageAdapter` is the position of the next item initialized. This has nothing to do with the item that is displayed.

    0 讨论(0)
  • 2021-01-01 10:45

    Try this:

      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) {}
                        })
    
    0 讨论(0)
提交回复
热议问题