The super-strong control of Android design package Coordinatorlayout and Snackbar simple use

Source: Internet
Author: User

In the previous design, learning to use the magical effects of Tablayout,navigationview and Drawerlayout, today brings the control coordinatorlayout that I think is most meaningful in this design package.

Of course, there are snackbar, but in practice he is generally used in conjunction with the Coordinatorlayout.

First of all, Snackbar, this control actually I think and toast is no different, but the function is indeed enhanced. This control can be set like a button by the Setaction method. And this thing can be set multiple.

The focus is on our coordinatorlayout control, which is an enhanced framelayout that is powerful enough to add a lot of interesting stuff to its sub-layouts.

It is usually used in conjunction with behavior.

First a wave of the run diagram:

Control can be displayed and hidden in behavior by listening to Scoll

Package Com.example.nanchen.designcoodinatordemo;import Android.content.context;import Android.support.design.widget.coordinatorlayout;import Android.support.v4.view.viewcompat;import Android.util.attributeset;import android.view.view;/** * Custom behavior * Created by Southern dust on 16-7-14. */public class Mybehavior extends Coordinatorlayout.behavior {//write this construction method to specify public Mybehavior directly in the XML file (Context con    Text, AttributeSet attrs) {Super (context, attrs); } @Override public boolean onstartnestedscroll (Coordinatorlayout coordinatorlayout, view child, view Directtargetchi LD, View target, int nestedscrollaxes) {return true;//return true means we care about this scrolling event} @Override public void onnested Prescroll (coordinatorlayout coordinatorlayout, view child, view target, int dx, int dy, int[] consumed) {Super.onn        Estedprescroll (coordinatorlayout, child, target, DX, dy, consumed);        if (Dy < 0) {//Scroll down viewcompat.animate (child). ScaleX (1). Alpha (1). Start ();} else {//scroll up viewcompat.animate (child). ScaleX (0). Alpha (0). Start (); }    }}

This will disappear when you pull down.

It is important to note that if you want to display the specified behavior in XML, you must override the construction method

Public Mybehavior (context context, AttributeSet Attrs) {        Super (context, attrs);    }

  

Some of the other code

Package Com.example.nanchen.designcoodinatordemo;import Android.os.bundle;import Android.support.design.widget.snackbar;import Android.support.design.widget.swipedismissbehavior;import Android.support.v4.view.viewcompat;import Android.support.v7.app.appcompatactivity;import Android.support.v7.widget.recyclerview;import Android.view.view;import Android.widget.textview;import Java.util.arraylist;import Java.util.list;import Java.util.locale;public class Mainactivity extends    Appcompatactivity implements Swipedismissbehavior.ondismisslistener {private TextView TV;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TV = (TextView) Findviewbyid (R.ID.MAIN_TV);        Recyclerview Recyclerview = (recyclerview) Findviewbyid (R.id.main_recycler);        list<string> list = new arraylist<> (); for (int i = 0; i < i++) {List.add (String.Format (LOcale.        China, "line%03d", i));        } myadapter adapter = new Myadapter (list,this); Recyclerview.setadapter (adapter);//Coordinatorlayout.layoutparams params = (coordinatorlayout.layoutparams) tv.get Layoutparams ();//swipedismissbehavior<textview> behavior = new swipedismissbehavior<> ();//Beha Vior.setlistener (this);//Set a listener//params.setbehavior (behavior);//Set a behavior//mybehavior behavior = new Mybehavi    or ();//Params.setbehavior (behavior);        } @Override public void Ondismiss (view view) {view.setvisibility (view.gone); Snackbar.make (View, "deleted a control!                    ", Snackbar.length_short). Setaction (" undo ", New View.onclicklistener () {@Override                        public void OnClick (View v) {tv.setvisibility (view.visible);    Viewcompat.animate (TV). Alpha (1). Start ();//Set Transparency to 1}}). Show (); } @Override PublIC void ondragstatechanged (int state) {}} 

  

Package Com.example.nanchen.designcoodinatordemo;import Android.content.context;import Android.support.v7.widget.recyclerview;import Android.view.viewgroup;import Android.widget.textview;import java.util.list;/** * Created by South Dust on 16-7-14. */public class Myadapter extends recyclerview.adapter<recyclerview.viewholder> {private list<string> List    ;    Private context context;        Public Myadapter (list<string> list, context context) {this.list = list;    This.context = context; } @Override Public Recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) {TextView text        View = new TextView (context);    return new Recyclerview.viewholder (TextView) {}; } @Override public void Onbindviewholder (recyclerview.viewholder holder, int position) {(TextView) holder.i    Temview). SetText (List.get (position));    } @Override public int getitemcount () {return list.size (); }}

  

<?xml version= "1.0" encoding= "Utf-8"?>
<android.support.design.widget.coordinatorlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
xmlns:app= "Http://schemas.android.com/apk/res-auto"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "Com.example.nanchen.designcoodinatordemo.MainActivity" >


<android.support.v7.widget.recyclerview
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
App:layoutmanager= "Linearlayoutmanager"
Android:id= "@+id/main_recycler"/>


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "30SP"
Android:background= "#f00"
Android:clickable= "true"
android:layout_gravity= "Center"
Android:id= "@+id/main_tv"
App:layout_behavior= "Com.example.nanchen.designcoodinatordemo.MyBehavior"
android:text= "Hello world!" />


</android.support.design.widget.CoordinatorLayout>

Android Design Pack's super-strong controls Coordinatorlayout and Snackbar simple use

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.