我使用ViewPager通过左/右滑动切换视图。
ViewPager需要一个适配器,所以我构建了这个:
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),但它不起作用。
是否有一些示例,或者您是否知道如何获取当前视图?
由于
答案 0 :(得分:86)
可以通过覆盖PagerAdapter.setPrimaryItem方法保存当前活动对象(View,Fragment,...)。例如:
private View mCurrentView;
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
mCurrentView = (View)object;
}
答案 1 :(得分:41)
您必须向ViewPager
注册听众:
pager.setOnPageChangeListener(new MyPageChangeListener());
您必须通过扩展存根侦听器来自定义侦听器:
private int focusedPage = 0;
private class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageSelected(int position) {
focusedPage = position;
}
}
我通过查看compatibility library中的ViewPager.java源代码找到了这个。我读到我们可以做更多,例如catch onPageScrollStateChanged
。
为了构建适配器,我使用了代码on this blog post。你可能想看看。
答案 2 :(得分:41)
您可以在instantiateItem方法中为创建的视图添加标记:
view.setTag(position);
稍后您可以通过以下方式访问当前选定的视图:
mPager.findViewWithTag(mPager.getCurrentItem());
答案 3 :(得分:4)
我最近需要实现这个确切的解决方案。这就是我做的方式:
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;
}
答案 4 :(得分:2)
试试这个:
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);
}
答案 5 :(得分:1)
为了获得专注的观点,我采用这种方式:
当我在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;
}
答案 6 :(得分:0)
给定的答案并不适合我,它们都有不必要的副作用。要么我必须添加不必要的变量(我喜欢干净的代码),要么我必须解决Android框架本身。
我的解决方案基于反射,它可以访问ViewPager中所有对象的数组列表,并返回ViewPager中当前选定的对象。它几乎没有副作用(反射很慢,现有类的子类化)并且它保持代码清洁。
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");
}
}
答案 7 :(得分:0)
在FragmentStatePagerAdapter
内:
private View mCurrentView;
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Fragment f = (Fragment) object;
mCurrentView = f.getView();
}
答案 8 :(得分:0)
您可以使用此代码
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
答案 9 :(得分:0)
您可以使用:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.i("maylonsales", "onPageSelected position " + position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
答案 10 :(得分:-1)
如果仔细检查,ViewPager最多可保存3个视图。 您可以通过
轻松获取当前视图view = MyActivity.mViewPager.getChildAt(1);
答案 11 :(得分:-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);
}