|
| 1 | +package com.udacity.notepad; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.design.widget.FloatingActionButton; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.support.v7.widget.LinearLayoutManager; |
| 7 | +import android.support.v7.widget.RecyclerView; |
| 8 | +import android.support.v7.widget.Toolbar; |
| 9 | +import android.view.Menu; |
| 10 | +import android.view.MenuItem; |
| 11 | +import android.view.View; |
| 12 | + |
| 13 | +import com.udacity.notepad.crud.CreateActivity; |
| 14 | +import com.udacity.notepad.recycler.NotesAdapter; |
| 15 | +import com.udacity.notepad.util.SpaceItemDecoration; |
| 16 | + |
| 17 | +public class MainActivity extends AppCompatActivity { |
| 18 | + |
| 19 | + private RecyclerView recycler; |
| 20 | + |
| 21 | + @Override |
| 22 | + public void onCreate(Bundle savedInstanceState) { |
| 23 | + super.onCreate(savedInstanceState); |
| 24 | + setContentView(R.layout.activity_main); |
| 25 | + |
| 26 | + Toolbar toolbar = findViewById(R.id.toolbar); |
| 27 | + setSupportActionBar(toolbar); |
| 28 | + |
| 29 | + FloatingActionButton fab = findViewById(R.id.fab); |
| 30 | + fab.setOnClickListener(new View.OnClickListener() { |
| 31 | + @Override |
| 32 | + public void onClick(View v) { |
| 33 | + startActivity(CreateActivity.get(MainActivity.this)); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + recycler = findViewById(R.id.recycler); |
| 38 | + recycler.setLayoutManager(new LinearLayoutManager(this)); |
| 39 | + recycler.addItemDecoration(new SpaceItemDecoration(this, R.dimen.margin_small)); |
| 40 | + recycler.setAdapter(new NotesAdapter(this)); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void onResume() { |
| 45 | + super.onResume(); |
| 46 | + refresh(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onDestroy() { |
| 51 | + super.onDestroy(); |
| 52 | + recycler.setAdapter(null); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 57 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 58 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 64 | + // Handle action bar item clicks here. The action bar will |
| 65 | + // automatically handle clicks on the Home/Up button, so long |
| 66 | + // as you specify a parent activity in AndroidManifest.xml. |
| 67 | + switch (item.getItemId()) { |
| 68 | + case R.id.action_settings: |
| 69 | + return true; |
| 70 | + default: |
| 71 | + return super.onOptionsItemSelected(item); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private void refresh() { |
| 77 | + ((NotesAdapter) recycler.getAdapter()).refresh(); |
| 78 | + } |
| 79 | +} |
0 commit comments