监听viewPager移动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float offset, int i1) { Log.i("zjc", "" + position % banners.size() + " " + offset); }
@Override public void onPageSelected(int i) { Log.i("zjc", "======================== " + i % banners.size() + " "); }
@Override public void onPageScrollStateChanged(int i) { } });
2021-08-17 09:17:12.185 17584-17584/com.qiyu.world I/zjc: ======================== select: 1 2021-08-17 09:17:12.185 17584-17584/com.qiyu.world I/zjc: 0 0.5955553 2021-08-17 09:17:12.219 17584-17584/com.qiyu.world I/zjc: 0 0.8666668 2021-08-17 09:17:12.223 17584-17584/com.qiyu.world I/zjc: 0 0.9333334 2021-08-17 09:17:12.240 17584-17584/com.qiyu.world I/zjc: 0 0.96888924 2021-08-17 09:17:12.257 17584-17584/com.qiyu.world I/zjc: 0 0.9866667 2021-08-17 09:17:12.273 17584-17584/com.qiyu.world I/zjc: 0 0.9955559 2021-08-17 09:17:12.290 17584-17584/com.qiyu.world I/zjc: 1 0.0 2021-08-17 09:17:15.140 17584-17584/com.qiyu.world I/zjc: ======================== select: 2 2021-08-17 09:17:15.147 17584-17584/com.qiyu.world I/zjc: 1 0.09777737 2021-08-17 09:17:15.161 17584-17584/com.qiyu.world I/zjc: 1 0.40888882 2021-08-17 09:17:15.177 17584-17584/com.qiyu.world I/zjc: 1 0.64000034 2021-08-17 09:17:15.194 17584-17584/com.qiyu.world I/zjc: 1 0.78666687 2021-08-17 09:17:15.211 17584-17584/com.qiyu.world I/zjc: 1 0.88444424 2021-08-17 09:17:15.227 17584-17584/com.qiyu.world I/zjc: 1 0.9422226 2021-08-17 09:17:15.244 17584-17584/com.qiyu.world I/zjc: 1 0.97333336 2021-08-17 09:17:15.260 17584-17584/com.qiyu.world I/zjc: 1 0.9911108 2021-08-17 09:17:15.277 17584-17584/com.qiyu.world I/zjc: 1 0.9955559 2021-08-17 09:17:15.294 17584-17584/com.qiyu.world I/zjc: 2 0.0 2021-08-17 09:17:18.142 17584-17584/com.qiyu.world I/zjc: ======================== select: 3 2021-08-17 09:17:18.152 17584-17584/com.qiyu.world I/zjc: 2 0.1422224 2021-08-17 09:17:18.164 17584-17584/com.qiyu.world I/zjc: 2 0.43999958 2021-08-17 09:17:18.181 17584-17584/com.qiyu.world I/zjc: 2 0.6622219 2021-08-17 09:17:18.198 17584-17584/com.qiyu.world I/zjc: 2 0.8044443 2021-08-17 09:17:18.214 17584-17584/com.qiyu.world I/zjc: 2 0.89333344 2021-08-17 09:17:18.231 17584-17584/com.qiyu.world I/zjc: 2 0.9466667 2021-08-17 09:17:18.247 17584-17584/com.qiyu.world I/zjc: 2 0.9777775 2021-08-17 09:17:18.263 17584-17584/com.qiyu.world I/zjc: 2 0.9911108 2021-08-17 09:17:18.280 17584-17584/com.qiyu.world I/zjc: 2 0.9955559 2021-08-17 09:17:18.297 17584-17584/com.qiyu.world I/zjc: 3 0.0 2021-08-17 09:17:21.140 17584-17584/com.qiyu.world I/zjc: ======================== select: 0 2021-08-17 09:17:21.154 17584-17584/com.qiyu.world I/zjc: 3 0.2666664 2021-08-17 09:17:21.168 17584-17584/com.qiyu.world I/zjc: 3 0.542222 2021-08-17 09:17:21.185 17584-17584/com.qiyu.world I/zjc: 3 0.72000027 2021-08-17 09:17:21.202 17584-17584/com.qiyu.world I/zjc: 3 0.8444443 2021-08-17 09:17:21.218 17584-17584/com.qiyu.world I/zjc: 3 0.91555595 2021-08-17 09:17:21.235 17584-17584/com.qiyu.world I/zjc: 3 0.96000004 2021-08-17 09:17:21.251 17584-17584/com.qiyu.world I/zjc: 3 0.98222256 2021-08-17 09:17:21.268 17584-17584/com.qiyu.world I/zjc: 3 0.9955559 2021-08-17 09:17:21.285 17584-17584/com.qiyu.world I/zjc: 0 0.0
|
通过打印结果可以知道:在viewpager移动的时候,首先onPageSelected方法先被调用,他先确定被选中的item是哪个。然后onPageScrolled方法被调用,他告诉我们移动的过程,比如我们从1 -> 2。这个时候onPageSelected先返回2,然后onPageScrolled先指示出1的移动过程移动结束后,position变成0,移动距离offset变成0,因为已经移动到了目标item所以offset变成了0