Android ViewPager get the current View

前端 未结 15 2333
天命终不由人
天命终不由人 2020-11-28 06:24

I have a ViewPager, and I\'d like to get the current selected and visible view, not a position.

  1. getChildAt(getCurrentItem) returns wrong Vi
相关标签:
15条回答
  • 2020-11-28 06:33

    Try this

     final int position = mViewPager.getCurrentItem();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.rewards_viewpager + ":"
                + position);
    
    0 讨论(0)
  • 2020-11-28 06:38

    During my endeavors to find a way to decorate android views I think I defined alternative solution for th OP's problem that I have documented in my blog. I am linking to it as the code seems to be a little bit too much for including everything here.

    The solution I propose:

    • keeps the adapter and the view entirely separated
    • one can easily query for a view with any index form the view pager and he will be returned either null if this view is currently not loaded or the corresponding view.
    0 讨论(0)
  • 2020-11-28 06:39

    I use this method with android.support.v4.view.ViewPager

    View getCurrentView(ViewPager viewPager) {
            try {
                final int currentItem = viewPager.getCurrentItem();
                for (int i = 0; i < viewPager.getChildCount(); i++) {
                    final View child = viewPager.getChildAt(i);
                    final ViewPager.LayoutParams layoutParams = (ViewPager.LayoutParams) child.getLayoutParams();
    
                    Field f = layoutParams.getClass().getDeclaredField("position"); //NoSuchFieldException
                    f.setAccessible(true);
                    int position = (Integer) f.get(layoutParams); //IllegalAccessException
    
                    if (!layoutParams.isDecor && currentItem == position) {
                        return child;
                    }
                }
            } catch (NoSuchFieldException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
            } catch (IllegalAccessException e) {
                Log.e(TAG, e.toString());
            }
            return null;
        }
    
    0 讨论(0)
  • 2020-11-28 06:39

    Please check this

    ((ViewGroup))mViewPager.getChildAt(MyActivity.mViewPager.getCurrentItem()));
    

    else verify this link.

    0 讨论(0)
  • 2020-11-28 06:40
    viewpager.getChildAt(0)
    

    this always returns my currently selected view. this worked for me but I don't know-how.

    0 讨论(0)
  • 2020-11-28 06:41

    I had to do it more general, so I decided to use the private 'position' of ViewPager.LayoutParams

            final int childCount = viewPager.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = viewPager.getChildAt(i);
                final ViewPager.LayoutParams lp = (ViewPager.LayoutParams) child.getLayoutParams();
                int position = 0;
                try {
                    Field f = lp.getClass().getDeclaredField("position");
                    f.setAccessible(true);
                    position = f.getInt(lp); //IllegalAccessException
                } catch (NoSuchFieldException | IllegalAccessException ex) {ex.printStackTrace();}
                if (position == viewPager.getCurrentItem()) {
                    viewToDraw = child;
                }
            }
    
    0 讨论(0)
提交回复
热议问题