android - Differentiating between user scroll and programatic page change in ViewPager -
i have android.support.v4.view.viewpager
in application , differentiate between programmatically-initiated smooth scroll , user-initiated touch scroll.
i've had @ viewpager.onpagechangelistener
, believe answer may lie in there i'm not sure how.
ok, turns out right answer lying in viewpager.onpagechangelistener
. in particular lies in using onpagescrollstatechanged(int state)
. there 3 states page in viewpager
can in:
- dragging: indicates pager being dragged user.
- idle: indicates pager in idle, settled state.
- settling: indicates pager in process of settling final position.
so dragging state occurs when current page being physically dragged user. when user has swiped page states occur in following order: dragging -> settling -> idle. now, onpageselected(int position)
method called between "settling" , "idle" states. thus, in order determine whether or not page change caused user scroll 1 needs check previous state "dragging" , current state "settling". can keep boolean
variable track whether or not page change user initiated or not , check in onpageselected(int position)
method.
here onpagescrollstatechanged
method
public void onpagescrollstatechanged(int state) { if (previousstate == viewpager.scroll_state_dragging && state == viewpager.scroll_state_settling) userscrollchange = true; else if (previousstate == viewpager.scroll_state_settling && state == viewpager.scroll_state_idle) userscrollchange = false; previousstate = state; }
the if
, else if
statements need not explicit did clarity.
Comments
Post a Comment