1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.support.design.widget; 18 19 import com.example.android.support.design.R; 20 21 import android.os.Bundle; 22 import android.support.annotation.LayoutRes; 23 import android.support.design.widget.NavigationView; 24 import android.support.v7.app.AppCompatActivity; 25 import android.view.MenuItem; 26 import android.widget.TextView; 27 import android.widget.Toast; 28 29 public abstract class NavigationViewUsageBase extends AppCompatActivity { 30 31 private TextView mTextMessage; 32 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(getLayout()); 37 38 mTextMessage = (TextView) findViewById(R.id.message); 39 40 // Menu 41 NavigationView navigation = (NavigationView) findViewById(R.id.navigation); 42 navigation.setNavigationItemSelectedListener(getNavigationItemSelectedListener()); 43 navigation.inflateHeaderView(R.layout.design_navigation_header); 44 } 45 46 @LayoutRes getLayout()47 protected abstract int getLayout(); 48 getNavigationItemSelectedListener()49 protected abstract NavigationView.OnNavigationItemSelectedListener getNavigationItemSelectedListener(); 50 handleNavigationItemSelected(MenuItem item)51 protected boolean handleNavigationItemSelected(MenuItem item) { 52 switch (item.getItemId()) { 53 case R.id.navigation_item_1: 54 mTextMessage.setText("1"); 55 return true; 56 case R.id.navigation_item_2: 57 mTextMessage.setText("2"); 58 return true; 59 case R.id.navigation_item_3: 60 mTextMessage.setText("3"); 61 return true; 62 case R.id.navigation_sub_item_1: 63 showToast(R.string.navigation_sub_item_1); 64 return true; 65 case R.id.navigation_sub_item_2: 66 showToast(R.string.navigation_sub_item_2); 67 return true; 68 case R.id.navigation_with_icon: 69 showToast(R.string.navigation_item_with_icon); 70 return true; 71 case R.id.navigation_without_icon: 72 showToast(R.string.navigation_item_without_icon); 73 return true; 74 default: 75 return false; 76 } 77 } 78 showToast(int res)79 private void showToast(int res) { 80 Toast.makeText(this, getString(R.string.navigation_message, getString(res)), 81 Toast.LENGTH_SHORT).show(); 82 } 83 84 } 85