Android Material Design:基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出

activity_main.xml:
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" > <android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <!-- app:layout_scrollFlags="scroll|enterAlways" -->
<!-- 这句话的作用:向上滑动的时候Toolbar消失,向下滑的时候ToolBar立即出现 -->
<!-- 如果没有scroll的话向上滑动Toolbar不会消失 -->
<!-- 如果没有enterAlways的话向下滑动Toolbar不会立即出现,会有短暂的延迟 --> <!-- android:minHeight="?attr/actionBarSize"(默认为此) 设置Toolbar的NavigationIcon位置 --> <android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#9C27B0"
android:minHeight="?attr/actionBarSize" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可以添加多个view,会挤压标题栏"
android:textColor="#E91E63" />
</android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0"
app:tabIndicatorColor="#ef5350"
app:tabSelectedTextColor="#1976d2"
app:tabTextColor="#90caf9" />
</android.support.design.widget.AppBarLayout> <!-- app:layout_behavior="@string/appbar_scrolling_view_behavior"的作用是使上方两个数据不被AppBarLayout遮盖 --> <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dip"
android:layout_marginRight="10dip"
android:src="@drawable/ic_launcher"
app:backgroundTint="#e57373"
app:borderWidth="10dp"
app:elevation="10dip"
app:fabSize="normal"
app:pressedTranslationZ="10dp"
app:rippleColor="#c62828" /> </android.support.design.widget.CoordinatorLayout>
MainActivity.java:
package com.example.testappbarlayout_zzw; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Toolbar mToolbar=(Toolbar) findViewById(R.id.toolBar);
mToolbar.setLogo(R.drawable.ic_launcher);
mToolbar.setNavigationIcon(R.drawable.ic_launcher);
mToolbar.setTitle("大家好");
mToolbar.setSubtitle("我是xxx"); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
for (int i = 0; i < 10; i++)
tabLayout.addTab(tabLayout.newTab().setText("选项" + i));
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); RecyclerView mRecyclerView=(RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager mLayoutManager=new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayout.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
MyRecyclerViewAdapter adapter=new MyRecyclerViewAdapter(this);
mRecyclerView.setAdapter(adapter); } private class MyViewHolder extends ViewHolder{ public TextView text; public MyViewHolder(View itemView) {
super(itemView);
text=(TextView) itemView.findViewById(android.R.id.text1);
}
} private class MyRecyclerViewAdapter extends Adapter<MyViewHolder>{ private LayoutInflater inflater; public MyRecyclerViewAdapter(Context context) {
inflater=LayoutInflater.from(context);
} @Override
public int getItemCount() { return 100;
} @Override
public void onBindViewHolder(MyViewHolder viewHolder, int position) { viewHolder.text.setText("测试数据:"+position);
} @Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) { View v=inflater.inflate(android.R.layout.simple_list_item_1,null);
MyViewHolder holder=new MyViewHolder(v); return holder;
} } }
可能出现的相关问题解决:
使用android.support.v7.widget.RecyclerView出现 java.lang.reflect.InvocationTargetException:http://www.cnblogs.com/zzw1994/p/5004564.html
使用android.support.design出现java.lang.reflect.InvocationTargetException:http://www.cnblogs.com/zzw1994/p/5012467.html
Android Material Design:基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出的更多相关文章
- Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏ToolBar
ok,今天继续更新Material Design系列!!! 废话不说,先看看效果图吧: 好了,现在来讲讲上图是怎么实现的吧!讲之前先讲讲几个控件: CoordinatorLayout 该控件也是De ...
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...
- Android Material Design 兼容库的使用
Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...
- Android Material Design简单使用
吐槽 作为一个 Android developer,没有什么比拿着 UI 设计的一堆 iOS 风格的设计 来做需求更恶心的了,基本所有空间都要照着 iOS 来画一遍,Material Design 辣 ...
- Android material design support library -- CollapsingToolbarLayout简介
本文是codetrick上material design support library教程的第三篇,主要讲的是CollapsingToolbarLayout的概念和应用方法. 原文链接:Materi ...
- Android Material Design之CollapsingToolbarLayout使用
CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含在Collapsin ...
- Android Material Design NavigationView 及 Palette 颜色提取器
DrawerLayout + NavigationView DrawerLayout布局,通常在里面添加两个子控件,程序主界面添加到NavitagionView前面. <android.supp ...
- 使用 CoordinatorLayout 出错 inflating class android.support.design.widget.CoordinatorLayout
ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.czr.ianpu/com.czr.ianpu.MainAc ...
- Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决
Android Material Design Ripple Effect在Android5.0(SDK=21)以下Android版本崩溃问题解决 附录1的Android Ripple Effect水 ...
随机推荐
- CodeForces 617C【序枚举】
题意: 有两个点喷水,有很多个点有花,给出坐标. 求使得每个花都可以被喷到,两个喷水的半径的平方的和最小是多少. 思路: 枚举其中一个喷水的最大半径. 坑: 这题我贪心的思路有很大问题.一开始也是想这 ...
- nyoj 90 整数划分
点击打开链接 整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+-+nk, 其中n1≥n2≥-≥nk≥1,k≥ ...
- SQL执行的原理以及一些常见的关键字
sql语句在面试里面问道的问题: sql的解析的顺序 1.where里面的条件是从右向左扫描解析 2.from里面的大表在前,小表在后,解析的顺序是从右向左解析. 3.left/right/inner ...
- JAVA设计模式之单一职责原则
概念: 就一个类而言应该只有一个因其他变化的原因. 流程: 问题由来:设类或接口类C负责两个不同不同的职责:职责T1,职责T2.当由于职责T1需求改变进而需要修改类C时,可能导致职责T2收到不可预知的 ...
- 用sql语句生成sqlserver数据库表的数据字典
THEN O.name ELSE N'' END, 表描述 THEN PTB.[value] END,N''), 字段序号=C.column_id, 字段名称=C.name, 字段描述=ISNULL( ...
- Bootstrap 3 How-To #1 下载与配置
Bootstrap 3 发布了,通过简单的几步,我们就可以使用 Bootstrap 的样式表,图标,以及 javascript 来配置一个简单的站点. 准备 Bootstrap 不仅仅是一个代码集,还 ...
- linux 关闭系统提示声音
关闭Linux 提示声音: rmmod pcspkr //永久关闭 在/etc/modprobe.d/blacklist文件最后加上 blacklist pcspkr
- Ajax-(get/post/jQuery方式请求)
< !DOCTYPE html > < html xmlns = "http://www.w3.org/1999/xhtml" > < head &g ...
- jquery递归遍历xml文件,形成ul-li序列,生成树结构(使用了treeview插件)
treeview插件从这里获得,下载的文件中有demo,看demo文件夹里面的index.html文件就差不多知道如何使用该控件了,在我做的项目里用到的部分代码截图如下(在引用下面的js文件前要先引用 ...
- phonegap android3.5.1 Crosswalk
1. your phonegap platform for android update 3.5.1 cordova platform add android@3.5 2. download cros ...