Original link: http://www.jianshu.com/p/9dbb03203fbc
Stabilize: prevent users tremor, many times in rapid succession with a button click.
Why do I need anti-tremor treatment?
During the jump between Fragment (v4), the following two situations can lead to a poor user experience :( Go to AFragment BFragment for example)
1, BFragment not really create (because the transaction is not Fragment start immediately), you tremor under 2 points, there will be a one-time start two BFragment;
2, in the jump BFragment process, if there is a transition animation, at any time before the end of the animation, you click on a region BFragment clickable page, click on the area of the event will be triggered.
Without any anti-tremor treatment, you may encounter situations GIF below:
No anti-tremor treatment
So for Fragment be anti-tremor treatment is still very necessary, RxJava series Rxbinding package can help you solve the problem, debounce operator can achieve anti-tremor, but a lot of cases, you will use RxJava but do not want to use Rxbinding.
In fact, no matter what kind of program, I think you are not willing to deal with all the manual once judder issues in each event click in.
We need a base class package, can solve the problem of anti-shake, and do not have to be anti-shake button to start the process in each of the Fragment.
Common program
There is one kind of common anti-tremor treatment: by the time difference
|
|
Although this method can achieve the effect of anti-shake, but the applicability is limited;
for example, if I need to add a Fragment, then followed by the immediate need and Fragment Fragment add a sub, then add sub Fragment of operation might be the time difference is masked.
My program
In view of the above-described binding process, is combined with a host Fragment Activity, I have the following protocol:
using the Activity dispatchTouchEvent(MotionEvent ev)
method.
General idea is that after clicking immediately to intercept everything Activity Touch event, at the end of the transition animation target Fragment (if no transition animations, it is after onActivityCreated is called), let Activity unblocked.
Next we will look at the specifics of how to achieve it!
In BaseActivity in:
|
|
所有Activity中的Touch事件,首先都需要经过Activity的dispatchTouchEvent
方法,该方法通过Window分发Touch事件,如果返回true,则不再往下层分发,这时我们布局内的任何Touch事件都会“无效”。
在通过Activity来加载Fragment时,将mAllowClickable设为false,此时只到为true时,屏幕的Touch事件将都无效。
在BaseFragment中:
这里将有转场动画和无转场动画两种情况都进行了防手抖处理:
|
|
When Fragment initialization sequence of their life cycle:
... -> onCreateView -> onCreateAnimation -> onActivityCreated
Thus the life cycle, combined with comments on the code, I believe you understand at a glance ~
Finally, the use of Android event distribution mechanism, using less than 50 lines of code perfect solution to handle Fragment of anti-tremor.
Finally, look at the effect of:
Anti-tremor
To get more details or depth Fragment optimization using Fragment of a small partner, I recommend a look at this Fragmentation library
Shall not be reproduced without permission, please indicate the zilianliuxue the blog, I All Rights Reserved.