onTouchEvent在DoubleViewPager中不起作用

时间:2016-09-03 16:41:35

标签: android android-fragments android-viewpager ontouchevent

我坚持我的项目,我想请求帮助

我的Activity中有一个包含3个片段的Horizo​​ntal ViewPager,第二个片段中有一个Vertical。它在中间迷茫。如果是向左或向右滑动,我想将我的事件传递给父级为Horizo​​ntal,如果向下或向上滑动则将其处理。 我在onTouchEvent(MotionEvent ev)中做错了什么?

public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mStartDragX = ev.getX();
            mStartDragY = ev.getY();
            Log.d("action_down", "down : x=" + mStartDragX + "y=" + mStartDragY);

        case MotionEvent.ACTION_MOVE:
            Log.d("values", "ev.getX()" + ev.getX() + "after: " + (ev.getX() - mStartDragX));
            Log.d("values", "ev.getY()" + ev.getY() + "after: " + (ev.getY() - mStartDragY));

            if (Math.abs(ev.getY() - mStartDragY) > Math.abs(ev.getX() - mStartDragX)) {
                Log.d("swiper: ", "Down or Up");
                mParent.requestDisallowInterceptTouchEvent(false);
                return super.onTouchEvent(swapXY(ev));
            } else {
                Log.d("swiper: ", "Left or Right");
                mParent.requestDisallowInterceptTouchEvent(true);
                mParent.onTouchEvent(ev);
            }

            break;
        case MotionEvent.ACTION_UP:
            Log.d("action_up", "up");
            mStartDragX = -1;
            mStartDragY = -1;
            break;
    }

    return false;

}

1 个答案:

答案 0 :(得分:0)

您尝试在Android库中使用Vertical-ViewPager-Horizo​​ntal-ViewPager-Android

头等舱

公共类ContentFragment扩展了片段{

public HorizontalViewPager viewPager;
public HorizontalViewPagerAdapter horizontalViewPagerAdapter;
public String parentInd;

public ContentFragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_content, container, false);

    parentInd=getArguments().getString("parent");

    viewPager = (HorizontalViewPager) view.findViewById(R.id.vpHorizontal);
    horizontalViewPagerAdapter = new HorizontalViewPagerAdapter(getChildFragmentManager());
    horizontalViewPagerAdapter.setParentID(parentInd);
    viewPager.setAdapter(horizontalViewPagerAdapter);
    Helper.log("Card Created : " + parentInd);
    viewPager.setCurrentItem(1);
    return view;
}

}

第二堂课

公共类ViewPagerAdapter扩展FragmentStatePagerAdapter {     公共ViewPagerAdapter(FragmentManager fm){         超级(fm);     }

@Override
public int getCount() {
    return 10;
}

@Override
public Fragment getItem(int position) {
    ContentFragment contentFragment = new ContentFragment();
    Bundle bundle = new Bundle();
    bundle.putString("parent",String.valueOf(position));
    contentFragment.setArguments(bundle);
    return contentFragment;
}

第三堂课

公共类VerticalViewPager扩展了ViewPager {

public VerticalViewPager(Context context) {
    super(context);
    init();
}

public void init(){
    setPageTransformer(true,new VerticalViewPagerTransform());
    setOverScrollMode(OVER_SCROLL_NEVER);
}

public VerticalViewPager(Context context, AttributeSet attributeSet){
    super(context,attributeSet);
    init();
}

private MotionEvent swapXY(MotionEvent event) {
    float x = getWidth();
    float y = getHeight();

    float newX = (event.getY()/y)*y;
    float newY = (event.getX()/x)*x;

    event.setLocation(newX,newY);
    return event;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean intercept = super.onInterceptTouchEvent(swapXY(ev));
    swapXY(ev);
    return intercept;
}


@Override
public boolean onTouchEvent(MotionEvent ev) {
    return super.onTouchEvent(swapXY(ev));
}

private class VerticalViewPagerTransform implements ViewPager.PageTransformer{

    private static final float Min_Scale = 0.65f;
    @Override
    public void transformPage(View page, float position) {

        if(position < -1) {
            page.setAlpha(0);
        }else if(position <= 0){
            page.setAlpha(1);
            page.setTranslationX(page.getWidth() * -position);
            page.setTranslationY(page.getHeight() * position);
            page.setScaleX(1);
            page.setScaleY(1);
        }else if(position <= 1){
            page.setAlpha(1-position);
            page.setTranslationX(page.getWidth()* -position);
            page.setTranslationY(0);
            float scaleFactor = Min_Scale + (1 - Min_Scale) * (1 - Math.abs(position));
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
        }else if(position > 1){
            page.setAlpha(0);
        }
    }
}

}

https://github.com/mohitsvnit/Vertical-ViewPager-Horizontal-ViewPager-Android/tree/master/app