Android ViewPager 获取当前View [英] Android ViewPager get the current View
问题描述
我有一个 ViewPager,我想获取当前选定的可见视图,而不是位置.
I have a ViewPager, and I'd like to get the current selected and visible view, not a position.
getChildAt(getCurrentItem)
返回错误的View
这并非一直有效.有时返回null,有时只是返回错误的View.
getChildAt(getCurrentItem)
returns wrongView
This works not all the time. Sometimes returns null, sometimes just returns wrong View.
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser == true) {
mFocusedListView = ListView;
}
}
PageListener on ViewPager
和 getChildAt()
也不起作用,每次都没有给我正确的视图.
PageListener on ViewPager
with getChildAt()
also not working, not giving me the correct View every time.
如何获取当前可见的视图?
How can i get current visible View?
View view = MyActivity.mViewPager.getChildAt(MyActivity.mViewPager.getCurrentItem()).getRootView();
ListView listview = (ListView) view.findViewById(R.id.ListViewItems);
推荐答案
我已经想通了.我所做的是调用带有名称的 setTag()
到所有 View
s/ListView
s,然后调用 findViewWithTag(mytag)
, mytag
是标签.
I've figured it out. What I did was to call setTag()
with a name to all View
s/ListView
s, and just call findViewWithTag(mytag)
, mytag
being the tag.
不幸的是,没有其他方法可以解决这个问题.
Unfortunately, there's no other way to solve this.
这篇关于Android ViewPager 获取当前View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!