|
1 | 1 | package com.xiaofeng.androidlibs; |
2 | 2 |
|
3 | | -import android.content.Intent; |
4 | | -import android.content.SharedPreferences; |
5 | 3 | import android.graphics.Rect; |
6 | 4 | import android.os.Bundle; |
7 | | -import android.preference.PreferenceManager; |
| 5 | +import android.support.annotation.NonNull; |
| 6 | +import android.support.design.widget.BottomNavigationView; |
8 | 7 | import android.support.v7.app.AppCompatActivity; |
9 | 8 | import android.support.v7.widget.RecyclerView; |
10 | | -import android.support.v7.widget.Toolbar; |
11 | | -import android.view.Menu; |
12 | 9 | import android.view.MenuItem; |
13 | 10 | import android.view.View; |
14 | 11 |
|
15 | 12 | import com.xiaofeng.flowlayoutmanager.Alignment; |
16 | 13 | import com.xiaofeng.flowlayoutmanager.FlowLayoutManager; |
17 | 14 |
|
18 | | -import us.feras.mdv.MarkdownView; |
19 | | - |
20 | 15 | public class MainActivity extends AppCompatActivity { |
21 | 16 |
|
22 | | - private static final int REQ_CODE_SETTINGS = 101; |
23 | | - RecyclerView recyclerView; |
24 | | - FlowLayoutManager flowLayoutManager; |
25 | | - MarkdownView markdownView; |
26 | | - private boolean settingChanged = false; |
27 | | - @Override |
28 | | - protected void onCreate(Bundle savedInstanceState) { |
29 | | - super.onCreate(savedInstanceState); |
30 | | - setContentView(R.layout.main_app_bar); |
31 | | - Toolbar toolbar = findViewById(R.id.toolbar); |
32 | | - setSupportActionBar(toolbar); |
33 | | - init(); |
34 | | - } |
35 | | - |
36 | | - private void init() { |
37 | | - recyclerView = findViewById(R.id.list); |
38 | | - flowLayoutManager = new FlowLayoutManager().singleItemPerLine(); |
39 | | - flowLayoutManager.setAutoMeasureEnabled(true); |
40 | | - recyclerView.setLayoutManager(flowLayoutManager); |
41 | | - recyclerView.setAdapter(new DemoAdapter(1, DemoUtil.listWords())); |
42 | | - recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() { |
43 | | - @Override |
44 | | - public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { |
45 | | - super.getItemOffsets(outRect, view, parent, state); |
46 | | - outRect.set(5, 5, 5, 5); |
47 | | - } |
48 | | - }); |
49 | | - |
50 | | -// markdownView = (MarkdownView)findViewById(R.id.instruction_mdown); |
51 | | -// markdownView.loadMarkdownFile("file:///android_asset/instruction.md"); |
52 | | - loadSettingsFromSharedPref(); |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public boolean onCreateOptionsMenu(Menu menu) { |
57 | | - getMenuInflater().inflate(R.menu.main, menu); |
58 | | - return super.onCreateOptionsMenu(menu); |
59 | | - } |
60 | | - |
61 | | - @Override |
62 | | - public boolean onOptionsItemSelected(MenuItem item) { |
63 | | - if (item.getItemId() == R.id.action_settings) { |
64 | | - startActivityForResult(new Intent(this, SettingsActivity.class), REQ_CODE_SETTINGS); |
65 | | - return true; |
66 | | - } |
67 | | - return super.onOptionsItemSelected(item); |
68 | | - } |
69 | | - |
70 | | - private void loadSettingsFromSharedPref() { |
71 | | - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); |
72 | | - String itemsPerLineString = sharedPreferences.getString(getResources().getString(R.string.pref_key_max_items_per_line), getString(R.string.pref_max_items_per_line_default)); |
73 | | - int itemsPerLine = Integer.valueOf(itemsPerLineString); |
74 | | - |
75 | | - String alignmentString = sharedPreferences.getString(getResources().getString(R.string.pref_key_alignment), getString(R.string.pref_alignment_default)); |
76 | | - |
77 | | - if (alignmentString.equals("0")) { |
78 | | - flowLayoutManager.setAlignment(Alignment.LEFT); |
79 | | - } else if (alignmentString.equals("2")) { |
80 | | - flowLayoutManager.setAlignment(Alignment.RIGHT); |
81 | | - } else { |
82 | | - flowLayoutManager.setAlignment(Alignment.CENTER); |
| 17 | + RecyclerView recyclerView; |
| 18 | + FlowLayoutManager flowLayoutManager; |
| 19 | + RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() { |
| 20 | + @Override |
| 21 | + public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { |
| 22 | + super.getItemOffsets(outRect, view, parent, state); |
| 23 | + outRect.set(5, 5, 5, 5); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener |
| 28 | + = new BottomNavigationView.OnNavigationItemSelectedListener() { |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean onNavigationItemSelected(@NonNull MenuItem item) { |
| 32 | + switch (item.getItemId()) { |
| 33 | + case R.id.navigation_left: |
| 34 | + flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.LEFT); |
| 35 | + |
| 36 | + recyclerView.setLayoutManager(flowLayoutManager); |
| 37 | + recyclerView.getAdapter().notifyDataSetChanged(); |
| 38 | + recyclerView.addItemDecoration(itemDecoration); |
| 39 | + |
| 40 | + return true; |
| 41 | + case R.id.navigation_center: |
| 42 | + flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.CENTER); |
| 43 | + flowLayoutManager.setAutoMeasureEnabled(true); |
| 44 | + |
| 45 | + recyclerView.setLayoutManager(flowLayoutManager); |
| 46 | + recyclerView.getAdapter().notifyDataSetChanged(); |
| 47 | + recyclerView.addItemDecoration(itemDecoration); |
| 48 | + |
| 49 | + return true; |
| 50 | + case R.id.navigation_rigth: |
| 51 | + flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.RIGHT); |
| 52 | + flowLayoutManager.setAutoMeasureEnabled(true); |
| 53 | + |
| 54 | + recyclerView.setLayoutManager(flowLayoutManager); |
| 55 | + recyclerView.getAdapter().notifyDataSetChanged(); |
| 56 | + recyclerView.addItemDecoration(itemDecoration); |
| 57 | + |
| 58 | + return true; |
| 59 | + } |
| 60 | + return false; |
83 | 61 | } |
84 | | -// boolean showMeta = sharedPreferences.getBoolean(getString(R.string.pref_key_show_meta), false); |
85 | | - |
86 | | - flowLayoutManager.maxItemsPerLine(itemsPerLine); |
87 | | - DemoAdapter demoAdapter = (DemoAdapter)recyclerView.getAdapter(); |
88 | | -// demoAdapter.setShowMeta(showMeta); |
89 | | - String maxLinesPerItemString = sharedPreferences.getString(getString(R.string.pref_key_max_lines_per_item), getString(R.string.pref_max_lines_per_item_default)); |
90 | | - int maxLinesPerItem = Integer.valueOf(maxLinesPerItemString); |
91 | | - demoAdapter.newItems(maxLinesPerItem, DemoUtil.listWords()); |
92 | | - recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount()); |
93 | | - } |
94 | | - |
95 | | - @Override |
96 | | - protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
97 | | - super.onActivityResult(requestCode, resultCode, data); |
98 | | - if (requestCode == REQ_CODE_SETTINGS) { |
99 | | - settingChanged = true; |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - @Override |
104 | | - public void onWindowFocusChanged(boolean hasFocus) { |
105 | | - super.onWindowFocusChanged(hasFocus); |
106 | | - if (hasFocus) { |
107 | | - if (settingChanged) { |
108 | | - settingChanged = false; |
109 | | - recyclerView.post(new Runnable() { |
110 | | - @Override |
111 | | - public void run() { |
112 | | - loadSettingsFromSharedPref(); |
113 | | - } |
114 | | - }); |
115 | | - } |
116 | | - } |
117 | | - } |
| 62 | + }; |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void onCreate(Bundle savedInstanceState) { |
| 66 | + super.onCreate(savedInstanceState); |
| 67 | + setContentView(R.layout.main_activity); |
| 68 | + |
| 69 | + BottomNavigationView navigation = findViewById(R.id.navigation); |
| 70 | + navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); |
| 71 | + |
| 72 | + recyclerView = findViewById(R.id.list); |
| 73 | + flowLayoutManager = new FlowLayoutManager().setAlignment(Alignment.LEFT); |
| 74 | + flowLayoutManager.setAutoMeasureEnabled(true); |
| 75 | + recyclerView.setLayoutManager(flowLayoutManager); |
| 76 | + recyclerView.setAdapter(new DemoAdapter(DemoUtil.listWords())); |
| 77 | + recyclerView.getAdapter().notifyDataSetChanged(); |
| 78 | + recyclerView.addItemDecoration(itemDecoration); |
| 79 | + |
| 80 | + |
| 81 | + } |
| 82 | + |
118 | 83 | } |
0 commit comments