我使用ViewPager
创建了一个垂直ViewPage.PageTransformer
并交换了X和Y坐标。 (我使用this approach)
现在,我想要它做的是停止在某一点滚动(我希望最后一个视图只占屏幕高度的65%,但是全宽)。
通常,在这种情况下我会覆盖getPageWidth()
,但由于我现在的宽度和高度有点混淆,当我这样做时,我的视图占据了屏幕高度和宽度的65%。
那我该如何解决这个问题?
谢谢!
MyViewPager.java
public class MyViewPager extends ViewPager {
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setPageTransformer(true, new VerticalPageTransformer());
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View view, float position) {
if (position < -1) {
view.setAlpha(0);
} else if (position <= 1) {
view.setAlpha(1);
view.setTranslationX(view.getWidth() * -position);
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else {
view.setAlpha(0);
}
}
}
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 onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev);
return intercepted;
}
}
MyPagerAdapter.java
public class MyPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
public MyPagerAdapter(Context context) {
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
if (position == 0) {
View view = layoutInflater.inflate(R.layout.item_profile_picture, container, false);
container.addView(view);
return view;
}
else {
View view = layoutInflater.inflate(R.layout.item_profile_info, container, false);
container.addView(view);
return view;
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((FrameLayout) object);
}
@Override
public float getPageWidth(int position) {
if (position == 1) return (0.65f);
return super.getPageWidth(position);
}
}
答案 0 :(得分:0)
据我所知,你试图在没有过度滚动的情况下停在某一点。将overscroll(never)方法添加到所需的viewpager。并管理对象的边距。尽可能地包装内容。
答案 1 :(得分:0)
使用this library解决了这个问题。