How to flexibly use ActionBar, Google music ActionBar to hide and display effects, and googleactionbar

Source: Internet
Author: User

How to flexibly use ActionBar, Google music ActionBar to hide and display effects, and googleactionbar

The history of ActionBar is not introduced here. I believe everyone knows it. In an app, if ActionBar is used well, it will save a lot of code, and the effect of the entire app is quite good, if you are interested, you can download the google music app. The interface looks quite comfortable. OK. Here we will teach you a quick and convenient way to use ActionBar.

I still remember an alternative implementation of the custom style in the blog Android AlertDialog dialog box I wrote. That's right. Here we will use the method mentioned in that blog.

    public int getIdentifier(String name, String defType, String defPackage) {        if (name == null) {            throw new NullPointerException("name is null");        }        try {            return Integer.parseInt(name);        } catch (Exception e) {            // Ignore        }        return mAssets.getResourceIdentifier(name, defType, defPackage);    }

You will not be unfamiliar with this!
However, this method requires you to know the id of the ActionBar control. Here I will tell you that it is "action_bar_container"

    <android.support.v7.internal.widget.ActionBarContainer            android:id="@+id/action_bar_container"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentTop="true"            style="?attr/actionBarStyle"            android:touchscreenBlocksFocus="true"            android:gravity="top">        <android.support.v7.widget.Toolbar                android:id="@+id/action_bar"                android:layout_width="match_parent"                android:layout_height="wrap_content"                app:navigationContentDescription="@string/abc_action_bar_up_description"                style="?attr/toolbarStyle"/>

It is easy to find.
Therefore, we can easily obtain the ActionBar View.
The Code is as follows:

    private View getActionBarView(){        View view = getWindow().getDecorView() ;        int actionBarId  = getResources().getIdentifier("action_bar_container","id",getPackageName()) ;        return view.findViewById(actionBarId) ;    }

What can we do after we get the View? Let's look at the two figures:

The ActionBar color is changed and the angle is rotated along the X axis.

This... Vertical ActionBar. See it for the first time!

Through the above two pictures, I just want to explain that, with the View of the ActionBar, We can flexibly control the ActionBar, such as the display and hiding effects of Google Music, next let's take a look at how to implement it.

First look at the dynamic diagram:

This effect is that when the RecyclerView slides up the ActionBar, The ActionBar is hidden and the moving distance of the ActionBar is controlled as the sliding distance.
Code is very simple

@ Override public void onScrolled (RecyclerView recyclerView, int dx, int dy) {super. onScrolled (recyclerView, dx, dy); int transY = (int) (mActionBarView. getTranslationY ()-dy); // The moving distance of the ActionBar cannot exceed the boundary transY = (int) clamp (transY,-mActionBarHeight, 0); mActionBarView. setTranslationY (transY );}

However, after the animation is released, another animation needs to be processed. If the display height of the ActionBar is half that of the height of the ActionBar, the animation is used to show all the animations. Otherwise, the animation is hidden.

@Overridepublic void onScrollStateChanged(RecyclerView recyclerView, int newState) {    super.onScrollStateChanged(recyclerView, newState);      if (newState == RecyclerView.SCROLL_STATE_IDLE){         checkAnimation() ;       }}
private void checkAnimation(){    int transY = (int)mActionBarView.getTranslationY();   if (transY != 0|| transY != -mActionBarHeight){       startAnimation() ;   }}private void startAnimation(){    float [] value = new float[2] ;    value[0] = mActionBarView.getTranslationY();    if (value[0] > -mActionBarHeight/2.0f){         value[1] = 0 ;    }else {         value[1] = - mActionBarHeight ;    }    ObjectAnimator animator =ObjectAnimator.ofFloat(MainActivity.this,"transY",value) ;   animator.setDuration(150) ;   animator.start();}

Here we mainly deal with animation and translation. If you are not familiar with it, you can download the source code and analyze it.

Summary:

The following method is used to obtain the View of the ActionBar:

public int getIdentifier(String name, String defType, String defPackage)

You need to know the name of the AcitonBar resource id. You can view the source code.

Source code: I am the code

Related Article

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.