Commit d06f2cc2d8e48f4b493af0cdf0a33c1d05f6ed57
1 parent
0551ca94
- add booksearch bar
Showing
4 changed files
with
87 additions
and
6 deletions
app/src/main/java/net/devfac/userstory/FragmentBookShelf.java
1 | 1 | package net.devfac.userstory; |
2 | 2 | |
3 | 3 | import android.app.ProgressDialog; |
4 | +import android.database.Cursor; | |
5 | +import android.database.DataSetObserver; | |
4 | 6 | import android.os.Bundle; |
5 | 7 | import android.support.v4.app.Fragment; |
8 | +import android.support.v4.view.MenuItemCompat; | |
6 | 9 | import android.support.v7.widget.LinearLayoutManager; |
7 | 10 | import android.support.v7.widget.RecyclerView; |
8 | 11 | import android.view.LayoutInflater; |
12 | +import android.view.MenuItem; | |
9 | 13 | import android.view.View; |
10 | 14 | import android.view.ViewGroup; |
11 | 15 | import android.webkit.WebView; |
16 | +import android.widget.FilterQueryProvider; | |
12 | 17 | import android.widget.ListView; |
18 | +import android.widget.SearchView; | |
13 | 19 | import android.widget.Toast; |
14 | 20 | |
15 | 21 | import net.devfac.userstory.FSM.Action; |
... | ... | @@ -25,13 +31,14 @@ import java.util.List; |
25 | 31 | * Fragment for Book Shelf |
26 | 32 | * Created by Onether on 15. 10. 18.. |
27 | 33 | */ |
28 | -public class FragmentBookShelf extends Fragment implements StateEventListener { | |
34 | +public class FragmentBookShelf extends Fragment implements StateEventListener, SearchView.OnQueryTextListener { | |
29 | 35 | private BookShelfCursorAdapter mBookShelfCursorAdapter; |
30 | 36 | private ListView mBookshelf; |
31 | 37 | private WebView wv; |
32 | 38 | private ProgressDialog mProgressDialog; |
33 | 39 | private RecyclerView mRecyclerView; |
34 | 40 | private RecyclerAdapterBookShelfItem mRecyclerAdapter; |
41 | + private MenuItem mSearchMenuItem; | |
35 | 42 | |
36 | 43 | public static FragmentBookShelf getInstance(int itemCount){ |
37 | 44 | FragmentBookShelf fragmentBookShelf = new FragmentBookShelf(); |
... | ... | @@ -50,6 +57,23 @@ public class FragmentBookShelf extends Fragment implements StateEventListener { |
50 | 57 | getActivity(), |
51 | 58 | StateContext.getInstance(getActivity()).getDbOpenHelper().getCursor(), true); |
52 | 59 | |
60 | + mBookShelfCursorAdapter.registerDataSetObserver(new DataSetObserver() { | |
61 | + @Override | |
62 | + public void onChanged() { | |
63 | + super.onChanged(); | |
64 | +// Logger.i("DATA SET CHANGED : " + mBookShelfCursorAdapter.getCount()); | |
65 | + mRecyclerAdapter.notifyDataSetChanged(); | |
66 | + | |
67 | + } | |
68 | + }); | |
69 | + mBookShelfCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() { | |
70 | + @Override | |
71 | + public Cursor runQuery(CharSequence constraint) { | |
72 | + String keyword = constraint.toString().replace("\n", ""); | |
73 | + return StateContext.getInstance(getActivity()).getDbOpenHelper().getCursor(keyword); | |
74 | + } | |
75 | + }); | |
76 | + | |
53 | 77 | setupRecyclerView(mRecyclerView); |
54 | 78 | |
55 | 79 | StateContext.getInstance(getActivity()).setStateEventListener(this); |
... | ... | @@ -95,4 +119,23 @@ public class FragmentBookShelf extends Fragment implements StateEventListener { |
95 | 119 | }); |
96 | 120 | } |
97 | 121 | } |
122 | + | |
123 | + public void setMenuSearchMenu(MenuItem searchMenuItem){ | |
124 | + mSearchMenuItem = searchMenuItem; | |
125 | + | |
126 | + final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem); | |
127 | + searchView.setOnQueryTextListener(this); | |
128 | + | |
129 | + } | |
130 | + | |
131 | + @Override | |
132 | + public boolean onQueryTextSubmit(String query) { | |
133 | + return false; | |
134 | + } | |
135 | + | |
136 | + @Override | |
137 | + public boolean onQueryTextChange(String newText) { | |
138 | + mBookShelfCursorAdapter.getFilter().filter(newText); | |
139 | + return true; | |
140 | + } | |
98 | 141 | } | ... | ... |
app/src/main/java/net/devfac/userstory/MainActivity.java
... | ... | @@ -13,8 +13,10 @@ import android.support.design.widget.TabLayout; |
13 | 13 | import android.support.v4.app.Fragment; |
14 | 14 | import android.support.v4.app.FragmentManager; |
15 | 15 | import android.support.v4.app.FragmentPagerAdapter; |
16 | +import android.support.v4.view.MenuItemCompat; | |
16 | 17 | import android.support.v4.view.ViewPager; |
17 | 18 | import android.util.AttributeSet; |
19 | +import android.view.MenuInflater; | |
18 | 20 | import android.view.View; |
19 | 21 | import android.support.design.widget.NavigationView; |
20 | 22 | import android.support.v4.view.GravityCompat; |
... | ... | @@ -25,6 +27,8 @@ import android.support.v7.widget.Toolbar; |
25 | 27 | import android.view.Menu; |
26 | 28 | import android.view.MenuItem; |
27 | 29 | import android.widget.Button; |
30 | +import android.widget.LinearLayout; | |
31 | +import android.widget.SearchView; | |
28 | 32 | import android.widget.TextView; |
29 | 33 | import android.widget.Toast; |
30 | 34 | |
... | ... | @@ -41,15 +45,18 @@ import java.util.List; |
41 | 45 | |
42 | 46 | //TODO: Add Search bar - http://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview |
43 | 47 | public class MainActivity extends AppCompatActivity |
44 | - implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener, StateEventListener { | |
48 | + implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener, StateEventListener{ | |
45 | 49 | |
46 | 50 | private ViewPager mViewPagerMain; |
47 | 51 | private ViewPagerAdapter mViewPagerAdapter; |
48 | 52 | private TabLayout mTabLayoutMain; |
49 | 53 | private AppBarLayout mAppBarMain; |
50 | 54 | private CoordinatorLayout mCoordinateMain; |
51 | - | |
52 | 55 | private ProgressDialog mProgressDialog; |
56 | + private MenuItem mSearchMenuItem; | |
57 | + | |
58 | + private FragmentBookShelf mFragmentBookShelf; | |
59 | + private FragmentAddBook mFragmentAddBoook; | |
53 | 60 | |
54 | 61 | FloatingActionButton mFab; |
55 | 62 | |
... | ... | @@ -106,6 +113,8 @@ public class MainActivity extends AppCompatActivity |
106 | 113 | public void onPageSelected(int position) { |
107 | 114 | if (mViewPagerAdapter.getPosition(Constants.FRAGMENT_TITLE_BOOK_SHELF) == position) { |
108 | 115 | StateContext.getInstance(MainActivity.this).processEvent(Action.REQ_SYNC, null); |
116 | + | |
117 | + mSearchMenuItem.setVisible(true); | |
109 | 118 | } else if (mViewPagerAdapter.getPosition(Constants.FRAGMENT_TITLE_ADD_BOOK) == position) { |
110 | 119 | //Expand toolbar |
111 | 120 | CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarMain.getLayoutParams(); |
... | ... | @@ -114,6 +123,11 @@ public class MainActivity extends AppCompatActivity |
114 | 123 | behavior.setTopAndBottomOffset(0); |
115 | 124 | behavior.onNestedPreScroll(mCoordinateMain, mAppBarMain, null, 0, 1, new int[2]); |
116 | 125 | } |
126 | + | |
127 | + mSearchMenuItem.setVisible(false); | |
128 | + } | |
129 | + else{ | |
130 | + mSearchMenuItem.setVisible(false); | |
117 | 131 | } |
118 | 132 | } |
119 | 133 | |
... | ... | @@ -195,6 +209,11 @@ public class MainActivity extends AppCompatActivity |
195 | 209 | public boolean onCreateOptionsMenu(Menu menu) { |
196 | 210 | // Inflate the menu; this adds items to the action bar if it is present. |
197 | 211 | getMenuInflater().inflate(R.menu.main, menu); |
212 | + | |
213 | + mSearchMenuItem = menu.findItem(R.id.action_search); | |
214 | + mSearchMenuItem.setVisible(false); | |
215 | + mFragmentBookShelf.setMenuSearchMenu(mSearchMenuItem); | |
216 | + | |
198 | 217 | return true; |
199 | 218 | } |
200 | 219 | |
... | ... | @@ -236,9 +255,12 @@ public class MainActivity extends AppCompatActivity |
236 | 255 | |
237 | 256 | private void setupViewPager(ViewPager viewPager){ |
238 | 257 | mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); |
239 | - mViewPagerAdapter.addFragment(new FragmentAddBook(), Constants.FRAGMENT_TITLE_ADD_BOOK); | |
240 | - mViewPagerAdapter.addFragment(new FragmentBookShelf(), Constants.FRAGMENT_TITLE_BOOK_SHELF); | |
241 | -// mViewPagerAdapter.addFragment(new FragmentLogin(), Constants.FRAGMENT_TITLE_LOGIN); | |
258 | + mFragmentBookShelf = new FragmentBookShelf(); | |
259 | + mFragmentAddBoook = new FragmentAddBook(); | |
260 | + | |
261 | + mViewPagerAdapter.addFragment(mFragmentAddBoook, Constants.FRAGMENT_TITLE_ADD_BOOK); | |
262 | + mViewPagerAdapter.addFragment(mFragmentBookShelf, Constants.FRAGMENT_TITLE_BOOK_SHELF); | |
263 | + | |
242 | 264 | viewPager.setAdapter(mViewPagerAdapter); |
243 | 265 | } |
244 | 266 | ... | ... |
app/src/main/java/net/devfac/userstory/Utils/DbOpenHelper.java
... | ... | @@ -97,6 +97,19 @@ public class DbOpenHelper { |
97 | 97 | } |
98 | 98 | |
99 | 99 | public Cursor getCursor(){ return mCursor; } |
100 | + public Cursor getCursor(String title){ | |
101 | + Cursor tempCursor = null; | |
102 | + String querySelectByTitle = String.format( "SELECT * FROM %s", Database.BookShelfTable._TABLE_NAME) | |
103 | + + " WHERE " + Database.BookShelfTable.TITLE + " LIKE '%" + title + "%' order by " + Database.BookShelfTable.TITLE + " asc"; | |
104 | + try{ | |
105 | + tempCursor = mDB.rawQuery(querySelectByTitle, null); | |
106 | + } | |
107 | + catch (android.database.sqlite.SQLiteException e){ | |
108 | + e.printStackTrace(); | |
109 | + } | |
110 | + | |
111 | + return tempCursor; | |
112 | + } | |
100 | 113 | |
101 | 114 | public ArrayList<BookShelfInfo> getAllEntries(){ |
102 | 115 | ArrayList<BookShelfInfo> entries = new ArrayList<>(); | ... | ... |
app/src/main/res/menu/main.xml
... | ... | @@ -5,4 +5,7 @@ |
5 | 5 | android:orderInCategory="100" app:showAsAction="never" /> |
6 | 6 | <item android:id="@+id/action_main_about" android:title="@string/action_main_about" |
7 | 7 | android:orderInCategory="200" app:showAsAction="never" /> |
8 | + <item android:id="@+id/action_search" android:title="Search" | |
9 | + app:actionViewClass="android.widget.SearchView" | |
10 | + app:showAsAction="always"/> | |
8 | 11 | </menu> | ... | ... |