我使用的是ViewPager的变体,它不是在水平方向上滚动,而是在垂直方向上滚动.
VerticalViewPager:
https://android.googlesource.com/platform/packages/apps/DeskClock/+/master/src/com/android/deskclock/VerticalViewPager.java
android.support.v4.app.FragmentStatePagerAdapter用于设置VerticalViewPager中的内容.
每次调用FragmentStatePagerAdapter的getItem()时,我都会从FragmentStatePagerAdapter创建并返回一个新的android.support.v4.app.Fragment实例,如下所示:
@Override public Fragment getItem(int position) { return PreviewFragment.getNewFragmentInstance(position); }
PreviewFragment呈现可能不适合屏幕的内容.
为了显示不适合屏幕的所有内容,android.support.v4.widget.NestedScrollView用作PreviewFragment布局的父元素.
问题:
PreviewFragment的内容按预期滚动到位置0.
但是,为了切换到下一页,必须多次执行甩动或滑动操作,因此向位置1的页面滑动是非常困难的.
此外,投掷或滑回到位置0非常困难或几乎不可能.
需要:
Fling或滑动或切换到PreviewFragment的下一个实例应该是平滑的.
换句话说,滚动内容应该是平滑的,同时请记住,滑动到下一页也应该顺利工作.
解决方法
Github Link – https://github.com/karanatwal/vertical-viewpager-nestedscrollview
以下是VerticalViewPager代码 –
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { boolean toIntercept = super.onInterceptTouchEvent(flipXY(ev)); // Return MotionEvent to normal 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){ //top return true; } else if (percentage>99 && direction==1){ //bottom return true; }else { //scroll nestedscrollview 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方法 – 使用以下逻辑作为替代
//offset,range and extent 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))));
您只需要滚动百分比和滑动方向.在SO上找到滚动百分比和滑动方向有很多问题,如this和this.你应该为它做一点R& D.