在前面的Design中,学习使用了TabLayout,NavigationView与DrawerLayout实现的神奇效果,今天就带来本次Design包中我认为最有意义的控件CoordinatorLayout。

当然还有SnackBar,不过他在实际运用中一般都是和CoordinatorLayout搭配使用的。

先说下SnackBar,这个控件其实我觉得和Toast没什么差别,不过功能上的确有增强。这个控件可以通过setAction方法设置类似按钮一样的东西。而且这个东西可以设置多个。

重点还是我们的CoordinatorLayout控件,这是一个增强型的FrameLayout,功能非常强大,可以给它的子布局增加很多有意思的东西。

它通常和Behavior联合在一起使用。

项目已同步至github:https://github.com/nanchen2251/CoordinatorLayout(包括了下一节内容的源代码)

先上一波运行图:

可以在Behavior中通过监听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; /**
* 自定义Behavior
* Created by 南尘 on 16-7-14.
*/
public class MyBehavior extends CoordinatorLayout.Behavior { //写了这个构造方法才能在XML文件中直接指定
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
return true;//返回true代表我们关心这个滚动事件
} @Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
if (dy < 0) {//向下滚动
ViewCompat.animate(child).scaleX(1).alpha(1).start();
} else {//向上滚动
ViewCompat.animate(child).scaleX(0).alpha(0).start();
}
}
}

  这样在下拉的时候就会消失。

值得注意的是,若是想在XML中显示指定Behavior的话,必须重写构造方法

public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

  

其他的一些代码

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 < 100; i++) {
list.add(String.format(Locale.CHINA,"第%03d行",i));
}
MyAdapter adapter = new MyAdapter(list,this);
recyclerView.setAdapter(adapter); // CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) tv.getLayoutParams();
// SwipeDismissBehavior<TextView> behavior = new SwipeDismissBehavior<>();
// behavior.setListener(this);//设置一个监听
// params.setBehavior(behavior);//设置一个行为
// MyBehavior behavior = new MyBehavior();
// params.setBehavior(behavior);
} @Override
public void onDismiss(View view) {
view.setVisibility(View.GONE);
Snackbar.make(view,"删除了一个控件!",Snackbar.LENGTH_SHORT)
.setAction("撤销", new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setVisibility(View.VISIBLE);
ViewCompat.animate(tv).alpha(1).start();//把透明度设置为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 南尘 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 textView = new TextView(context);
return new RecyclerView.ViewHolder(textView) {};
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((TextView) holder.itemView).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>

安卓Design包之超强控件CoordinatorLayout与SnackBar的简单使用的更多相关文章

  1. 安卓Design包之TabLayout控件的使用

    转自: 安卓Design包之TabLayout控件的简单使用 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android ...

  2. 安卓Design包之Toolbar控件的使用

    转自:ToolBar的使用 ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话, ...

  3. 安卓Design包之TabLayout控件的简单使用

    Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...

  4. 安卓Design包之CollapsingToolbarLayout(可折叠的工具栏布局)的简单使用

    转自: CollapsingToolbarLayout的使用 注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 CollapsingToolbarLayout作用是提供了一个 ...

  5. 安卓Design包下的TextInputLayout和FloatingActionButton的简单使用

    终于介绍到Design包的最后的东西了. 也很简单,一个是TextInputLayout. TextInputLayout作为一个父容器,包含一个新的EditText,可以给EditText添加意想不 ...

  6. iOS 开发 ZFUI framework控件,使布局更简单

    来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...

  7. 安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用

    转载: CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用 控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个V ...

  8. 安卓Design包之NavigationView结合DrawerLayout,toolbar的使用,FloatingActionButton

    注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 FloatingActionButton 悬浮按钮:FloatingActionButton是重写ImageView的,所有 ...

  9. 安卓Design包之AppBar和Toolbar的联用

    前面讲了Design包的的CoordinatorLayout和SnackBar的混用,现在继续理解Design包的AppBar; AppBarLayout跟它的名字一样,把容器类的组件全部作为AppB ...

随机推荐

  1. Solr_全文检索引擎系统

    Solr介绍: Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务.Solr可以独立运行在Jetty.Tomcat等这些Servlet容器中. Solr ...

  2. React的使用与JSX的转换

    前置技能:Chrome浏览器   一.拿糖:React的使用 React v0.14 RC 发布,主要更新项目: 两个包: React 和 React DOM DOM node refs 无状态的功能 ...

  3. Hawk 7. 常见问题

    本页面您可以通过关键字搜索来获取信息. 理性使用爬虫 爬虫是一种灰色的应用,虽然作为Hawk的设计者,但我依然不得不这么说. 各大网站都在收集和整理数据上花费了大量的精力,因此抓取的数据应当仅仅作为科 ...

  4. 移动端访问PC站点时自动跳转至移动站点

    方法一: 百度Site APP的uaredirect.js 实现手机访问,自动跳转 <script src="http://siteapp.baidu.com/static/webap ...

  5. JQuery 复制粘贴上传图片插件(textarea 和 tinyMCE)

    开源地址:https://github.com/yuezhongxin/paste-upload-image.js 支持 Ctrl+C/Ctrl+V 上传,支持拖拽上传,也支持 QQ/微信截图上传. ...

  6. C#数组,List,Dictionary的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  7. Oracle学习之路-- 案例分析实现行列转换的几种方式

    注:本文使用的数据库表为oracle自带scott用户下的emp,dept等表结构. 通过一个例子来说明行列转换: 需求:查询每个部门中各个职位的总工资 按我们最原始的思路可能会这么写:       ...

  8. ntp

    一: 在一台可以连接外网的服务器A上配置ntp: 配置  /etc/ntp.conf  文件: server 202.120.2.101            # local clock (LCL) ...

  9. Spring注解

    AccountController .java Java代码   1.        /** 2.         * 2010-1-23 3.         */ 4.        packag ...

  10. 极光推送和友盟推送,ios端和安卓端的后端调试设置

    我是最后端的,这两天搞了一个app项目,前端安卓使用友盟很方便,调试比较顺利,然后ios就遇到各种问题了,证书.发送成功推送不成功,测试时用的TestMode(),ios上架之后就必须用product ...