Android coordinatorlayout, Appbarlayout, Drawerlayout, Navigationview use and problems summary

Source: Internet
Author: User

This is only a summary of the important parts of these components used in material design and where problems occur (see the final FAQ section for more details), please consult the official documentation yourself

First, Coordinatorlayout

Introduction: Coordinatorlayout is primarily used as a top-level layout to coordinate sub-layouts

use: The coordinatorlayout contains a child and a dependency, and the final need for one of the most importantBehavior。

1) Child refers to the child to perform the action CoordinatorLayout View ;

2) Dependency refers Child to Reliance View ;

3) Simply put, that is, if Dependency the view has changed, then Child this will be changed View accordingly;

4) The code for the Child specific execution of the change is placed in Behavior this class.

5) The most basic usage of behavior is to define an app in the child's view XML file : layout_behavior="@string/appbar_scrolling_view_ Behavior " means to follow the dependency slide together

Custom behavior:

1) First, define a class, inheritance CoordinatorLayout.Behavior<T> , where the generic parameter T is the class in which we want to execute the action View , that is Child . Then there are two ways to achieve this Behavior :

/*** Determine if child's layout depends on dependency*/@Override Public BooleanLayoutdependson (coordinatorlayout parent, T Child, View dependency) {Booleanrs; //based on the logic to determine the value of RS, for dependency instanceof CustomDependency example: rs =    //returns false to indicate that child does not rely on Dependency,ture for dependency    returnrs; }/*** When the dependency changes (position, width and high), execute this function * Returns true to indicate the location of the child or the width of the change, otherwise it will return false
* 每次dependency位置发生变化,都会执行onDependentViewChanged方法*/@Override Public Booleanondependentviewchanged (coordinatorlayout parent, T Child, View dependency) {//specific actions to be performed by child return true;}

Second, appbarlayout

Description: The gesture sliding effect of sliding view is customized by Appbarlayout's five kinds of scrollflags.

use: There are two ways to set scrollflags and fiveScrollFlags

1) by defining in the XML fileapp:layout_scrollFlags属性来设置,代码中通过setScrollFlags(int)方法设置

2) Scroll:

• You need to set this value for the view you want to scroll out of the screen;

• If there is no Other_view setting this value before setting this flag's view, the view setting will be invalidated;

• The use of the other four flags must be used in conjunction with the scroll.

3) Enteralways:

If the view has the flag set, then the view will enable quick return mode, which means that the mode is quickly returned, that is, sliding the view in the swipe down will give the its first display

4) Enteralwayscollapsed:

Enteralways added value, if the view has both the flag set and the minimum height minheight, in the swipe down, it will take precedence to slide the view to the minimum height, then slide the other scrolling view, when other sliding view slide to the boundary , set the flag's view and then continue sliding to show the full

5) Enteruntilcollapsed:

If the view has the flag set and the minimum height minheight is set, the view will slide out only to the minimum height during the swipe up, and will not completely slide out of the screen

6) Snap:

The main is to achieve a sliding adsorption effect, either the view all slide out of the screen, or all displayed on the screen, will not appear only half of the situation displayed on the screen, similar to the Viewpager sliding effect

Third, Drawerlayout and Navigationview

Introduction: through the drawerlayout and Navigationview these two are mainly side-slip and side-slip menu implementation

use: drawerlayout as the parent layout, home page content as the first child layout, and Navigationview as the last layout

1) It is usually necessary to specify a header layout and a menu layout in Navigationview, respectively: menu="@menu/xxx" and app:headerlayout=" @layout/xxx "

  2) There is a group and item two nodes in the menu, the difference is that there will be split lines between the two groups to represent the different group

3) An Android is also required in the Navigationview XML: The layout_gravity="Start" property indicates that it can be drawn from the left or right

Problems:

1. When combined with coordinatorlayout and Appbarlayout, the Scroll_flag is set but no immersive effect is achieved

  Reason: Coordinatorlayout's Child view is Viewpager, and Viewpager is nested in a ListView

  Solution: You must use Recyclerview to

2. When combined with drawerlayout and Navigationview, there is no way to withdraw the side-slip menu by left stroke

  Reason: Navigationview not as the last sub-layout

  Solution: You need to use the home page as the first sub-layout, and Navigationview as the last child layout

3. When combined with drawerlayout and Navigationview, only the sidebar is displayed and full screen

  Reason: android not set Navigationview : layout_gravity="Start" Property

  Solution: Set this property, maybe Android studio does not have this property hint, need pure hand to hit

Android coordinatorlayout, Appbarlayout, Drawerlayout, Navigationview usage and issues summary

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.