解决ViewFlow在Scrollview下滑动不灵敏的BUG(解决viewflow与父控件的滑动事件冲突问题)

转自http://www.oschina.net/question/778954_158470?sort=time

 

第一次在开源中国发分享贴,有点小紧张,把我最近遇到的问题和解决方法分享出来,供遇到此类问题的朋友参考

PS:关联代码来源于开源社区

最近针对公司的电商客户端做优化,首页实现类似主流电商客户端那样,有左右banner图片切换,实现下拉刷新

但是,在实现的过程中遇到了一个问题,当左后滑动的时候,不是很灵敏,一卡卡的,外层是一个Scrollview,banner是用的viewFlow,后修改源码如下,完美决问题(此方法同样适用于ViewPager)

处理办法:添加手势控制,当左后滑动的时候,把滑动事件传递给子项处理,当上下滑动的时候,把滑动事件交给ViewGroup处理

重要修改代码:

?
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public boolean onInterceptTouchEvent(MotionEvent ev) {
         if (getChildCount() == 0 )
             return false ;
 
         if (mVelocityTracker == null ) {
             mVelocityTracker = VelocityTracker.obtain();
         }
         mVelocityTracker.addMovement(ev);
 
         final int action = ev.getAction();
         final float x = ev.getX();
 
         switch (action) {
         case MotionEvent.ACTION_DOWN:
             // 处理事件,左后滑动时,传递给子项处理,上下滑动时,交由ViewGroup处理
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent(!mGestureDetector.onTouchEvent(ev));
             }
 
             if (!mScroller.isFinished()) {
                 mScroller.abortAnimation();
             }
 
             // Remember where the motion event started
             mLastMotionX = x;
 
             mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
             if (handler != null )
                 handler.removeMessages( 0 );
             break ;
 
         case MotionEvent.ACTION_MOVE:
             // 处理事件,左后滑动时,传递给子项处理,上下滑动时,交由ViewGroup处理
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent(!mGestureDetector.onTouchEvent(ev));
             }
 
             final int xDiff = ( int ) Math.abs(x - mLastMotionX);
 
             boolean xMoved = xDiff > mTouchSlop;
 
             if (xMoved) {
                 // Scroll if the user moved far enough along the X axis
                 mTouchState = TOUCH_STATE_SCROLLING;
             }
 
             if (mTouchState == TOUCH_STATE_SCROLLING) {
                 // Scroll to follow the motion event
                 final int deltaX = ( int ) (mLastMotionX - x);
                 mLastMotionX = x;
 
                 final int scrollX = getScrollX();
                 if (deltaX < 0 ) {
                     if (scrollX > 0 ) {
                         scrollBy(Math.max(-scrollX, deltaX), 0 );
                     }
                 } else if (deltaX > 0 ) {
                     final int availableToScroll = getChildAt(getChildCount() - 1 ).getRight() - scrollX - getWidth();
                     if (availableToScroll > 0 ) {
                         scrollBy(Math.min(availableToScroll, deltaX), 0 );
                     }
                 }
                 return true ;
             }
             break ;
 
         case MotionEvent.ACTION_UP:
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent( false );
             }
 
             if (mTouchState == TOUCH_STATE_SCROLLING) {
                 final VelocityTracker velocityTracker = mVelocityTracker;
                 velocityTracker.computeCurrentVelocity( 1000 , mMaximumVelocity);
                 int velocityX = ( int ) velocityTracker.getXVelocity();
 
                 if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0 ) {
                     // Fling hard enough to move left
                     snapToScreen(mCurrentScreen - 1 );
                 } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1 ) {
                     // Fling hard enough to move right
                     snapToScreen(mCurrentScreen + 1 );
                 } else {
                     snapToDestination();
                 }
 
                 if (mVelocityTracker != null ) {
                     mVelocityTracker.recycle();
                     mVelocityTracker = null ;
                 }
             }
 
             mTouchState = TOUCH_STATE_REST;
             if (handler != null ) {
                 Message message = handler.obtainMessage( 0 );
                 handler.sendMessageDelayed(message, timeSpan);
             }
             break ;
         case MotionEvent.ACTION_CANCEL:
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent( false );
             }
 
             mTouchState = TOUCH_STATE_REST;
         }
         return false ;
     }
 
     /**
      * 手势监听(用于识别手势滑动)
      *
      * @author LiangZiChao
      *
      */
     class YScrollDetector extends SimpleOnGestureListener {
 
         @Override
         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
             /**
              * if we're scrolling more closer to x direction, return false, let
              * subview to process it
              */
             return (Math.abs(distanceY) > Math.abs(distanceX));
         }
     }



此方法同样适用于ViewPager,可以对比ViewFLow改造ViewPager

其中ViewGrop是外层的Scrollview,贴出源码,希望能给遇到问题的同学以参考,第一次发帖,多谢支持

?
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/***
  * Copyright (C) 2011 Patrik Åkerfeldt
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  ***/
package com.example.viewflowdemo.view;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Scroller;
 
import com.example.viewflowdemo.R;
 
/**
  * viewflow
  *
  * LiangZiChao Update By 2014-3-18
  **/
public class ViewFlow extends AdapterView<Adapter> {
 
     private static final int SNAP_VELOCITY = 1000 ;
     private static final int INVALID_SCREEN = - 1 ;
     private final static int TOUCH_STATE_REST = 0 ;
     private final static int TOUCH_STATE_SCROLLING = 1 ;
 
     private LinkedList<View> mLoadedViews;
     private int mCurrentBufferIndex;
     private int mCurrentAdapterIndex;
     private int mSideBuffer = 2 ;
     private Scroller mScroller;
     private VelocityTracker mVelocityTracker;
     private int mTouchState = TOUCH_STATE_REST;
     private float mLastMotionX;
     private int mTouchSlop;
     private int mMaximumVelocity;
     private int mCurrentScreen;
     private int mNextScreen = INVALID_SCREEN;
     private boolean mFirstLayout = true ;
     private ViewSwitchListener mViewSwitchListener;
     private Adapter mAdapter;
     private int mLastScrollDirection;
     private AdapterDataSetObserver mDataSetObserver;
     private ViewFlowIndicator mIndicator;
     private int mLastOrientation = - 1 ;
     private long timeSpan = 3000 ;
     private Handler handler;
     private ViewGroup viewGroup;
     private GestureDetector mGestureDetector;
 
     private OnGlobalLayoutListener orientationChangeListener = new OnGlobalLayoutListener() {
 
         @Override
         public void onGlobalLayout() {
             getViewTreeObserver().removeGlobalOnLayoutListener(orientationChangeListener);
             setSelection(mCurrentAdapterIndex);
         }
     };
 
         public static interface ViewSwitchListener {
 
         /**
          * This method is called when a new View has been scrolled to.
          *
          * @param view
          *            the {@link View} currently in focus.
          * @param position
          *            The position in the adapter of the {@link View} currently
          *            in focus.
          */
         void onSwitched(View view, int position);
 
     }
 
     public ViewFlow(Context context) {
         super (context);
         mSideBuffer = 3 ;
         init();
     }
 
     public ViewFlow(Context context, int sideBuffer) {
         super (context);
         mSideBuffer = sideBuffer;
         init();
     }
 
     public ViewFlow(Context context, AttributeSet attrs) {
         super (context, attrs);
         TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.ViewFlow);
         mSideBuffer = styledAttrs.getInt(R.styleable.ViewFlow_sidebuffer, 3 );
         init();
     }
 
     private void init() {
         mLoadedViews = new LinkedList<View>();
         mScroller = new Scroller(getContext());
         final ViewConfiguration configuration = ViewConfiguration.get(getContext());
         mTouchSlop = configuration.getScaledTouchSlop();
         mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
         mGestureDetector = new GestureDetector(getContext(), new YScrollDetector());
     }
 
     public void setViewGroup(ViewGroup viewGroup) {
         this .viewGroup = viewGroup;
     }
 
     public void startAutoFlowTimer() {
         handler = new Handler() {
             @Override
             public void handleMessage(Message msg) {
                 if (getChildCount() > 0 && mSideBuffer > 1 )
                     snapToScreen((mCurrentScreen + 1 ) % getChildCount());
                 Message message = handler.obtainMessage( 0 );
                 sendMessageDelayed(message, timeSpan);
             }
         };
 
         Message message = handler.obtainMessage( 0 );
         handler.sendMessageDelayed(message, timeSpan);
     }
 
     public void stopAutoFlowTimer() {
         if (handler != null )
             handler.removeMessages( 0 );
         handler = null ;
     }
 
     public void onConfigurationChanged(Configuration newConfig) {
         if (newConfig.orientation != mLastOrientation) {
             mLastOrientation = newConfig.orientation;
             getViewTreeObserver().addOnGlobalLayoutListener(orientationChangeListener);
         }
     }
 
     public int getViewsCount() {
         return mSideBuffer;
     }
 
     @Override
     protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
         super .onMeasure(widthMeasureSpec, heightMeasureSpec);
 
         final int width = MeasureSpec.getSize(widthMeasureSpec);
         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
         if (widthMode != MeasureSpec.EXACTLY && !isInEditMode()) {
             throw new IllegalStateException( "ViewFlow can only be used in EXACTLY mode." );
         }
 
         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
         if (heightMode != MeasureSpec.EXACTLY && !isInEditMode()) {
             throw new IllegalStateException( "ViewFlow can only be used in EXACTLY mode." );
         }
 
         // The children are given the same width and height as the workspace
         final int count = getChildCount();
         for ( int i = 0 ; i < count; i++) {
             getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
         }
 
         if (mFirstLayout) {
             mScroller.startScroll( 0 , 0 , mCurrentScreen * width, 0 , 0 );
             mFirstLayout = false ;
         }
     }
 
     @Override
     protected void onLayout( boolean changed, int l, int t, int r, int b) {
         int childLeft = 0 ;
 
         final int count = getChildCount();
         for ( int i = 0 ; i < count; i++) {
             final View child = getChildAt(i);
             if (child.getVisibility() != View.GONE) {
                 final int childWidth = child.getMeasuredWidth();
                 child.layout(childLeft, 0 , childLeft + childWidth, child.getMeasuredHeight());
                 childLeft += childWidth;
             }
         }
     }
 
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         if (getChildCount() == 0 )
             return false ;
 
         if (mVelocityTracker == null ) {
             mVelocityTracker = VelocityTracker.obtain();
         }
         mVelocityTracker.addMovement(ev);
 
         final int action = ev.getAction();
         final float x = ev.getX();
 
         switch (action) {
         case MotionEvent.ACTION_DOWN:
             
             // 处理事件,左后滑动时,传递给子项处理,上下滑动时,交由ViewGroup处理
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent(!mGestureDetector.onTouchEvent(ev));
             }
 
             if (!mScroller.isFinished()) {
                 mScroller.abortAnimation();
             }
 
             // Remember where the motion event started
             mLastMotionX = x;
 
             mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
             if (handler != null )
                 handler.removeMessages( 0 );
             break ;
 
         case MotionEvent.ACTION_MOVE:
             // 处理事件,左后滑动时,传递给子项处理,上下滑动时,交由ViewGroup处理
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent(!mGestureDetector.onTouchEvent(ev));
             }
 
             final int xDiff = ( int ) Math.abs(x - mLastMotionX);
 
             boolean xMoved = xDiff > mTouchSlop;
 
             if (xMoved) {
                 // Scroll if the user moved far enough along the X axis
                 mTouchState = TOUCH_STATE_SCROLLING;
             }
 
             if (mTouchState == TOUCH_STATE_SCROLLING) {
                 // Scroll to follow the motion event
                 final int deltaX = ( int ) (mLastMotionX - x);
                 mLastMotionX = x;
 
                 final int scrollX = getScrollX();
                 if (deltaX < 0 ) {
                     if (scrollX > 0 ) {
                         scrollBy(Math.max(-scrollX, deltaX), 0 );
                     }
                 } else if (deltaX > 0 ) {
                     final int availableToScroll = getChildAt(getChildCount() - 1 ).getRight() - scrollX - getWidth();
                     if (availableToScroll > 0 ) {
                         scrollBy(Math.min(availableToScroll, deltaX), 0 );
                     }
                 }
                 return true ;
             }
             break ;
 
         case MotionEvent.ACTION_UP:
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent( false );
             }
 
             if (mTouchState == TOUCH_STATE_SCROLLING) {
                 final VelocityTracker velocityTracker = mVelocityTracker;
                 velocityTracker.computeCurrentVelocity( 1000 , mMaximumVelocity);
                 int velocityX = ( int ) velocityTracker.getXVelocity();
 
                 if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0 ) {
                     // Fling hard enough to move left
                     snapToScreen(mCurrentScreen - 1 );
                 } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1 ) {
                     // Fling hard enough to move right
                     snapToScreen(mCurrentScreen + 1 );
                 } else {
                     snapToDestination();
                 }
 
                 if (mVelocityTracker != null ) {
                     mVelocityTracker.recycle();
                     mVelocityTracker = null ;
                 }
             }
 
             mTouchState = TOUCH_STATE_REST;
             if (handler != null ) {
                 Message message = handler.obtainMessage( 0 );
                 handler.sendMessageDelayed(message, timeSpan);
             }
             break ;
         case MotionEvent.ACTION_CANCEL:
             if (viewGroup != null ) {
                 viewGroup.requestDisallowInterceptTouchEvent( false );
             }
 
             mTouchState = TOUCH_STATE_REST;
         }
         return false ;
     }
 
     /**
      * 手势监听(用于识别手势滑动)
      *
      * @author LiangZiChao
      *
      */
     class YScrollDetector extends SimpleOnGestureListener {
 
         @Override
         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
             /**
              * if we're scrolling more closer to x direction, return false, let
              * subview to process it
              */
             return (Math.abs(distanceY) > Math.abs(distanceX));
         }
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent ev) {
         if (getChildCount() == 0 )
             return false ;
 
         if (mVelocityTracker == null ) {
             mVelocityTracker = VelocityTracker.obtain();
         }
         mVelocityTracker.addMovement(ev);
 
         final int action = ev.getAction();
         final float x = ev.getX();
 
         switch (action) {
         case MotionEvent.ACTION_DOWN:
             
             if (!mScroller.isFinished()) {
                 mScroller.abortAnimation();
             }
 
             // Remember where the motion event started
             mLastMotionX = x;
 
             mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
             if (handler != null )
                 handler.removeMessages( 0 );
             break ;
 
         case MotionEvent.ACTION_MOVE:
             final int xDiff = ( int ) Math.abs(x - mLastMotionX);
 
             boolean xMoved = xDiff > mTouchSlop;
 
             if (xMoved) {
                 // Scroll if the user moved far enough along the X axis
                 mTouchState = TOUCH_STATE_SCROLLING;
             }
 
             if (mTouchState == TOUCH_STATE_SCROLLING) {
                 // Scroll to follow the motion event
                 final int deltaX = ( int ) (mLastMotionX - x);
                 mLastMotionX = x;
 
                 final int scrollX = getScrollX();
                 if (deltaX < 0 ) {
                     if (scrollX > 0 ) {
                         scrollBy(Math.max(-scrollX, deltaX), 0 );
                     }
                 } else if (deltaX > 0 ) {
                     final int availableToScroll = getChildAt(getChildCount() - 1 ).getRight() - scrollX - getWidth();
                     if (availableToScroll > 0 ) {
                         scrollBy(Math.min(availableToScroll, deltaX), 0 );
                     }
                 }
                 return true ;
             }
             break ;
 
         case MotionEvent.ACTION_UP:
             if (mTouchState == TOUCH_STATE_SCROLLING) {
                 final VelocityTracker velocityTracker = mVelocityTracker;
                 velocityTracker.computeCurrentVelocity( 1000 , mMaximumVelocity);
                 int velocityX = ( int ) velocityTracker.getXVelocity();
 
                 if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0 ) {
                     // Fling hard enough to move left
                     snapToScreen(mCurrentScreen - 1 );
                 } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1 ) {
                     // Fling hard enough to move right
                     snapToScreen(mCurrentScreen + 1 );
                 }
                 // else if (velocityX < -SNAP_VELOCITY
                 // && mCurrentScreen == getChildCount() - 1) {
                 // snapToScreen(0);
                 // }
                 // else if (velocityX > SNAP_VELOCITY
                 // && mCurrentScreen == 0) {
                 // snapToScreen(getChildCount() - 1);
                 // }
                 else {
                     snapToDestination();
                 }
 
                 if (mVelocityTracker != null ) {
                     mVelocityTracker.recycle();
                     mVelocityTracker = null ;
                 }
             }
 
             mTouchState = TOUCH_STATE_REST;
 
             if (handler != null ) {
                 Message message = handler.obtainMessage( 0 );
                 handler.sendMessageDelayed(message, timeSpan);
             }
             break ;
         case MotionEvent.ACTION_CANCEL:
             snapToDestination();
             mTouchState = TOUCH_STATE_REST;
         }
         return true ;
     }
 
     @Override
     protected void onScrollChanged( int h, int v, int oldh, int oldv) {
         super .onScrollChanged(h, v, oldh, oldv);
         if (mIndicator != null ) {
             /*
              * The actual horizontal scroll origin does typically not match the
              * perceived one. Therefore, we need to calculate the perceived
              * horizontal scroll origin here, since we use a view buffer.
              */
             int hPerceived = h + (mCurrentAdapterIndex - mCurrentBufferIndex) * getWidth();
             mIndicator.onScrolled(hPerceived, v, oldh, oldv);
         }
     }
 
     private void snapToDestination() {
         final int screenWidth = getWidth();
         final int whichScreen = (getScrollX() + (screenWidth / 2 )) / screenWidth;
 
         snapToScreen(whichScreen);
     }
 
     private void snapToScreen( int whichScreen) {
         mLastScrollDirection = whichScreen - mCurrentScreen;
         if (!mScroller.isFinished())
             return ;
 
         whichScreen = Math.max( 0 , Math.min(whichScreen, getChildCount() - 1 ));
 
         mNextScreen = whichScreen;
 
         final int newX = whichScreen * getWidth();
         final int delta = newX - getScrollX();
         mScroller.startScroll(getScrollX(), 0 , delta, 0 , Math.abs(delta) * 2 );
         invalidate();
     }
 
     @Override
     public void computeScroll() {
         if (mScroller.computeScrollOffset()) {
             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
             postInvalidate();
         } else if (mNextScreen != INVALID_SCREEN) {
             mCurrentScreen = Math.max( 0 , Math.min(mNextScreen, getChildCount() - 1 ));
             mNextScreen = INVALID_SCREEN;
             postViewSwitched(mLastScrollDirection);
         }
     }
 
     /**
      * Scroll to the {@link View} in the view buffer specified by the index.
      *
      * @param indexInBuffer
      *            Index of the view in the view buffer.
      */
     private void setVisibleView( int indexInBuffer, boolean uiThread) {
         mCurrentScreen = Math.max( 0 , Math.min(indexInBuffer, getChildCount() - 1 ));
         int dx = (mCurrentScreen * getWidth()) - mScroller.getCurrX();
         mScroller.startScroll(mScroller.getCurrX(), mScroller.getCurrY(), dx, 0 , 0 );
         if (dx == 0 )
             onScrollChanged(mScroller.getCurrX() + dx, mScroller.getCurrY(), mScroller.getCurrX() + dx, mScroller.getCurrY());
         if (uiThread)
             invalidate();
         else
             postInvalidate();
     }
 
     /**
      * Set the listener that will receive notifications every time the {code
      * ViewFlow} scrolls.
      *
      * @param l
      *            the scroll listener
      */
     public void setOnViewSwitchListener(ViewSwitchListener l) {
         mViewSwitchListener = l;
     }
 
     @Override
     public Adapter getAdapter() {
         return mAdapter;
     }
 
     @Override
     public void setAdapter(Adapter adapter) {
         setAdapter(adapter, 0 );
     }
 
     public void setAdapter(Adapter adapter, int initialPosition) {
         if (mAdapter != null ) {
             mAdapter.unregisterDataSetObserver(mDataSetObserver);
         }
 
         mAdapter = adapter;
 
         if (mAdapter != null ) {
             mDataSetObserver = new AdapterDataSetObserver();
             mAdapter.registerDataSetObserver(mDataSetObserver);
 
         }
         if (mAdapter == null || mAdapter.getCount() == 0 )
             return ;
 
         setSelection(initialPosition);
     }
 
     @Override
     public View getSelectedView() {
         return (mCurrentBufferIndex < mLoadedViews.size() ? mLoadedViews.get(mCurrentBufferIndex) : null );
     }
 
     @Override
     public int getSelectedItemPosition() {
         return mCurrentAdapterIndex;
     }
 
     /**
      * Set the FlowIndicator
      *
      * @param flowIndicator
      */
     public void setFlowIndicator(ViewFlowIndicator flowIndicator) {
         mIndicator = flowIndicator;
         mIndicator.setViewFlow( this );
     }
 
     @Override
     public void setSelection( int position) {
         mNextScreen = INVALID_SCREEN;
         mScroller.forceFinished( true );
         if (mAdapter == null )
             return ;
 
         position = Math.max(position, 0 );
         position = Math.min(position, mAdapter.getCount() - 1 );
 
         ArrayList<View> recycleViews = new ArrayList<View>();
         View recycleView;
         while (!mLoadedViews.isEmpty()) {
             recycleViews.add(recycleView = mLoadedViews.remove());
             detachViewFromParent(recycleView);
         }
 
         View currentView = makeAndAddView(position, true , (recycleViews.isEmpty() ? null : recycleViews.remove( 0 )));
         mLoadedViews.addLast(currentView);
 
         for ( int offset = 1 ; mSideBuffer - offset >= 0 ; offset++) {
             int leftIndex = position - offset;
             int rightIndex = position + offset;
             if (leftIndex >= 0 )
                 mLoadedViews.addFirst(makeAndAddView(leftIndex, false , (recycleViews.isEmpty() ? null : recycleViews.remove( 0 ))));
             if (rightIndex < mAdapter.getCount())
                 mLoadedViews.addLast(makeAndAddView(rightIndex, true , (recycleViews.isEmpty() ? null : recycleViews.remove( 0 ))));
         }
 
         mCurrentBufferIndex = mLoadedViews.indexOf(currentView);
         mCurrentAdapterIndex = position;
 
         for (View view : recycleViews) {
             removeDetachedView(view, false );
         }
         requestLayout();
         setVisibleView(mCurrentBufferIndex, false );
         if (mIndicator != null ) {
             mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex);
         }
         if (mViewSwitchListener != null ) {
             mViewSwitchListener.onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex);
         }
     }
 
     private void resetFocus() {
         mLoadedViews.clear();
         removeAllViewsInLayout();
 
         for ( int i = Math.max( 0 , mCurrentAdapterIndex - mSideBuffer); i < Math.min(mAdapter.getCount(), mCurrentAdapterIndex + mSideBuffer + 1 ); i++) {
             mLoadedViews.addLast(makeAndAddView(i, true , null ));
             if (i == mCurrentAdapterIndex)
                 mCurrentBufferIndex = mLoadedViews.size() - 1 ;
         }
         requestLayout();
     }
 
     private void postViewSwitched( int direction) {
         if (direction == 0 )
             return ;
 
         if (direction > 0 ) { // to the right
             mCurrentAdapterIndex++;
             mCurrentBufferIndex++;
 
             // if(direction > 1) {
             // mCurrentAdapterIndex += mAdapter.getCount() - 2;
             // mCurrentBufferIndex += mAdapter.getCount() - 2;
             // }
 
             View recycleView = null ;
 
             // Remove view outside buffer range
             if (mCurrentAdapterIndex > mSideBuffer) {
                 recycleView = mLoadedViews.removeFirst();
                 detachViewFromParent(recycleView);
                 // removeView(recycleView);
                 mCurrentBufferIndex--;
             }
 
             // Add new view to buffer
             int newBufferIndex = mCurrentAdapterIndex + mSideBuffer;
             if (newBufferIndex < mAdapter.getCount())
                 mLoadedViews.addLast(makeAndAddView(newBufferIndex, true , recycleView));
 
         } else { // to the left
             mCurrentAdapterIndex--;
             mCurrentBufferIndex--;
 
             // if(direction < -1) {
             // mCurrentAdapterIndex -= mAdapter.getCount() - 2;
             // mCurrentBufferIndex -= mAdapter.getCount() - 2;
             // }
 
             View recycleView = null ;
 
             // Remove view outside buffer range
             if (mAdapter.getCount() - 1 - mCurrentAdapterIndex > mSideBuffer) {
                 recycleView = mLoadedViews.removeLast();
                 detachViewFromParent(recycleView);
             }
 
             // Add new view to buffer
             int newBufferIndex = mCurrentAdapterIndex - mSideBuffer;
             if (newBufferIndex > - 1 ) {
                 mLoadedViews.addFirst(makeAndAddView(newBufferIndex, false , recycleView));
                 mCurrentBufferIndex++;
             }
 
         }
 
         requestLayout();
         setVisibleView(mCurrentBufferIndex, true );
         if (mIndicator != null ) {
             mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex);
         }
         if (mViewSwitchListener != null ) {
             mViewSwitchListener.onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex);
         }
     }
 
     private View setupChild(View child, boolean addToEnd, boolean recycle) {
         ViewGroup.LayoutParams p = (ViewGroup.LayoutParams) child.getLayoutParams();
         if (p == null ) {
             p = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0 );
         }
         if (recycle)
             attachViewToParent(child, (addToEnd ? - 1 : 0 ), p);
         else
             addViewInLayout(child, (addToEnd ? - 1 : 0 ), p, true );
         return child;
     }
 
     private View makeAndAddView( int position, boolean addToEnd, View convertView) {
         View view = mAdapter.getView(position, convertView, this );
         return setupChild(view, addToEnd, convertView != null );
     }
 
     class AdapterDataSetObserver extends DataSetObserver {
 
         @Override
         public void onChanged() {
             View v = getChildAt(mCurrentBufferIndex);
             if (v != null ) {
                 for ( int index = 0 ; index < mAdapter.getCount(); index++) {
                     if (v.equals(mAdapter.getItem(index))) {
                         mCurrentAdapterIndex = index;
                         break ;
                     }
                 }
             }
             resetFocus();
         }
 
         @Override
         public void onInvalidated() {
             // Not yet implemented!
         }
 
     }
 
     public void setTimeSpan( long timeSpan) {
         this .timeSpan = timeSpan;
     }
 
     public void setSideBuffer( int sideBuffer) {
         this .mSideBuffer = sideBuffer;
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值