Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

ViewPager mScroller Reflection

$
0
0

Good day Xamarin!

I am try to get inner field of ViewPager to substitute mScroller with custom Scroller. End target - to override ViewPager.SetItem() animation duration. So I found folow solution under java language that I try to make on C#. Found solution: http://stackoverflow.com/questions/11962268/viewpager-setcurrentitempageid-true-does-not-smoothscroll

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*/);
    }
  }
}

And this code that I wrote:

       try{
            /*Class viewpager = ViewPager.Class;
            Field scroller = viewpager.GetDeclaredField("mScroller");
            scroller.Accessible = true;
            scroller.Set(this, new CustomViewPagerScroller(context));
            */

            Type viewpager = typeof(ViewPager);
            FieldInfo scroller = viewpager.GetField("mScroller");
            //var info = viewpager.GetRuntimeFields();
            FieldInfo[] flds = viewpager.GetFields();
            scroller.SetValue(this, new CustomViewPagerScroller(context));


        }catch(System.Exception e)
        {
            Console.WriteLine (e.Message);
        }

At line "FieldInfo scroller = viewpager.GetField("mScroller");" I always get scroller = null. So please help me what I do not right?

Thanks In Advance!


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>