Android ViewPager get the current View
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.
Getting current view from ViewPager
You can try something like this -
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater;
if(position != 10){
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
itemView.setTag("View"+position);
} else {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.results, container,
false);
}
container.addView(itemView);
return itemView ;
}
And when you need the current view-
View myView = pager.findViewWithTag("View"+pager.getCurrentItem());
Getting the current Fragment instance in the viewpager
by selecting an option, I need to update the fragment that is
currently visible.
A simple way of doing this is using a trick related to the FragmentPagerAdapter
implementation:
case R.id.addText:
Fragment page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + ViewPager.getCurrentItem());
// based on the current position you can then cast the page to the correct
// class and call the method:
if (ViewPager.getCurrentItem() == 0 && page != null) {
((FragmentClass1)page).updateList("new item");
}
return true;
Please rethink your variable naming convention, using as the variable name the name of the class is very confusing(so no ViewPager ViewPager
, use ViewPager mPager
for example).
How do you get the current page number of a ViewPager for Android?
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
Get Current Position of ViewPager
You shoul set text onPageSelected.
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
System.out.println("Current position=="+num);
textView.setText((arg0+1)+" of "+ images.size());
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
System.out.println("onPageScrolled");
}
@Override
public void onPageScrollStateChanged(int num) {
// TODO Auto-generated method stub
}
});
Getting the current position of a ViewPager
Create a listener and set it on your viewpager:
/**
* Get the current view position from the ViewPager by
* extending SimpleOnPageChangeListener class and adding your method
*/
public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
private int currentPage;
@Override
public void onPageSelected(int position) {
currentPage = position;
}
public final int getCurrentPage() {
return currentPage;
}
}
ViewPager2, get current page viewholder
You can do something like this
(viewPager2.get(0) as RecyclerView).findViewHolderForAdapterPosition(position)
or
(viewPager2.get(0) as RecyclerView).layoutManager.findViewByPosition(position)
Related Topics
Error Inflating Class Androidx.Constraintlayout.Constraintlayout After Migration to Androidx
Android Sdk Cannot Be Found by Flutter
Equivalent of Clean & Build in Android Studio
Find Out the Running Process Id by Package Name
Custom Converter for Retrofit 2
Detect When Android V2 Maps Has Loaded
How to Correctly Start Activity from Postexecute in Android
Open a PDF File Inside a Webview
Java.Lang.Numberformatexception: Invalid Int: "" in Android
Runonuithread VS Looper.Getmainlooper().Post in Android
Android E/Parcel﹕ Class Not Found When Unmarshalling (Only on Samsung Tab3)
How to Set Spinner Default Value to Null
How to Wrap Lengthy Text in a Spinner
Checking a Checkbox in Listview Makes Other Random Checkboxes Checked Too