简单的界面效果如下
布局如下,通过一个RecyclerView控制显示三种情况的显示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/material_primary"
android:theme="@style/ToolBarStyle"
app:popupTheme="@style/Theme.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:background="@color/material_icons"
android:scrollbars="none"/>
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="@color/material_red_a700"
android:text="Open The SearchView"
android:layout_centerInParent="true"/>
</RelativeLayout>
①界面初始化,简单设置下Toolbar,RecyclerView ,
mHistoryOrAllSearchList用于存放历史或联想词;mHotSearchList用于存放热词
SharedPreUtil.readString(Constant.OPENTIME).isEmpty()用来模拟版本变化更新数据库
更新数据库会将上版本的热词和联想词删除,涉及到大数据量,所以开线程操作mKeywordsTableDao.getSession().runInTx
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar == null) {
return;
}
mToolbar.setTitle(R.string.app_name);
setSupportActionBar(mToolbar);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
StaggeredGridLayoutManager mManager = new StaggeredGridLayoutManager(2,
StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mManager);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(5));
mAdapter = new SearchAdapter(this);
mAdapter.setItemClickLitener(this);
mRecyclerView.setAdapter(mAdapter);
mHistoryOrAssociativeSearchList = new ArrayList<>();
mHotSearchList = new ArrayList<>();
mKeywordsTableDao = getSearchKeywordsTableDao(mKeywordsTableDao);
if (SharedPreUtil.readString(Constant.OPENTIME).isEmpty()) {
// 词库版本有更新才去更新数据库的热词和练习词
final List<SearchKeywords> keywordses = new ArrayList<>();
// 热词
keywordses.add(new SearchKeywords("women", 12, -1, Constant.SEARCH_KEYWORDS_HOT));
keywordses.add(new SearchKeywords("men", 10, -1, Constant.SEARCH_KEYWORDS_HOT));
keywordses.add(new SearchKeywords("beauty", 16, -1, Constant.SEARCH_KEYWORDS_HOT));
keywordses.add(new SearchKeywords("makeup", 2, -1, Constant.SEARCH_KEYWORDS_HOT));
keywordses.add(new SearchKeywords("clothes", 6, -1, Constant.SEARCH_KEYWORDS_HOT));