Android ViewPager setCurrentItem not working after onResume
i can't really answer WHY exactly this happens, but if you delay the setCurrentItem call for a few milliseconds it should work. My guess is that because during onResume
there hasn't been a rendering pass yet, and the ViewPager needs one or something like that.
private ViewPager viewPager;
@Override
public void onResume() {
final int pos = 3;
viewPager.postDelayed(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(pos);
}
}, 100);
}
UPDATE: story time
so today i had the problem that the viewpager ignored my setCurrentItem action, and i searched stackoverflow for a solution. i found someone with the same problem and a fix; i implemented the fix and it didn't work. whoa! back to stackoverflow to downvote that faux-fix-provider, and ...
it was me. i implemented my own faulty non-fix, which i came up with the first time i stumbled over the problem (and which was later forgotten). i'll now have to downvote myself for providing bad information.
the reason my initial "fix" worked was not because of of a "rendering pass"; the problem was that the pager's content was controlled by a spinner. both the spinners and the pagers state were restored onResume, and because of this the spinners onItemSelected listener was called during the next event propagation cycle, which did repopulate the viewpager - this time using a different default value.
removing and resetting the listener during the initial state restoration fixed the issue.
the fix above kind-of worked the first time, because it set the pagers current position after the onItemSelected event fired. later, it ceased to work for some reason (probably the app became too slow - in my implementation i didn't use 100ms, but 10ms). i then removed the postDelayed in a cleanup cycle, because it didn't change the already faulty behaviour.
update 2: i can't downvote my own post. i assume, honorable seppuku is the only option left.
ViewPager2 default position
setCurrentItem(int item, boolean smoothScroll)
works correctly in ViewPager
but in ViewPager2
it does not work as expected. Finally, I faced this problem by adding setCurrentItem(int item, boolean smoothScroll)
method into a delay like this:
Handler().postDelayed({
view.viewPager.setCurrentItem(startPosition, false)
}, 100)
Viewpager setCurrentItem() method don't work normally after activity jump in don't keep activity develop option
Try this code:
@Override
void onStart(Bundle savedInstanceBundle){
if (savedInstanceBundle != null){
viewPager.setCurrentItem(savedInstanceBundle.getInt(VIEWPAGER_PAGE);
}
}
@Override
void onSavedInstanceState(Bundle outState){
// Put your current ViewPager Page here;
outState.putInt(VIEWPAGER_PAGE, page));
}
viewPager.setCurrentItem not working inside adapter
Considering you have your view pager in your Fragment.
Your ViewPager is not initialized and the way you have initialized your Viewpager is incorrect.
You could use an interface to do this.
Create an interface
public interface ViewPagerListener{
void setViewPager(int position);
}
Implement the interface in your Fragment
public class MyFragment extends Fragment implements ViewPagerListener{
//method to be implemented.
public void setViewPager(int position){
//TODO: do your implementation here
}
}
Add a ViewPagerListener
param in your adapter in one of the constructor
public class MyAdapter extends ArrayAdapter<Obj>{
Context c;
ArrayList<Obj> objects
ViewPagerListener mListener;
public MyAdapter(Context c,ArrayList<Obj> objects,ViewPagerListener mListener){
this.c = c;
this.objects = objects;
this.mListener = mListener;
}
}
In your Fragment, initialize your Adapter like this
MyAdapter myAdapter = new MyAdapter(getActivity(),objects,MyFragment.this); //3rd param is your interface instance.
Call the interface method in your adapter onBindViewHolder
method
public void onBindViewHolder(ViewHolder holder, int position) {
Upload upload = (Upload) this.uploads.get(position);
holder.make.setText(upload.getName());
Glide.with(this.context).load(upload.getUrl()).into(holder.imageView);
holder.year.setText(upload.getYr());
holder.mView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mListener.setViewPager(3) // Calling the interface method
}
});
}
You can set the ViewPager position in your fragment like this
public void setViewPager(int position){
viewPager.setCurrentItem(position);
}
Related Topics
How to Remove a Contact Programmatically in Android
Signal 11 Sigsegv Crash Android
What Is the Alternative to Android Orientation Sensor
Android: How to Monitor Wifi Signal Strength
Android Make Animated Video from List of Images
Onclicklistener - X,Y Location of Event
Android Replace the Current Fragment with Another Fragment
How to Check Internal and External Storage If Exist
Open Android App from Url Using Intent-Filter Not Working
Error Type 3: Activity Class {...} Does Not Exist
How to Take Multiple Photos Before Dismissing Camera Intent
Save State of Activity When Orientation Changes Android
Volley Out of Memory Error, Weird Allocation Attempt
How to Save the Image to Sd Card on Button Click Android
How to Get Current Buildtype in Android Gradle Configuration