最近在项目中有使用到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