Repo: https://github.com/Trinea/android-auto-scroll-view-pager
I have been trying to port it into c# but no successful
using System;
using Java.Lang.Ref;
using Java.Lang.Reflect;
using Android.Content;
using Android.OS;
using Android.Support.V4.View;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Android.Runtime;
namespace HoneyBany {
public class AutoScrollViewPager: ViewPager {
public static readonly int DEFAULT_INTERVAL = 1500;
public static readonly int LEFT = 0;
public static readonly int RIGHT = 1;
public static readonly int SLIDE_BORDER_MODE_NONE = 0;
public static readonly int SLIDE_BORDER_MODE_CYCLE = 1;
public static readonly int SLIDE_BORDER_MODE_TO_PARENT = 2;
private long interval = DEFAULT_INTERVAL;
private int direction = RIGHT;
private bool isCycle = true;
private bool stopScrollWhenTouch = true;
private int slideBorderMode = SLIDE_BORDER_MODE_NONE;
private bool isBorderAnimation = true;
private double autoScrollFactor = 1.0;
private double swipeScrollFactor = 1.0;
private Handler handler;
private bool isAutoScroll = false;
private bool isStopByTouch = false;
private float touchX = 0f, downX = 0f;
private CustomDurationScroller scroller = null;
public static readonly int SCROLL_WHAT = 0;
private Context context;
public AutoScrollViewPager(Context context): base(context) {
this.context = context;
init();
}
public AutoScrollViewPager(Context paramContext, IAttributeSet paramAttributeSet): base(paramContext, paramAttributeSet) {
init();
}
public void init() {
handler = new ScrollViewHandler(this);
setViewPagerScroller();
}
public void startAutoScroll() {
isAutoScroll = true;
sendScrollMessage((long)(interval + scroller.Duration / autoScrollFactor * swipeScrollFactor));
}
public void startAutoScroll(int delayTimeInMills) {
isAutoScroll = true;
sendScrollMessage(delayTimeInMills);
}
public void stopAutoScroll() {
isAutoScroll = false;
handler.RemoveMessages(SCROLL_WHAT);
}
public void setSwipeScrollDurationFactor(double scrollFactor) {
swipeScrollFactor = scrollFactor;
}
private void sendScrollMessage(long delayTimeInMills) {
handler.RemoveMessages(SCROLL_WHAT);
handler.SendEmptyMessageDelayed(SCROLL_WHAT, delayTimeInMills);
}
public void setAutoScrollDurationFactor(double scrollFactor) {
autoScrollFactor = scrollFactor;
}
private void setViewPagerScroller() {
try {
IntPtr ViewPagerClass = JNIEnv.FindClass ("android/support/v4/view/ViewPager");
IntPtr mScrollerProperty = JNIEnv.GetFieldID (ViewPagerClass, "mScroller", "Landroid/widget/Scroller;");
scroller = new CustomDurationScroller(this.context);
JNIEnv.SetField (this.Handle, mScrollerProperty, scroller.Handle);
} catch (Exception e) {
//e.printStackTrace();
}
}
public void scrollOnce() {
PagerAdapter adapter = Adapter;
int currentItem = CurrentItem;
int totalCount;
if (adapter == null || (totalCount = adapter.Count) <= 1) {
return;
}
int nextItem = (direction == LEFT) ? --currentItem : ++currentItem;
if (nextItem < 0) {
if (isCycle) {
SetCurrentItem(totalCount - 1, isBorderAnimation);
}
} else if (nextItem == totalCount) {
if (isCycle) {
SetCurrentItem(0, isBorderAnimation);
}
} else {
SetCurrentItem(nextItem, true);
}
}
public override bool DispatchTouchEvent(MotionEvent ev) {
int action = MotionEventCompat.GetActionMasked(ev);
if (stopScrollWhenTouch) {
if ((action == (int)MotionEventActions.Down) && isAutoScroll) {
isStopByTouch = true;
stopAutoScroll();
} else if (ev.Action == MotionEventActions.Up && isStopByTouch) {
startAutoScroll();
}
}
if (slideBorderMode == SLIDE_BORDER_MODE_TO_PARENT || slideBorderMode == SLIDE_BORDER_MODE_CYCLE) {
touchX = ev.GetX();
if (ev.Action == MotionEventActions.Down) {
downX = touchX;
}
int currentItem = CurrentItem;
PagerAdapter adapter = Adapter;
int pageCount = adapter == null ? 0 : adapter.Count;
if ((currentItem == 0 && downX <= touchX) || (currentItem == pageCount - 1 && downX >= touchX)) {
if (slideBorderMode == SLIDE_BORDER_MODE_TO_PARENT) {
Parent.RequestDisallowInterceptTouchEvent(false);
} else {
if (pageCount > 1) {
SetCurrentItem(pageCount - currentItem - 1, isBorderAnimation);
}
Parent.RequestDisallowInterceptTouchEvent(true);
}
return base.DispatchTouchEvent(ev);
}
}
Parent.RequestDisallowInterceptTouchEvent(true);
return base.DispatchTouchEvent(ev);
}
public long getInterval() {
return interval;
}
public void setInterval(long interval) {
this.interval = interval;
}
public int getDirection() {
return (direction == LEFT) ? LEFT : RIGHT;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void setCycle(bool isCycle) {
this.isCycle = isCycle;
}
public bool isStopScrollWhenTouch() {
return stopScrollWhenTouch;
}
public void setStopScrollWhenTouch(bool stopScrollWhenTouch) {
this.stopScrollWhenTouch = stopScrollWhenTouch;
}
public int getSlideBorderMode() {
return slideBorderMode;
}
public void setSlideBorderMode(int slideBorderMode) {
this.slideBorderMode = slideBorderMode;
}
public void setBorderAnimation(bool isBorderAnimation) {
this.isBorderAnimation = isBorderAnimation;
}
public class ScrollViewHandler: Handler {
private AutoScrollViewPager autoScrollViewPager;
public ScrollViewHandler(AutoScrollViewPager asvp) {
this.autoScrollViewPager = asvp;
}
public override void HandleMessage(Message msg) {
base.HandleMessage(msg);
if (msg.What == SCROLL_WHAT) {
AutoScrollViewPager pager = autoScrollViewPager;
if (pager != null) {
pager.scroller.setScrollDurationFactor(pager.autoScrollFactor);
pager.scrollOnce();
pager.scroller.setScrollDurationFactor(pager.swipeScrollFactor);
pager.sendScrollMessage(pager.interval + pager.scroller.Duration);
}
}
}
}
}
public class CustomDurationScroller: Scroller {
private double scrollFactor = 1;
public CustomDurationScroller(Context context): base(context) {
}
public CustomDurationScroller(Context context, Android.Views.Animations.IInterpolator interpolator): base(context, interpolator) {
}
public void setScrollDurationFactor(double scrollFactor) {
this.scrollFactor = scrollFactor;
}
public override void StartScroll(int startX, int startY, int dx, int dy, int duration) {
base.StartScroll(startX, startY, dx, dy, (int)(duration * scrollFactor));
}
public override void StartScroll (int startX, int startY, int dx, int dy)
{
base.StartScroll (startX, startY, dx, dy);
}
}
}
Facing problem in this code:
private void setViewPagerScroller() {
try {
IntPtr ViewPagerClass = JNIEnv.FindClass ("android/support/v4/view/ViewPager");
IntPtr mScrollerProperty = JNIEnv.GetFieldID (ViewPagerClass, "mScroller", "Landroid/widget/Scroller;");
scroller = new CustomDurationScroller(this.context);
JNIEnv.SetField (this.Handle, mScrollerProperty, scroller.Handle);
} catch (Exception e) {
//e.printStackTrace();
}
}