ViewPager2 里面有水平滚动视图 [英] ViewPager2 with horizontal scrollView inside
问题描述
我为我的项目实现了新的 ViewPager.viewPager2 包含一个片段列表
I implemented the new ViewPager for my project. The viewPager2 contains a list of fragment
private class ViewPagerAdapter extends FragmentStateAdapter {
private ArrayList<Integer> classifiedIds;
ViewPagerAdapter(@NonNull Fragment fragment, final ArrayList<Integer> classifiedIds) {
super(fragment);
this.classifiedIds = classifiedIds;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return DetailsFragment.newInstance(classifiedIds.get(position));
}
@Override
public int getItemCount() {
return classifiedIds.size();
}
}
在片段内部我得到了一个水平的 recyclerView
Inside the fragment I got an horizontal recyclerView
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerViewPicture.setLayoutManager(layoutManager);
问题是当我尝试滚动 recyclerview 时,viewPager 触摸并切换到下一个片段
The issue is when I try to scroll the recyclerview the viewPager take the touch and swap to the next fragment
当我使用旧的 ViewPager 时,我没有这个问题
When I was using the old ViewPager I didn't have this issue
推荐答案
我找到了一个解决方案,这是一个已知错误,您可以在这里看到 https://issuetracker.google.com/issues/123006042 也许他们会在下一次更新中解决这个问题
I find a solution it's a know bug as you can see here https://issuetracker.google.com/issues/123006042 maybe they would solve it in the next updates
感谢 TakeInfos 和链接中的示例项目
Thanks to TakeInfos and the exemple project inside the link
recyclerViewPicture.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
int lastX = 0;
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) e.getX();
break;
case MotionEvent.ACTION_MOVE:
boolean isScrollingRight = e.getX() < lastX;
if ((isScrollingRight && ((LinearLayoutManager) recyclerViewPicture.getLayoutManager()).findLastCompletelyVisibleItemPosition() == recyclerViewPicture.getAdapter().getItemCount() - 1) ||
(!isScrollingRight && ((LinearLayoutManager) recyclerViewPicture.getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0)) {
viewPager.setUserInputEnabled(true);
} else {
viewPager.setUserInputEnabled(false);
}
break;
case MotionEvent.ACTION_UP:
lastX = 0;
viewPager.setUserInputEnabled(true);
break;
}
return false;
}
@Override
public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
我正在检查用户是向右还是向左滚动.如果用户到达 recyclerView 的结尾或开头,我将启用或禁用视图寻呼机上的滑动
I'm checking if the user scroll on the right or on the left. If the user reach the end or the start of the recyclerView I'm enable or disable the swipe on the view pager
这篇关于ViewPager2 里面有水平滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!