From 7dec9d366c9479c56e3198faf0f5a17e479c51b8 Mon Sep 17 00:00:00 2001 From: Yuichi Araki Date: Tue, 1 Dec 2015 18:15:01 +0900 Subject: [PATCH] Add use of NavigationView without drawer Change-Id: Ic6344e7cbfad0d37726d3c2ee4d070f133674033 --- .../SupportDesignDemos/AndroidManifest.xml | 9 ++ .../design_navigation_without_drawer.xml | 40 +++++++++ .../SupportDesignDemos/res/values/strings.xml | 1 + .../design/widget/NavigationViewUsage.java | 70 ++++------------ .../widget/NavigationViewUsageBase.java | 84 +++++++++++++++++++ .../widget/NavigationViewWithoutDrawer.java | 44 ++++++++++ 6 files changed, 194 insertions(+), 54 deletions(-) create mode 100644 samples/SupportDesignDemos/res/layout/design_navigation_without_drawer.xml create mode 100644 samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsageBase.java create mode 100644 samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewWithoutDrawer.java diff --git a/samples/SupportDesignDemos/AndroidManifest.xml b/samples/SupportDesignDemos/AndroidManifest.xml index 3a4dbdf04..07935ae61 100644 --- a/samples/SupportDesignDemos/AndroidManifest.xml +++ b/samples/SupportDesignDemos/AndroidManifest.xml @@ -55,6 +55,15 @@ + + + + + + + diff --git a/samples/SupportDesignDemos/res/layout/design_navigation_without_drawer.xml b/samples/SupportDesignDemos/res/layout/design_navigation_without_drawer.xml new file mode 100644 index 000000000..31cf5dee0 --- /dev/null +++ b/samples/SupportDesignDemos/res/layout/design_navigation_without_drawer.xml @@ -0,0 +1,40 @@ + + + + + + + + + diff --git a/samples/SupportDesignDemos/res/values/strings.xml b/samples/SupportDesignDemos/res/values/strings.xml index c3a981a02..35f5cbc87 100644 --- a/samples/SupportDesignDemos/res/values/strings.xml +++ b/samples/SupportDesignDemos/res/values/strings.xml @@ -19,6 +19,7 @@ Floating Action Button/Basic NavigationView/Usage + NavigationView/Without drawer TabLayout/Usage Text Input BottomSheet/Usage diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsage.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsage.java index c58e49e12..9404cc4f3 100644 --- a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsage.java +++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsage.java @@ -22,30 +22,23 @@ import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; -import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.TypedValue; import android.view.MenuItem; import android.view.View; -import android.widget.TextView; -import android.widget.Toast; /** * This demonstrates basic usage of NavigationView */ -public class NavigationViewUsage extends AppCompatActivity { +public class NavigationViewUsage extends NavigationViewUsageBase { private DrawerLayout mDrawerLayout; - private TextView mTextMessage; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.design_navigation); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); - mTextMessage = (TextView) findViewById(R.id.message); // Set the color of status bar TypedValue value = new TypedValue(); @@ -64,56 +57,25 @@ public class NavigationViewUsage extends AppCompatActivity { mDrawerLayout.openDrawer(GravityCompat.START); } }); - - // Menu - NavigationView navigation = (NavigationView) findViewById(R.id.navigation); - navigation.setNavigationItemSelectedListener(mNavigationItemSelectedListener); - navigation.inflateHeaderView(R.layout.design_navigation_header); } - private NavigationView.OnNavigationItemSelectedListener mNavigationItemSelectedListener - = new NavigationView.OnNavigationItemSelectedListener() { - @Override - public boolean onNavigationItemSelected(MenuItem item) { - if (handleNavigationItemSelected(item)) { - mDrawerLayout.closeDrawers(); - return true; - } - return false; - } - }; + @Override + protected int getLayout() { + return R.layout.design_navigation; + } - private boolean handleNavigationItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.navigation_item_1: - mTextMessage.setText("1"); - return true; - case R.id.navigation_item_2: - mTextMessage.setText("2"); - return true; - case R.id.navigation_item_3: - mTextMessage.setText("3"); - return true; - case R.id.navigation_sub_item_1: - showToast(R.string.navigation_sub_item_1); - return true; - case R.id.navigation_sub_item_2: - showToast(R.string.navigation_sub_item_2); - return true; - case R.id.navigation_with_icon: - showToast(R.string.navigation_item_with_icon); - return true; - case R.id.navigation_without_icon: - showToast(R.string.navigation_item_without_icon); - return true; - default: + @Override + public NavigationView.OnNavigationItemSelectedListener getNavigationItemSelectedListener() { + return new NavigationView.OnNavigationItemSelectedListener() { + @Override + public boolean onNavigationItemSelected(MenuItem item) { + if (handleNavigationItemSelected(item)) { + mDrawerLayout.closeDrawers(); + return true; + } return false; - } - } - - private void showToast(int res) { - Toast.makeText(this, getString(R.string.navigation_message, getString(res)), - Toast.LENGTH_SHORT).show(); + } + }; } @Override diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsageBase.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsageBase.java new file mode 100644 index 000000000..49080c0ab --- /dev/null +++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewUsageBase.java @@ -0,0 +1,84 @@ +/* + * 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. + */ + +package com.example.android.support.design.widget; + +import com.example.android.support.design.R; + +import android.os.Bundle; +import android.support.annotation.LayoutRes; +import android.support.design.widget.NavigationView; +import android.support.v7.app.AppCompatActivity; +import android.view.MenuItem; +import android.widget.TextView; +import android.widget.Toast; + +public abstract class NavigationViewUsageBase extends AppCompatActivity { + + private TextView mTextMessage; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(getLayout()); + + mTextMessage = (TextView) findViewById(R.id.message); + + // Menu + NavigationView navigation = (NavigationView) findViewById(R.id.navigation); + navigation.setNavigationItemSelectedListener(getNavigationItemSelectedListener()); + navigation.inflateHeaderView(R.layout.design_navigation_header); + } + + @LayoutRes + protected abstract int getLayout(); + + protected abstract NavigationView.OnNavigationItemSelectedListener getNavigationItemSelectedListener(); + + protected boolean handleNavigationItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.navigation_item_1: + mTextMessage.setText("1"); + return true; + case R.id.navigation_item_2: + mTextMessage.setText("2"); + return true; + case R.id.navigation_item_3: + mTextMessage.setText("3"); + return true; + case R.id.navigation_sub_item_1: + showToast(R.string.navigation_sub_item_1); + return true; + case R.id.navigation_sub_item_2: + showToast(R.string.navigation_sub_item_2); + return true; + case R.id.navigation_with_icon: + showToast(R.string.navigation_item_with_icon); + return true; + case R.id.navigation_without_icon: + showToast(R.string.navigation_item_without_icon); + return true; + default: + return false; + } + } + + private void showToast(int res) { + Toast.makeText(this, getString(R.string.navigation_message, getString(res)), + Toast.LENGTH_SHORT).show(); + } + +} diff --git a/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewWithoutDrawer.java b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewWithoutDrawer.java new file mode 100644 index 000000000..28e473342 --- /dev/null +++ b/samples/SupportDesignDemos/src/com/example/android/support/design/widget/NavigationViewWithoutDrawer.java @@ -0,0 +1,44 @@ +/* + * 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. + */ + +package com.example.android.support.design.widget; + +import com.example.android.support.design.R; + +import android.support.design.widget.NavigationView; +import android.view.MenuItem; + +/** + * This demonstrates basic usage of NavigationView + */ +public class NavigationViewWithoutDrawer extends NavigationViewUsageBase { + + @Override + protected int getLayout() { + return R.layout.design_navigation_without_drawer; + } + + @Override + protected NavigationView.OnNavigationItemSelectedListener getNavigationItemSelectedListener() { + return new NavigationView.OnNavigationItemSelectedListener() { + @Override + public boolean onNavigationItemSelected(MenuItem item) { + return handleNavigationItemSelected(item); + } + }; + } + +}