我使用ViewPager和滑动选项 如何检测滑动开始(视图开始之间转换的时刻)和滑动结束(视图停止时的转换时刻)?
答案 0 :(得分:2)
您可以创建自己的自定义视图寻呼机并覆盖onDrag或onTouch事件方法。
public class MyViewPager extends ViewPager{
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onDragEvent(DragEvent event) {
return super.onDragEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
}
答案 1 :(得分:1)
您可以使用OnPageChangeListener
。
方法onPageScrolled (int position, float positionOffset, int positionOffsetPixels)
将通知您页面更改的进度(以像素为单位,或者作为0到1之间的浮点数),因此很容易看到它何时开始和停止。
继承人的文件:
http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageScrolled%28int,%20float,%20int%29
答案 2 :(得分:1)
您可以实现ViewPager类的OnPageListener接口。该界面提供了三种方法来监听ViewPager的更改。
@Override
public void onPageScrollStateChanged(int state) {
//Called when the scroll state changes.
// The state variable that you receive as parameter will help you know the state of the view pager.
// The ViewPager can be in any of the three states,
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
// If the state is SCROLL_STATE_DRAGGING, it signals that the page is being dragged. A transition to this state can signal a start of drag.
// If the state is SCROLL_STATE_IDLE, it signals that there is no dragging going on and a possible end of any drags.
// Keep a variable that changes state as per your requirements.
}
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
//This method will be invoked when the current page is scrolled,
//either as part of a programmatically initiated smooth scroll
//or a user initiated touch scroll.
// The positionOffset parameted returns a float between [0,1) Any number between 0 and 1 indicates that the page is being dragged.
// This can be used as an indicator for drag start and end events.
}
@Override
public void onPageSelected(int position) {
//This method will be invoked when a new page becomes selected.
}};
一切顺利:)