Android开发-DesignDemo-AndroidStudio(九)FloatingActionButton(1)

简单对比FloatingActionButton和ImageButton的区别:

左边是ImageButton,右边是FloatingActionButton:

activity_main.xml:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.iwanghang.coordinatordemo.MainActivity">

    <!--layout_scrollFlags enterAlwaysCollapsed 第一条上拉至第一条数据才出现ActionBar-->
    <!--layout_scrollFlags enterAlways 只要上拉就出现ActionBar-->
    <!--contentScrim 遮蔽颜色-->
    <!--layout_collapseMode parallax 视差-->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/Widget.AppCompat.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                app:layout_collapseMode="parallax"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize" />
        </android.support.design.widget.CollapsingToolbarLayout>
        <!--<TextView-->
            <!--android:text="悬停条目"-->
            <!--android:textColor="#ffffff"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content" />-->
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="LinearLayoutManager" />

    <!--<TextView-->
        <!--android:clickable="true"-->
        <!--android:id="@+id/textView_1"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_marginTop="100dp"-->
        <!--android:layout_marginLeft="100dp"-->
        <!--android:textSize="20sp"-->
        <!--android:background="#f00"-->
        <!--android:text="textView_1\nlist上下滚动显示隐藏\n通过setBehavior实现" />-->

    <TextView
        android:clickable="true"
        android:id="@+id/textView_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:background="#f00"
        android:text="textView_2\n向右滑动" />

    <TextView
        app:layout_behavior="com.iwanghang.coordinatordemo.MyBehavior"
        android:clickable="true"
        android:id="@+id/textView_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"
        android:textSize="20sp"
        android:background="#f00"
        android:text="textView_3\nlist上下滚动显示隐藏\n指定layout_behavior实现" />

    <!--右下角 FloatingActionButton-->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="end|bottom"/>
    <!--左下角 ImageButton-->
    <ImageButton
        android:id="@+id/imageBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="start|bottom"/>

</android.support.design.widget.CoordinatorLayout>
MainActivity.java:

package com.iwanghang.coordinatordemo;

import android.support.design.widget.CoordinatorLayout;
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.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
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 {
    private TextView textView_1;
    private TextView textView_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //textView_1 = (TextView) findViewById(R.id.textView_1);
        textView_2 = (TextView) findViewById(R.id.textView_2);

        // 手动强转控件类型
        //CoordinatorLayout.LayoutParams params_1 =
        //        (CoordinatorLayout.LayoutParams) textView_1.getLayoutParams();
        CoordinatorLayout.LayoutParams params_2 =
                (CoordinatorLayout.LayoutParams) textView_2.getLayoutParams();

        // setBehavior 设置控件的行为
        //params.setBehavior();
        // SwipeDismissBehavior 实例化滑动删除行为
        SwipeDismissBehavior<View> behavior = new SwipeDismissBehavior<>();

        // 滑动删除行为监听
        behavior.setListener(new SwipeDismissBehavior.OnDismissListener() {
            @Override
            public void onDismiss(View view) { // 当控件消失
                // 在设置控件隐藏之前,其实控件只是透明,还可以滑动回来
                view.setVisibility(View.GONE); // 设置控件隐藏

                Snackbar.make(view,"删除控件",Snackbar.LENGTH_LONG)
                        .setAction("撤销", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                textView_2.setVisibility(View.VISIBLE); // 设置控件显示
                                ViewCompat.animate(textView_2).alpha(1).start();
                            }
                        }).show();
            }

            @Override
            public void onDragStateChanged(int state) { // 当控件停止

            }
        });

        // 把滑动删除行为设置到textView控件上
        params_2.setBehavior(behavior);











        RecyclerView recycler_1 = (RecyclerView) findViewById(R.id.recycler_1);
        List<String> list = new ArrayList<>();
        for (int i = 0; i <100 ; i++) {
            list.add(String.format(Locale.CHINA,"第%03d条数据",i+1));
        }
        recycler_1.setAdapter(new MyAdapter(this,list));

        MyBehavior myBehavior = new MyBehavior();
        //params_1.setBehavior(myBehavior);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);





        // 右下角悬浮按钮 点击事件监听 FloatingActionButton
        findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view,"FloatingActionButton Click",Snackbar.LENGTH_LONG).show();
            }
        });
        // 左下角悬浮钮 点击事件监听 ImageButton
        findViewById(R.id.imageBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view,"ImageButton Click",Snackbar.LENGTH_LONG).show();
            }
        });



    }
}

你可能感兴趣的:(Android开发,imagebutton,design,androidstudio)