InfiniteCycleViewPage的点击事件

最近在项目中有使用到GitHub超过4K个star的一个关于ViewPager的无限滑动的开源库,该库的主要效果如下所示:


使用这个库之后UI效果是达到了,但是这Fragment没有点击事件,在Issues上面也有使用者询问了setOnClickListener事件,库主人也没有作出回答,网上搜索也没有相关资料,所以我用了一种非常规办法进行了点击事件,下面贴出代码希望帮忙以后使用此库的兄弟们,当然有更好的办法也欢迎指教。

public class VerticalPagerFragment extends Fragment {
	//用来判断是单击还是滑动
	private boolean isOnclick = true;
	private  VerticalPagerAdapter mAdapter;
	@Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_vertical, container, false);
    }

    @Override
    public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
		mAdapter = new VerticalPagerAdapter(getContext());
        final VerticalInfiniteCycleViewPager verticalInfiniteCycleViewPager =
                (VerticalInfiniteCycleViewPager) view.findViewById(R.id.vicvp);
        verticalInfiniteCycleViewPager.setAdapter(mAdapter);
        verticalInfiniteCycleViewPager.setOnTouchListener(new View.OnTouchListener() {
		    @Override
		    public boolean onTouch(View v, MotionEvent event) {
			    switch (event.getAction()){
				    case MotionEvent.ACTION_DOWN:
					    isOnclick  = true;
					    break ;
				    case MotionEvent.ACTION_MOVE:
				    	//滑动置为false
					    isOnclick  = false;
					    break ;
				    case  MotionEvent.ACTION_UP :
					    if (isOnclick) {
						    int item = verticalInfiniteCycleViewPager.getCurrentItem() % mAdapter.getCount();
						    Log.e("VerticalPagerFragment", "item:" + item);
						    if (item == 0) {

						    } else if (item == 1) {

						    } else if (item == 2) {

						    }else if (item == 3) {

						    }
					    }
					    break ;
			    }
			    return false;
		    }
	    });
    }
}

最后贴下原库GitHub地址:https://github.com/Devlight/InfiniteCycleViewPager   

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值