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);
The ViewPager and pager adapter just deal with data in memory. So when data in memory is updated, we just need to call the adapter's notifyDataSetChanged() . Since the fragment is already created, the adapter's onItemPosition() will be called before notifyDataSetChanged() returns.
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.
I just came across the same issue and resolved it by using:
View view = MyActivity.mViewPager.getFocusedChild();
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;
}
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 ;)
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:
null
if this view is currently not loaded or the corresponding view.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;
View onScreenView = getChildAt(position);
}
// .......
}
viewpager.getChildAt(0)
this always returns my currently selected view. this worked for me.
Try this
final int position = mViewPager.getCurrentItem();
Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.rewards_viewpager + ":"
+ position);
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;
}
}
I'm using ViewPagerUtils from FabulousFilter:
ViewPagerUtils.getCurrentView(ViewPager viewPager)
If you do not have many pages and you can safely apply setOffscreenPageLimit(N-1)
where N is the total number of pages without wasting too much memory then you could do the following:
public Object instantiateItem(final ViewGroup container, final int position) {
CustomHomeView RL = new CustomHomeView(context);
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 method for accessing a page
RelativeLayout getPageAt(int index){
RelativeLayout rl = ((RelativeLayout)((ViewGroup)pager.findViewById(R.id.home_container)).getChildAt(index));
return rl;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With