DrawerLayout 其实是一个布局控件,跟LinerLayout 等控件是一种东西,但是DrawerLayout带有滑动的功能,只要按照drawerLayout 的规定布局方式写完布局,就能有侧滑的效果。
例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:material="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DADADA"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/headerbar_height"
android:background="@color/headbar_bg"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.lion.material.widget.LDrawerButton
android:id="@+id/header_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
material:widget_animColor="@color/color_anim_white"
material:widget_delayclick="false"
material:widget_type="left" />
<TextView
style="@style/headerbar_text_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ldrawer_title" />
<com.lion.material.widget.LDrawerButton
android:id="@+id/header_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
material:widget_animColor="@color/color_anim_white"
material:widget_delayclick="false"
material:widget_type="right" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.lion.material.widget.PreferenceNormal
android:id="@+id/main_item_lbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/lbutton_title" />
<com.lion.material.widget.PreferenceNormal
android:id="@+id/main_item_limagebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/limagebutton_title" />
<com.lion.material.widget.PreferenceNormal
android:id="@+id/main_item_lframelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/lframelayout_title" />
<com.lion.material.widget.PreferenceNormal
android:id="@+id/main_item_lpreference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="false"
material:preference_title="@string/lpreference_title" />
<com.lion.material.widget.PreferenceNormal
android:id="@+id/main_item_ldialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="false"
material:preference_title="@string/ldialog_title" />
<com.lion.material.widget.PreferenceNormal
android:id="@+id/main_item_ltab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="false"
material:preference_title="@string/ltab_title" />
</LinearLayout>
</ScrollView>
<com.lion.material.widget.LImageButton
android:id="@+id/main_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="25dp"
android:layout_marginRight="25dp"
material:widget_animColor="@color/color_anim_black"
material:widget_background="@drawable/icon_add" />
</FrameLayout>
<LinearLayout
android:id="@+id/main_menu_left"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:orientation="vertical" >
<com.lion.material.widget.PreferenceNormal
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/ldrawer_item1" />
<com.lion.material.widget.PreferenceNormal
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/ldrawer_item2" />
<com.lion.material.widget.PreferenceNormal
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/ldrawer_item3" />
<com.lion.material.widget.PreferenceNormal
android:layout_width="match_parent"
android:layout_height="wrap_content"
material:preference_show_divider="true"
material:preference_title="@string/ldrawer_item4" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_menu_right"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#00ffff"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/ldrawer_right_content"
android:textColor="@color/color_black87" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
有两点要注意:主内容的布局代码要放在侧滑布局的前面,这可以帮助DrawerLayout 判断谁是侧滑菜单,谁是主内容区;侧滑
菜单的部分的布局 可以设置layout_gravity 属性,他表示侧滑菜单是在左边还是右边
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerListener(new DrawerListener() {
@Override
public void onDrawerStateChanged(int drawerStatus) {
}
@Override
public void onDrawerSlide(View v, float progress) {
if (v == mLeftGravityView) {
drawerButtonLeft.onDrag(
mDrawerLayout.isDrawerOpen(Gravity.LEFT), progress);
} else {
drawerButtonRight.onDrag(
mDrawerLayout.isDrawerOpen(Gravity.RIGHT), progress);
}
}
@Override
public void onDrawerOpened(View v) {
}
@Override
public void onDrawerClosed(View v) {
}
});
}按钮点击有个渐变色
package com.lion.material.widget;
import com.lion.material.extra.IMaterial;
import com.lion.material.extra.MaterialStyle;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class LButton extends Button implements IMaterial {
private MaterialStyle mMaterialStyle;
public LButton(Context context) {
this(context, null);
}
@SuppressLint("NewApi")
public LButton(Context context, AttributeSet attrs) {
super(context, attrs);
mMaterialStyle = new MaterialStyle(this, attrs);
}
public void perfirmSuperClick() {
super.performClick();
}
public void setColor(int fullColor) {
mMaterialStyle.setColor(fullColor);
}
public void setType(int widgetType) {
mMaterialStyle.setType(widgetType);
}
@Override
protected void onDraw(Canvas canvas) {
if (!isEnabled()) {
canvas.drawColor(0xffCFCFCF);
super.onDraw(canvas);
return;
}
mMaterialStyle.doDraw(canvas);
super.onDraw(canvas);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean performClick() {
if (mMaterialStyle != null) {
mMaterialStyle.performClick();
} else {
return super.performClick();
}
return true;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mMaterialStyle != null)
mMaterialStyle.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if (mMaterialStyle != null)
mMaterialStyle.onWindowFocusChanged(hasWindowFocus);
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (mMaterialStyle != null)
mMaterialStyle.onVisibilityChanged(changedView, visibility);
}
/**
* set click event delay to animation end
*
* @param need
*/
public void setDelayClick(boolean delayClick) {
mMaterialStyle.setDelayClick(delayClick);
}
@Override
public void setBackgroundResource(int resId) {
setBackground(this.getResources().getDrawable(resId));
}
@Override
public void setBackground(Drawable background) {
if (mMaterialStyle != null)
mMaterialStyle.setBackground(background);
}
@Override
public void setBackgroundColor(int color) {
if (mMaterialStyle != null)
mMaterialStyle.setBackgroundColor(color);
}
@Override
@Deprecated
public void setBackgroundDrawable(Drawable background) {
// super.setBackgroundDrawable(background);
this.setBackground(background);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMaterialStyle != null) {
if (mMaterialStyle.needBackgroundMeasure()) {
int[] size = mMaterialStyle.getMeasureSize(widthMeasureSpec,
heightMeasureSpec);
setMeasuredDimension(size[0], size[1]);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (!isEnabled())
return false;
return super.dispatchTouchEvent(event);
}
}
按钮可用与不可用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:material="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/headerbar_height"
android:background="@color/headbar_bg"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.lion.material.widget.LButton
android:id="@+id/header_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/lbutton_back"
android:textColor="@color/color_normal"
material:widget_type="left" />
<TextView
style="@style/headerbar_text_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/lbutton_title" />
<com.lion.material.widget.LButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:maxWidth="100dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="@string/lbutton_menu"
android:textColor="@color/color_normal"
material:widget_animColor="@color/color_anim_white"
material:widget_type="right" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:textColor="@color/color_normal"
android:text="@string/lbutton_sytem_style"
android:textSize="15sp" />
<com.lion.material.widget.LButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:gravity="center"
android:text="@string/lbutton_enabled"
android:textColor="@color/color_normal"
android:textSize="15sp"
material:widget_animColor="#1a000000"
material:widget_background="#673ab7" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="@string/lbutton_wicth_background_color"
android:textColor="@color/color_normal" />
<com.lion.material.widget.LButton
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:padding="20dp"
android:textColor="@color/color_normal"
android:textSize="20sp"
material:widget_background="@drawable/icon_add" />
<com.lion.material.widget.LButton
android:id="@+id/button4"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="@string/lbutton_change_color"
android:textSize="20sp"
android:textColor="@color/color_normal"
material:widget_animColor="#1a000000" />
<com.lion.material.widget.LButton
android:layout_width="60dp"
android:layout_height="200dp"
android:gravity="center"
android:textColor="@color/color_normal"
android:text="@string/hello_world"
android:textSize="20sp"
material:widget_animColor="#bbff2d6f" />
</LinearLayout>
</ScrollView>
</LinearLayout>package com.lion.material.demo.activity;
import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.lion.material.demo.R;
import com.lion.material.widget.LButton;
public class LButtonActivity extends BaseActivity implements OnClickListener {
private Button button1;
private LButton button2;
private LButton button3;
private LButton button4;
private Random mRandom = new Random();
private int[] randomColor = new int[] { 0x1d000000, 0x1d00ff00, 0xff00ffff,
0xffffff00, 0x1dff0000 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lbutton);
initView();
}
private void initView() {
findViewById(R.id.header_left).setOnClickListener(this);
button1 = (Button) findViewById(R.id.button1);
button2 = (LButton) findViewById(R.id.button2);
button3 = (LButton) findViewById(R.id.button3);
button4 = (LButton) findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.header_left:
finish();
break;
case R.id.button1:
Toast.makeText(mContext, R.string.lbutton_toast_system_button,
Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
case R.id.button3:
button2.setEnabled(!button2.isEnabled());
button2.setText(button2.isEnabled() ? R.string.lbutton_enabled
: R.string.lbutton_disabled);
break;
case R.id.button4:
if (v instanceof LButton) {
int fullColor = randomColor[mRandom.nextInt(randomColor.length)];
((LButton) v).setColor(fullColor);
}
break;
default:
break;
}
}
}
RecyclerView 的使用
/*
* Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package vee.android.sample.recyclerview;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import vee.android.sample.recyclerview.ListAdapterHolder.OnItemClickListener;
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {
FragmentActivity mActivity;
RecyclerView mRecyclerView;
ListAdapterHolder adapter;
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (FragmentActivity) activity;
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
adapter = new ListAdapterHolder(mActivity);
return rootView;
}
@Override
public void onViewCreated(View view , Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView.setAdapter(adapter);
////如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
adapter.SetOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View v , int position) {
// do something with position
}
});
}
}
/*
* Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package vee.android.sample.recyclerview;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ListAdapterHolder extends RecyclerView.Adapter<ListAdapterHolder.ViewHolder> {
private final FragmentActivity mActivity;
private final List<UserDetails> mUserDetails = new ArrayList<UserDetails>();
OnItemClickListener mItemClickListener;
public ListAdapterHolder(FragmentActivity mActivity) {
this.mActivity = mActivity;
createUserDetails();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
final View sView = mInflater.inflate(R.layout.single_list_item, parent, false);
return new ViewHolder(sView);
}
@Override
public void onBindViewHolder(ViewHolder holder , int position) {
holder.vId.setText("ID: " + mUserDetails.get(position).getId());
holder.vName.setText("Name: " + mUserDetails.get(position).getName());
holder.vSex.setText("Sex: " + mUserDetails.get(position).getSex());
holder.vAge.setText("Age: " + mUserDetails.get(position).getAge());
}
@Override
public int getItemCount() {
return mUserDetails.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView vName, vSex, vId, vAge;
public ViewHolder(View view) {
super(view);
vId = (TextView) view.findViewById(R.id.list_id);
vName = (TextView) view.findViewById(R.id.list_name);
vSex = (TextView) view.findViewById(R.id.list_sex);
vAge = (TextView) view.findViewById(R.id.list_age);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
}
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
/* ==========This Part is not necessary========= */
/**
* Create Random Users
*/
private void createUserDetails() {
for (int i = 0; i < 100; i++) {
final UserDetails mDetails = new UserDetails();
mDetails.setId(i);
mDetails.setName("Name " + i);
mDetails.setSex((i % 2) == 0 ? "M" : "F");
mDetails.setAge(randInt(14, 50));
mUserDetails.add(mDetails);
}
}
/*
* Snippet from http://stackoverflow.com/a/363692/1008278
*/
public static int randInt(int min , int max) {
final Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
/* ==========This Part is not necessary========= */
}
CoordinatorLayout,顾名思义,它是用来组织它的子views之间协作的一个父view。CoordinatorLayout默认情况下可理解是一个FrameLayout,它的布局方式默认是一层一层叠上去。
其中有众多的控件,其中最复杂,功能最强大的就是CoordinatorLayout,顾名思义,它是用来组织它的子views之间协作的一个父view。CoordinatorLayout默认情况下可理解是一个FrameLayout,它的布局方式默认是一层一层叠上去。
那么,CoordinatorLayout的神奇之处就在于Behavior对象了
看下CoordinatorLayout.Behavior对象的 Overview
可知Behavior对象是用来给CoordinatorLayout的子view们进行交互用的。
Behavior接口拥有很多个方法,我们拿AppBarLayout为例。AppBarLayout中有两个Behavior,一个是拿来给它自己用的,另一个是拿来给它的兄弟结点用的,我们重点关注下AppBarLayout.ScrollingViewBehavior这个类。
http://www.itnose.net/detail/6292646.html
总结:
可以看到CoodinatorLayout给我们实现了一个可以被子view代理实现方法的一个布局。这和传统的ViewGroup不同,子view从此知道了彼此之间的存在,一个子view的变化可以通知到另一个子view。CoordinatorLayout所做的事情就是当成一个通信的桥梁,连接不同的view。使用Behavior对象进行通信。
我们具体的实现可以参照 Android官方文档告诉我们的每一个方法的作用 进行重写,实现自己想要的各种复杂的功能。
CoordinatorLayout 与 FloatingActionButton 的使用
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|right"
android:layout_margin="16dp"
app:layout_behavior="com.getbase.coordinatorlayoutdemo.FloatingActionButtonBehavior"
app:fab_icon="@drawable/ic_done" />
</android.support.design.widget.CoordinatorLayout>
package com.getbase.coordinatorlayoutdemo;
import com.getbase.floatingactionbutton.FloatingActionButton;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar.SnackbarLayout;
import android.util.AttributeSet;
import android.view.View;
public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return dependency instanceof SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
}package com.getbase.coordinatorlayoutdemo;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hello Snackbar", Snackbar.LENGTH_LONG).show();
}
});
}
}
其中,一个可以滚动的组件,例如RecyclerView、ListView(这里需要注意的是,貌似只支持RecyclerView、ListView,如果你用一个ScrollView,是没有效果的)。如果:
1、给这个可滚动组件设置了layout_behavior
2、给另一个控件设置了layout_scrollFlags
那么,当设置了layout_behavior的控件滑动时,就会触发设置了layout_scrollFlags的控件发生状态的改变。
有一件事情必须注意,那就是CoordinatorLayout并不知道FloatingActionButton或者AppBarLayout的内部工作原理,它只是以Coordinator.Behavior的形式提供了额外的API,该API可以使子View更好的控制触摸事件与手势以及声明它们之间的依赖,并通过onDependentViewChanged()接收回调。
可以使用CoordinatorLayout.DefaultBehavior(你的View.Behavior.class)注解或者在布局中使用app:layout_behavior=”com.example.app.你的View$Behavior”属性来定义view的默认行为。 framework让任意View和CoordinatorLayout结合在一起成为了可能。
AppBarLayout跟它的名字一样,把容器类的组件全部作为AppBar
Design 包中的 TabLayout 既然实现了固定 Tab ,也实现了滚动 Tab 。对于前者,视图的宽度在所有 Tab 之间等分,而对于后者, Tab 并没有统一的尺寸,而且可以横向滚动。 Tab 可以通过代码添加:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="24dp"
android:src="@mipmap/ic_add_black_24dp" />
</android.support.design.widget.CoordinatorLayout>
mTabLayout = (TabLayout) findViewById(R.id.tabs);
List<String> titles = new ArrayList<>();
titles.add("Page One");
titles.add("Page Two");
titles.add("Page Three");
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)));
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(2)));NavigationView
用于侧滑菜单中的menu布局。之前Google在V4包中推出自己的 DrawerLayout作为抽屉侧滑菜单,标准使用方法可以参考 google 原生态 抽屉式侧滑菜单 Android DrawerLayout 布局的使用介绍。
当时的官方布局是这样的:
<!-- the main content view -->
<framelayout android:id="@+id/frame_content" android:layout_height="match_parent" android:layout_width="match_parent">
</framelayout>
<!-- the navigetion view -->
<listview android:background="#9999cc" android:choicemode="singleChoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:id="@+id/drawer_list" android:layout_gravity="start" android:layout_height="match_parent" android:layout_width="240dp">
</listview>
</android.support.v4.widget.drawerlayout>
其实这次谷歌只是将上面的ListView布局替换成NavigationView了。简化了之前ListView写适配器的繁琐。
其中NavigationView 中的 android:layout_gravity=”start” 属性来控制抽屉菜单从哪边滑出,一般“start ”从左边滑出,“end”从右边滑出。
这里最主要的两个属性分别是:
1.app:headerLayout: 给NavigationView添加头部布局
2.app:menu:给NavigationView添加menu菜单布局
http://www.2cto.com/kf/201506/409067.html
http://www.2cto.com/kf/201506/409067.html
为了使得Toolbar可以滑动,我们必须还得有个条件,就是CoordinatorLayout布局下包裹一个可以滑动的布局,比如 RecyclerView,NestedScrollView(经过测试,ListView,ScrollView不支持)具有滑动效果的组件。并且给这些组件设置如下属性来告诉CoordinatorLayout,该组件是带有滑动行为的组件,然后CoordinatorLayout在接受到滑动时会通知AppBarLayout
中可滑动的Toolbar可以滑出屏幕了。
RecyclerView:ListView的升级版,它提供了更好的性能而且更容易使用。该控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动。当你list中的元素经常动态改变时可以使用RecyclerView控件。它提供了如下两个功能:
1、为每个条目位置提供了layout管理器(RecyclerView.setLayoutManager)
2、为每个条目设置了操作动画(RecyclerView.setItemAnimator)
CardView:Google提供的一个卡片式视图组件。CardView继承自FrameLayout,允许你在Card视图中显示信息, CardView也可以设置阴影和圆角。(其实现在很多应用都自定义了Card视图,Google这回将Card视图作为基本控件,可以拿来直接使用了。)
值得注意的是RecyclerView的添加删除都是有默认的动画效果的,如果没有效果可以添加如下代码:
|
1
|
mRecyclerView.setItemAnimator(newDefaultItemAnimator()); |
setDisplayHomeAsUpEnabled // 给左上角图标的左边加上一个返回的图标
TabLayout
tabLayout.setTabTextColors(Color.WHITE,
Color.GRAY);//设置文本在选中和为选中时候的颜色
本文详细解析了如何在DrawerLayout中集成滑动组件,包括如何配置侧滑菜单、滑动按钮的渐变色效果以及如何实现按钮的可用性切换。同时介绍了RecyclerView、CoordinatorLayout与FloatingActionButton的使用方法,以及如何与AppBarLayout配合实现更丰富的UI交互。通过实例代码展示,深入浅出地阐述了各个组件的集成与应用。
128

被折叠的 条评论
为什么被折叠?



