从ViewPager获得专注的视图

我使用ViewPager左右轻扫切换视图。

ViewPager需要一个适配器,所以我build立了这个:

public class ListViewPagerAdapter extends PagerAdapter { protected static final String TAG = "ListViewPagerAdapter"; protected static final int NUM_VIEWS = 3; protected final Activity mActivity; public ListViewPagerAdapter(Activity activity) { mActivity = activity; } @Override public int getCount() { return NUM_VIEWS; } @Override public void startUpdate(View container) {} @Override public Object instantiateItem(View container, int position) { // ViewPager ViewPager viewPager = (ViewPager) container; // Wird verwendet, um die Views aufzurufen LayoutInflater layoutInflater = mActivity.getLayoutInflater(); // Standardmäßig ist news eingeblendet View view = layoutInflater.inflate(R.layout.news_fragment, null); // Falls sich die Position verändert, so verändert sich auch die View if (position == 0) { view = layoutInflater.inflate(R.layout.favorite_fragment, null); } else if (position == 2) { view = layoutInflater.inflate(R.layout.videos_fragment, null); } // View einblenden viewPager.addView(view, 0); return view; } @Override public void destroyItem(View container, int position, Object object) { // ViewPager ViewPager viewPager = (ViewPager) container; // View View view = (View) object; // View löschen viewPager.removeView(view); } @Override public void finishUpdate(View container) {} @Override public boolean isViewFromObject(View view, Object object) { View _view = (View) object; return view == _view; } @Override public Parcelable saveState() { return null; } @Override public void restoreState(Parcelable state, ClassLoader loader) {} } 

现在,我想通过viewpager获得当前的重点视图。 我试过getChildAt(x),但它不起作用。

有没有一些例子,或者你有什么想法如何获得当前的观点?

谢谢

可以通过覆盖[PagerAdapter.setPrimaryItem]( http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html# )来保存当前活动的对象(View,Fragment,…) setPrimaryItem%28android.view.ViewGroup,%20int,%20java.lang.Object%29 )方法。 例如:

 private View mCurrentView; @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { mCurrentView = (View)object; } 

你必须注册一个监听器到你的ViewPager

 pager.setOnPageChangeListener(new MyPageChangeListener()); 

您必须通过扩展存根侦听器来自定义侦听器:

 private int focusedPage = 0; private class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener { @Override public void onPageSelected(int position) { focusedPage = position; } } 

我通过查看兼容性库中的ViewPager.java源代码find了这个。 我读过,我们可以做更多,例如catch onPageScrollStateChanged

为了构build适配器,我使用了这篇博文中的代码。 你可能想看看。

您可以在instantiateItem方法中为创build的视图添加标签:

 view.setTag(position); 

稍后您可以通过以下方式访问当前选定的视图

 mPager.findViewWithTag(mPager.getCurrentItem()); 

我最近需要实现这个确切的解决scheme。 这是我做的方式:

 Map<Integer, Object> views = Maps.newHashMap(); @Override public Object instantiateItem(View container, int position) { /* Create and add your View here */ Object result = ??? views.put(position, result); return result; } @Override public void destroyItem(View container, int position, Object object) { /** Remove your View here */ views.remove(position); } protected View findViewForPosition(int position) { Object object = views.get(position); if (object != null) { for (int i = 0; i < getChildCount(); i++) { View view = getChildAt(i); if (isViewFromObject(view, object)) { return view; } } } return null; } 

尝试这个:

 public View getCurrentView(ViewPager pager) { for (int i = 0; i < pager.getChildCount(); i++) { View child = pager.getChildAt(i); if (child.getX() <= pager.getScrollX() + pager.getWidth() && child.getX() + child.getWidth() >= pager.getScrollX() + pager.getWidth()) { return child; } } return getChildAt(0); } 

为了获得专注的观点,我使用这种方式:

当我膨胀我的观点之前,将其添加到ViewPager我设置标签。 然后我检查这个标签。

 private View getCurrentView() { for (int i = 0; i < pager.getChildCount(); i++) { View v = pager.getChildAt(i); if (v != null) { if (v.getTag().equals(pageList.get(pager.getCurrentItem()).getTag())) return v; // pageList is a list of pages that I pass to page adapter } } return null; } 

给出的答案并不适合我,他们都有不必要的副作用。 要么我必须添加不必要的variables(我喜欢干净的代码),或者我必须解决Android框架本身。

我的解决scheme基于reflection,它访问ViewPager中所有对象的数组列表,并返回ViewPager中当前选定的对象。 它几乎没有副作用(reflection很慢,现有类的子类化),并保持代码清洁。

 public class ViewPagerEx extends ViewPager { private static final String TAG = ViewPagerEx.class.getSimpleName(); public ViewPagerEx(Context context, AttributeSet attrs) { super(context, attrs); } public ViewPagerEx(Context context) { super(context); } public Object getCurrentObject() { try { final Field itemsField = ViewPager.class.getDeclaredField("mItems"); itemsField.setAccessible(true); final ArrayList<Object> items = (ArrayList<Object>) itemsField.get(this); final int currentItemIndex = getCurrentItem(); if (currentItemIndex < 0 || currentItemIndex >= items.size()) { return null; } final Object infoItem = items.get(getCurrentItem()); final Class<?> itemInfoClass = findItemInfoClass(ViewPager.class.getDeclaredClasses()); final Field objectField = itemInfoClass.getDeclaredField("object"); objectField.setAccessible(true); return objectField.get(infoItem); } 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; } private Class<?> findItemInfoClass(final Class<?>[] classes) throws IllegalArgumentException { for (int i = 0; i < classes.length; i++) { if (classes[i].getSimpleName().equals("ItemInfo")) { return classes[i]; } } throw new IllegalArgumentException("cannot find class ItemInfo"); } } 

在你的FragmentStatePagerAdapter里面:

  private View mCurrentView; @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { Fragment f = (Fragment) object; mCurrentView = f.getView(); } 

如果仔细检查,ViewPager最多只能保存3个视图。 您可以轻松地获取当前视图

 view = MyActivity.mViewPager.getChildAt(1); 

我错过了什么吗? ViewGroup里面只有3个孩子,所以可以归结为:

 int current = viewPager.getCurrentItem(); int childCount = viewPager.getChildCount(); // If there's a single page or we're at the beginning, return the first view if (childCount == 1 || current == 0) { return viewPager.getChildAt(0); } else { //For any other case, we want the second child. This is either the last page or the page in the middle for any other case. return viewPager.getChildAt(1); }