我查看了你的
Github代码。我为你解决了问题,并发送了一个
pull request。问题在于,如果返回
true
,则
onInterceptTouchEvent
会使
ViewPager
滚动,如果返回
false
,则会禁用
ViewPager
触摸并使
NestedScrollView
滚动。所以我在这里做的是检查是否需要滚动
NestedScrollView
,如果
NestedScrollView
在顶部或底部,则返回
false
,否则返回
true
。我在这里发布代码。你可以修改它并使其更加简洁。
Github链接 -
https://github.com/karanatwal/vertical-viewpager-nestedscrollview
下面是
VerticalViewPager
代码 -
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean toIntercept = super.onInterceptTouchEvent(flipXY(ev));
float x = ev.getX();
flipXY(ev);
int direction=1;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartDragX = x;
break;
case MotionEvent.ACTION_MOVE:
if (mStartDragX < x ) {
direction=-1;
} else if (mStartDragX > x) {
direction=1;
}
MainActivity.MyPagerAdapter adapter = (MainActivity.MyPagerAdapter)this.getAdapter();
if (adapter.getCurrentFragment() instanceof FragmentViewPager){
FragmentViewPager fragmentViewPager = (FragmentViewPager)adapter.getCurrentFragment();
toIntercept = !fragmentViewPager.getNestedScrollView().canScrollVertically(direction);
int range = fragmentViewPager.getNestedScrollView().computeVerticalScrollRange();
int offset = fragmentViewPager.getNestedScrollView().computeVerticalScrollOffset();
int extent = fragmentViewPager.getNestedScrollView().computeVerticalScrollExtent();
int percentage = (int)(100.0 * offset / (float)(range - extent));
System.out.println(" range: "+range);
System.out.print(" offset: "+offset);
System.out.print(" extent: "+extent);
System.out.print(" percentage: "+percentage);
System.out.print(" direction: "+direction);
if (fragmentViewPager.getNestedScrollView().computeVerticalScrollOffset()==0 && direction==-1){
return true;
}
else if (percentage>99 && direction==1){
return true;
}else {
return false;
}
}
break;
}
return toIntercept;
}
您的MyPagerAdapter
-
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private Fragment mCurrentFragment;
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
if (getCurrentFragment() != object) {
mCurrentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
@Override
public android.support.v4.app.Fragment getItem(int pos) {
switch (pos) {
case 0:
return FragmentViewPager.newInstance("Title Start", R.drawable.ic_launcher_background);
case 1:
return FragmentViewPager.newInstance("Rock", R.drawable.rock);
case 2:
return FragmentViewPager.newInstance("Paper", R.drawable.paper);
case 3:
return FragmentViewPager.newInstance("Scissors", R.drawable.scissors);
case 4:
return FragmentViewPager.newInstance("Title End", R.drawable.ic_launcher_background);
default:
return FragmentViewPager.newInstance("Default Title", R.drawable.rock);
}
}
@Override
public int getCount() {
return 5;
}
}
为了避免使用受限的API方法- 使用以下逻辑作为替代方案
int scrollY = scrollView.getScrollY();
int scrollContentHeight = scrollView.getChildAt(0).getHeight();
int screenHeight = Utility.getScreenHeight(context);
int statusBarHeight = Utility.getStatusBarHeight(context);
double percent = ((((float) scrollY) / ((float) (scrollContentHeight - screenHeight + statusBarHeight))));
你所需要的只是滚动百分比和滑动方向。关于如何获取滚动百分比和滑动方向,Stack Overflow上有很多相关问题,例如
this和
this。你需要做一些研究。
NestedScrollView
,因此我不知道如何解决您的情况。我已发布了一份悬赏以查看是否可以得到答案。 - CommonsWare