Material Design Series, custom behavior support all view

Source: Internet
Author: User

Material Design Series, custom behavior support all view

Copyright NOTICE: Reprint must indicate this article transferred from Zhangjie's blog: http://blog.csdn.net/yanzhenjie1003

Friendship Connection:
Material Design Blog Column

Series Blog:
1. Material Design Series, Behavior Bottomsheetbehavior and Bottomsheetdialog
2. Material Design Series, Behavior Swipedismissbehavior
3. Material Design Series, customize behavior slide show back top button
4. Material Design Series, custom behavior realize Android know home
5. Material Design Series, custom behavior support all View

Suggested to see today's blog Sir First look at the previous several blog, because today's blog is a few of the previous upgrade, do not look at the front, see this article you will feel no meaning.

Blog and demo source download: Portal.

First, to achieve

The fab in the lower right corner, animation of course can be a variety of, can be placed anywhere in the interface, we just give an example. However, the V7 package can Behavior only be used for the present FloatingActionButton , so today we implement this behavior is to support all the view, can be used in,, ImageView Button Layout as long as the inherited View classes are available.

Second, custom behavior and animation package

We know Behavior CoordinatorLayout It's a subclass, and Ctrl + T looking at its implementation class currently has the following several:

1. AppBarLayout.Behavior;2. AppBarLayout.ScrollingViewBehavior;3. FloatingActionButton.Behavior;4. Snackbar.Behavior;5. BottomSheetBehaviro;6. SwipeDismissBehavior;7. HeaderBehavior;8. ViewOffsetBehavior;9. HeaderScrollingViewBehavior;

1th, 7 is the abstract class, 8 is the package protection class, 9 is a subclass of 8, we'll talk about it later.

AppBarLayout.ScrollingViewBehaviorWe often use, that is, what we layout xml often use in the: app:layout_behavior="@string/appbar_scrolling_view_behavior" .

Snackbar.Behavioris used Snackbar , this does not have to say much.

FloatingActionButton.Behavior, BottomSheetBehaviro SwipeDismissBehavior in the article at the beginning of a few links to the blog has been very clear, we can go back and look.

Today we're talking about a custom behavior that supports all view as fab, so FloatingActionButton.Behavior that's it, but it only supports it FloatingActionButton , so today we are going to write it ourselves Behavior DefineBehavior . So the first step is FloatingActionButton.Behavior to open the source to see.

Implement Basicbehavior

The first thing you have to know is that CoordinatorLayout.Behavior the base class is support generics, and see that FloatingActionButton.Behavior after the first it is limited to reference it View must be FloatingActionButton , then we have to learn it here to inherit a bit OK.

We create a new class BasicBehavior , FloatingActionButton.Behavior Copy the code, and change the generic to read as follows:

publicclass BasicBehavior<T extends View> extends CoordinatorLayout.Behavior<T>;

That is, as long as the reference 实现BasicBehavior class is a View can, so then the BasicBehavior copy of the code inside the reference to the generic type to FloatingActionButton the place View , ah feel the work Gaocheng when found that there are several classes of package guide do not come in:

Take a closer look, these classes are android.support.design.widget under the package, one wants to be sure that these classes are packages protected class, so we create a new package under our project, android.support.design.widget 实现BasicBehavior move to the new package, find the problem solved.

Project source BasicBehavior code and full source download link please look at the beginning or end of the article.

Implementation and simplification of animations

(before looking at the objective of the blog must look back, there will be a different harvest.) )
In the previous series, we implemented the zoom animation of the view, especially when the view was hidden with the following code to record whether the view move-out animation was executed, because the view removal is always called when the interface is sliding Behavior , so it cannot be executed repeatedly and requires a value to record:

//Record whether the view move out animation is finished. Private BooleanIsoutexecute =false;PrivateViewpropertyanimatorlistener Outanimatorlistener =NewViewpropertyanimatorlistener () {@Override     Public void Onanimationstart(View view) {Isoutexecute =true; }@Override     Public void Onanimationend(View view)        {view.setvisibility (View.gone); Isoutexecute =false; }@Override     Public void Onanimationcancel(View view) {Isoutexecute =false; }};

In order not to write this long for every call, we encapsulate this end code as a class, simplifying the following:

 Public Static  class listeneranimatorendbuild {    //Record whether the view move out animation is finished.     Private BooleanIsoutexecute =false;PrivateViewpropertyanimatorlistener Outanimatorlistener; Public Listeneranimatorendbuild() {Outanimatorlistener =NewViewpropertyanimatorlistener () {@Override             Public void Onanimationstart(View view) {Isoutexecute =true; }@Override             Public void Onanimationend(View view)                {view.setvisibility (View.gone); Isoutexecute =false; }@Override             Public void Onanimationcancel(View view) {Isoutexecute =false;    }        }; }//View moves the animation out of execution.      Public Boolean Isfinish() {return!isoutexecute; }//Return to Viewpropertyanimatorlistener.      PublicViewpropertyanimatorlistenerBuild() {returnOutanimatorlistener; }}

In this way we are only two lines of code when we use it:

new ListenerAnimatorEndBuild();// 判断是否执行完动画:listenerAnimatorEndBuild.isFinish();
Inherit Basicbehavior implementation Definebavior

As defined above BasicBehavior , we only need to inherit and BasicBehavior implement our animation logic:

 Public  class definebehavior extends basicbehavior<View> {    PrivateListeneranimatorendbuild Listeneranimatorendbuild; Public Definebehavior(context context, AttributeSet attrs) {Super(context, attrs); Listeneranimatorendbuild =NewListeneranimatorendbuild (); }@Override     Public Boolean Onstartnestedscroll(Coordinatorlayout coordinatorlayout, view child, view directtargetchild, view target,intNestedscrollaxes) {returnNestedscrollaxes = = viewcompat.scroll_axis_vertical; }@Override     Public void Onnestedscroll(Coordinatorlayout coordinatorlayout, view child, view target,intDxconsumed,intDyconsumed,intDxunconsumed,intdyunconsumed) {//if (dyconsumed > 0 && dyunconsumed = = 0) {//System.out.println ("Top slide ... ");//        }//if (dyconsumed = = 0 && dyunconsumed > 0) {//System.out.println ("The border is still on the slide ... ");//        }//if (dyconsumed < 0 && dyunconsumed = = 0) {//System.out.println ("Falling in ... ");//        }//if (dyconsumed = = 0 && dyunconsumed < 0) {//System.out.println ("to the border, still in decline ... ");//        }    //You can write your other logic animations here, just to give an example to write a zoom animation.         if((Dyconsumed >0|| dyunconsumed >0) && listeneranimatorendbuild.isfinish () && child.getvisibility () = = view.visible) {//Go downhillScalehide (Child, Listeneranimatorendbuild.build ()); }Else if((Dyconsumed <0|| Dyunconsumed <0) && child.getvisibility ()! = view.visible) {scaleshow (Child,NULL); }    }}

You might be surprised, haha, not to be surprised that the package is so easy to implement all of the view support for a long time.

Third, how to use

Using the same as Google offers Behavior , it is possible to refer to the full package name:

app:layout_behavior="com.yanzhenjie.definebehavior.behavior.DefineBehavior"

To Behavior be as simple as Google offers, we can define this string in String.xml:

<string name="define_behavior">com.yanzhenjie.definebehavior.behavior.DefineBehavior</string>

When used:

app:layout_behavior="@string/define_behavior"

Now let's replace the original project FloatingActionButton with ImageView :

<imageview  android:id  = "@+id/fab"  android:layout_width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_gravity  =" bottom|end " android:layout_margin  =" 16DP " android:src  = "@mipmap/ic_launcher"  app:layout_behavior  = "@string/define_behavior"  Span class= "Hljs-attribute" >app:layout_scrollflags  = "Scroll|enteralways|snap" /> 

Okay, OK, the specific effect of everyone download source bar: Portal.

Copyright NOTICE: Reprint must indicate this article transferred from Zhangjie's blog: http://blog.csdn.net/yanzhenjie1003

Material Design Series, custom behavior support all view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.