也是在其他帖子看到的,记录下来。感谢万能的网络。
第一步 自定义view
/**
* 竖向的ViewPager
* Created by zh on 2018/4/27.
*/
public class VerticalViewPager extends ViewPager{
private boolean isVertical = false;
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
initIsVertical(attrs, 0);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
initIsVertical(attrs, defStyle);
init();
}
private void initIsVertical(AttributeSet attrs, int defStyle){
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyViewPager, defStyle, 0);
isVertical = a.getBoolean(R.styleable.MyViewPager_isVertical, false);
System.out.println("isVertical=>" + isVertical);
a.recycle();
}
private void init() {
if(isVertical){
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;
ev.setLocation(newX, newY);
return ev;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
if (isVertical) {
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}else {
return super.onInterceptTouchEvent(ev);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isVertical) {
return super.onTouchEvent(swapXY(ev));
}else {
return super.onTouchEvent(ev);
}
}
}
第二步 在attr.xml中
使用:
其他和正常的viewpager一样了
再加一点,当滑动到最后一页后再向后滑动自动关闭功能
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// arg0 确定到那页在改变 执行一次
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
switch (arg0) {
case ViewPager.SCROLL_STATE_DRAGGING:
misScrolled = false;
break;
case ViewPager.SCROLL_STATE_SETTLING:
misScrolled = true;
break;
case ViewPager.SCROLL_STATE_IDLE:
if (vp.getCurrentItem() == vp.getAdapter().getCount() - 1 && !misScrolled) {
// 已经是最后一页
finish();
}
if (vp.getCurrentItem() == 0 && !misScrolled) {
// 已经是第一页"
}
misScrolled = true;
break;
}
}
});