可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a ViewPager, and I'd like to get the current selected and visible view, not a position.
getChildAt(getCurrentItem)
returns wrong View
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
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);
回答1:
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.
回答2:
You can get the current element by accessing your list of itens from your adapter calling myAdapter.yourListItens.get(myViewPager.getCurrentItem());
As you can see, ViewPager can retrieve the current index of element of you adapter (current page).
If you is using FragmentPagerAdapter you can do this cast:
FragmentPagerAdapter adapter = (FragmentPagerAdapter)myViewPager.getAdapter();
and call
adapter.getItem(myViewPager.getCurrentItem());
This works very well for me ;)
回答3:
I just came across the same issue and resolved it by using:
View view = MyActivity.mViewPager.getFocusedChild();
回答4:
how does this play out for all of y'all?
public Object instantiateItem(final ViewGroup container, final int position) { CustomHomeView RL = new CustomHomeView(MainActivity.ctxt); if (position==0){ container.setId(R.id.home_container);} ...rest of code
then here is code to access your page
((ViewGroup)pager.findViewById(R.id.home_container)).getChildAt(pager.getCurrentItem()).setBackgroundColor(Color.BLUE);
If you want you can set up a little meathod
RelativeLayout getPageAt(int index){ RelativeLayout rl = ((RelativeLayout)((ViewGroup)pager.findViewById(R.id.home_container)).getChildAt(index)); return rl; }
回答5:
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.
回答6:
Use an Adapter extending PagerAdapter, and override setPrimaryItem method inside your PagerAdapter.
https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
class yourPagerAdapter extends PagerAdapter { // ....... @Override public void setPrimaryItem (ViewGroup container, int position, Object object) { int currentItemOnScreenPosition = position; } // ....... }
回答7:
is that your first activity on the screen or have you layered some above each other already?
try this:
findViewById(android.R.id.content).getRootView()
or just:
findViewById(android.R.id.content)
also depending on what you want try:
((ViewGroup)findViewById(android.R.id.content)).getChildAt(0)
回答8:
Try this
final int position = mViewPager.getCurrentItem(); Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.rewards_viewpager + ":" + position);
回答9:
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; } }
回答10:
Please check this
((ViewGroup))mViewPager.getChildAt(MyActivity.mViewPager.getCurrentItem()));
else verify this link.
回答11:
If you examine carefully, there are at most 3 views saved by ViewPager. You can easily get the current view by
view = MyActivity.mViewPager.getChildAt(1);