I am writing application that uses ViewPager
to host Fragment
s.
When I change fragment programmatically the smooth scroll function does not work. I use ViewPager.setCurrentItem(int item, boolean smoothScroll)` method.
Maybe anyone know a workaround this bug? Maybe with animations?
EDIT:
I am using support package. And the issue is that whether I use ViewPager.setCurrentItem(2, true)
or ViewPager.setCurrentItem(2, false)
the result is the same. The view switches really fast (not smoothly).
I've fixed this by creating a MyViewPager that overrides the ViewPager.mScroller using reflection.
public class MyViewPager extends ViewPager
{
public MyViewPager( Context context, AttributeSet attrs)
{
super( context, attrs );
setMyScroller();
}
private void setMyScroller()
{
try
{
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e)
{
e.printStackTrace();
}
}
public class MyScroller extends Scroller
{
public MyScroller(Context context)
{
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration)
{
super.startScroll(startX, startY, dx, dy, 1000 /*1 secs*/);
}
}
}
What worked for me was to call mPager.setCurrentItem after the mPagerAdapter.notifyDataSetChanged(), and not as a posted runnable.
Here is an example (in MonoDroid):
public void UpdateDetialsView(string type)
{
string typeName4 = Java.Lang.Class.FromType(typeof(ParcelRecordComplaintDetailsView)).Name;
if (_fragments.Count > 3)
{
_fragments.RemoveAt(_fragments.Count - 1);
////////////////////////////////////////////////////////////
// Called here it wasn't working for me (Posted or not)
//_viewPager.Post(new Action(() => { _viewPager.SetCurrentItem(3, true); }));
}
_fragments.Add(Fragment.Instantiate(Activity, typeName4));
_pagerAdapter.NotifyDataSetChanged();
////////////////////////////////////////////////////////////
// Called here it works as expected
_viewPager.SetCurrentItem(3, true);
}
where
protected MyAdapter _pagerAdapter;
protected ViewPager _viewPager;
private List<Android.App.Fragment> _fragments;
and
public class MyAdapter : Android.Support.V13.App.FragmentStatePagerAdapter
{
private List<Fragment> _fragments;
public override Java.Lang.Object InstantiateItem(View p0, int p1)
{
return base.InstantiateItem(p0, p1);
}
public MyAdapter(Android.App.FragmentManager fm) : base(fm)
{
}
public MyAdapter(Android.App.FragmentManager fm, List<Android.App.Fragment> fragments) : base(fm)
{
_fragments = fragments;
}
public override int Count
{
//get { return FRAG_PAGES; }
get { return _fragments.Count; }
}
public override Android.App.Fragment GetItem(int p0)
{
return _fragments[p0];
}
public override float GetPageWidth(int p0)
{
//return base.GetPageWidth(p0);
return (float) (0.5f);
}
public override int GetItemPosition(Java.Lang.Object p0)
{
//return base.GetItemPosition(p0);
return PagerAdapter.PositionNone;
}
}