Backlight87
8/24/2017 - 7:16 PM

view page 垂直的view page

view page 垂直的view page

/**
 * Touch 事件交换一下,也就是将 MotionEvent 的 x 坐标转换成 y 坐标,将 y 坐标转换成 x 坐标。这样,从下往上滑就转换成了从右往左滑。
 * 一个继承ViewPager的垂直滑动的控件
 * Created by hsp on 2017/8/8.
 */
public class VerticalViewPager extends ViewPager {


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

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

    private MotionEvent swapTouchEvent(MotionEvent event) {
        float width = getWidth();
        float height = getHeight();
        float swappedX = (event.getY() / height) * width;
        float swappedY = (event.getX() / width) * height;
        event.setLocation(swappedX, swappedY);
        return event;
    }

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

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean handled = super.onTouchEvent(swapTouchEvent(ev));
        swapTouchEvent(ev);
        return handled;
    }
}





/**
 * 页面的垂直切换效果
 * 
 */
public class DefaultTransformer implements ViewPager.PageTransformer {

    @Override
    public void transformPage(View view, float position) {
        //切换不要透明度效果了,无网络的时候透明效果会看到加载更多样式
       /* float alpha = 0;
        if (0 <= position && position <= 1) {
            alpha = 1 - position;
        } else if (-1 < position && position < 0) {
            alpha = position + 1;
        }
        view.setAlpha(alpha);*/
        view.setTranslationX(view.getWidth() * -position);
        float yPosition = position * view.getHeight();
        view.setTranslationY(yPosition);
    }
}


/**
 * view page 的适配器
 * Created by hsp on 2017/8/8.
 */
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> data = new ArrayList<>();

    public ViewPagerFragmentAdapter(FragmentManager fm, List<Fragment> data) {
        super(fm);
        this.data = data;
    }

    @Override
    public Fragment getItem(int position) {//返回子View对象
        return data.get(position);
    }

    @Override
    public int getCount() {//返回子View的个数
        return data.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        //调用fragment的title文案设置在页面的title上
        return super.getPageTitle(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {//初始子View方法
        return super.instantiateItem(container, position);
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {//销毁子View
        super.destroyItem(container, position, object);
    }


    public static class Holder {
        private final List<Fragment> fragments = new ArrayList<>();
        private FragmentManager manager;

        public Holder(FragmentManager manager) {
            this.manager = manager;
        }

        public Holder add(Fragment f) {
            fragments.add(f);
            return this;
        }

        public ViewPagerFragmentAdapter set() {
            return new ViewPagerFragmentAdapter(manager, fragments);
        }
        public List<Fragment> getFragments() {
            return fragments;
        }
    }
}

//具体的使用
//实例化holder,填充完成后,设置给viewpage
ViewPagerFragmentAdapter.Holder holder = new ViewPagerFragmentAdapter.Holder(getSupportFragmentManager());

mLoadMoreViewPager.setPageTransformer(true, new DefaultTransformer());
//禁止出现滑动的时候阴影效果
mLoadMoreViewPager.setOverScrollMode(View.OVER_SCROLL_NEVER);
for (int i = 0; i < list.size(); i++) {
        // TODO: 2017/8/14 在这里得判断list是不是为空
        holder.add(MainFragmentTest.newInstance(list.get(i).getUrl(), list.get(i).getCover_pic(), list.get(i).getPic_size()));
}
mPagerAdapter = holder.set();
mLoadMoreViewPager.setAdapt(mPagerAdapter);

//如果要添加数据直接holder继续add就好了,然后通知mLoadMoreViewPager.notifyDataSetChanged();
//接着看你要跳到哪个页面mLoadMoreViewPager.setCurrentItem(currentItem + 1);