|
1 | 1 | package com.mancj.example;
|
2 | 2 |
|
3 |
| -import android.graphics.Color; |
4 |
| -import android.support.v7.app.AppCompatActivity; |
5 | 3 | import android.os.Bundle;
|
6 |
| -import android.support.v7.widget.PopupMenu; |
7 |
| -import android.util.Log; |
8 |
| -import android.view.MenuItem; |
| 4 | +import android.support.design.widget.FloatingActionButton; |
| 5 | +import android.support.design.widget.Snackbar; |
| 6 | +import android.view.Gravity; |
9 | 7 | import android.view.View;
|
10 |
| -import android.widget.FrameLayout; |
11 |
| -import android.widget.RelativeLayout; |
12 |
| -import android.widget.Toast; |
| 8 | +import android.support.design.widget.NavigationView; |
| 9 | +import android.support.v4.view.GravityCompat; |
| 10 | +import android.support.v4.widget.DrawerLayout; |
| 11 | +import android.support.v7.app.ActionBarDrawerToggle; |
| 12 | +import android.support.v7.app.AppCompatActivity; |
| 13 | +import android.support.v7.widget.Toolbar; |
| 14 | +import android.view.Menu; |
| 15 | +import android.view.MenuItem; |
13 | 16 |
|
14 | 17 | import com.mancj.materialsearchbar.MaterialSearchBar;
|
15 | 18 |
|
16 |
| -import java.util.List; |
17 |
| - |
18 |
| -public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener, PopupMenu.OnMenuItemClickListener { |
19 |
| - private List<String> lastSearches; |
20 |
| - private MaterialSearchBar searchBar; |
21 |
| - private FrameLayout dim; |
| 19 | +public class MainActivity extends AppCompatActivity |
| 20 | + implements NavigationView.OnNavigationItemSelectedListener, MaterialSearchBar.OnSearchActionListener { |
| 21 | + MaterialSearchBar searchBar; |
| 22 | + private DrawerLayout drawer; |
22 | 23 |
|
23 | 24 | @Override
|
24 | 25 | protected void onCreate(Bundle savedInstanceState) {
|
25 | 26 | super.onCreate(savedInstanceState);
|
26 | 27 | setContentView(R.layout.activity_main);
|
27 | 28 |
|
28 |
| - dim = (FrameLayout) findViewById(R.id.dim); |
29 |
| - |
| 29 | + drawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 30 | + NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); |
| 31 | + navigationView.setNavigationItemSelectedListener(this); |
30 | 32 | searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
|
31 |
| - //enable searchbar callbacks |
32 | 33 | searchBar.setOnSearchActionListener(this);
|
33 | 34 | searchBar.inflateMenu(R.menu.main);
|
34 |
| - searchBar.getMenu().setOnMenuItemClickListener(this); |
35 |
| - //restore last queries from disk |
36 |
| -// lastSearches = loadSearchSuggestionFromDiks(); |
37 |
| -// searchBar.setLastSuggestions(list); |
38 | 35 | }
|
39 | 36 |
|
40 | 37 | @Override
|
41 |
| - protected void onDestroy() { |
42 |
| - super.onDestroy(); |
43 |
| - //save last queries to disk |
44 |
| -// saveSearchSuggestionToDisk(searchBar.getLastSuggestions()); |
| 38 | + public void onBackPressed() { |
| 39 | + DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 40 | + if (drawer.isDrawerOpen(GravityCompat.START)) { |
| 41 | + drawer.closeDrawer(GravityCompat.START); |
| 42 | + } else { |
| 43 | + super.onBackPressed(); |
| 44 | + } |
45 | 45 | }
|
46 | 46 |
|
47 |
| - //called when searchbar enabled or disabled |
48 | 47 | @Override
|
49 |
| - public void onSearchStateChanged(boolean enabled) { |
50 |
| - String s = enabled ? "enabled" : "disabled"; |
51 |
| - Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show(); |
52 |
| - if (enabled){ |
53 |
| - dim.setVisibility(View.VISIBLE); |
54 |
| - }else { |
55 |
| - dim.setVisibility(View.GONE); |
| 48 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 49 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 50 | + getMenuInflater().inflate(R.menu.main, menu); |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 56 | + // Handle action bar item clicks here. The action bar will |
| 57 | + // automatically handle clicks on the Home/Up button, so long |
| 58 | + // as you specify a parent activity in AndroidManifest.xml. |
| 59 | + int id = item.getItemId(); |
| 60 | + |
| 61 | + //noinspection SimplifiableIfStatement |
| 62 | + if (id == R.id.action_settings) { |
| 63 | + return true; |
56 | 64 | }
|
| 65 | + |
| 66 | + return super.onOptionsItemSelected(item); |
57 | 67 | }
|
58 | 68 |
|
59 |
| - //called when user confirms request |
| 69 | + @SuppressWarnings("StatementWithEmptyBody") |
60 | 70 | @Override
|
61 |
| - public void onSearchConfirmed(CharSequence text) { |
62 |
| -// startSearch(text.toString(), true, null, true); |
| 71 | + public boolean onNavigationItemSelected(MenuItem item) { |
| 72 | + // Handle navigation view item clicks here. |
| 73 | + int id = item.getItemId(); |
| 74 | + |
| 75 | + if (id == R.id.nav_camera) { |
| 76 | + // Handle the camera action |
| 77 | + } else if (id == R.id.nav_gallery) { |
| 78 | + |
| 79 | + } else if (id == R.id.nav_slideshow) { |
| 80 | + |
| 81 | + } else if (id == R.id.nav_manage) { |
| 82 | + |
| 83 | + } else if (id == R.id.nav_share) { |
| 84 | + |
| 85 | + } else if (id == R.id.nav_send) { |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 90 | + drawer.closeDrawer(GravityCompat.START); |
| 91 | + return true; |
63 | 92 | }
|
64 | 93 |
|
65 |
| - //called when microphone icon clicked |
66 | 94 | @Override
|
67 |
| - public void onSpeechIconSelected() { |
68 |
| -// openVoiceRecognizer(); |
| 95 | + public void onSearchStateChanged(boolean enabled) { |
| 96 | + |
69 | 97 | }
|
70 | 98 |
|
71 | 99 | @Override
|
72 |
| - public boolean onMenuItemClick(MenuItem item) { |
73 |
| - Log.d("LOG_TAG", getClass().getSimpleName() + ": item clicked " + item.getTitle()); |
74 |
| - return false; |
| 100 | + public void onSearchConfirmed(CharSequence text) { |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onButtonClicked(int buttonCode) { |
| 106 | + switch (buttonCode){ |
| 107 | + case MaterialSearchBar.BUTTON_NAVIGATION: |
| 108 | + drawer.openDrawer(Gravity.LEFT); |
| 109 | + break; |
| 110 | + case MaterialSearchBar.BUTTON_SPEECH: |
| 111 | + } |
75 | 112 | }
|
76 | 113 | }
|
0 commit comments