SearchView+RecyclerView+GreenDao的搜索功能实现(2)

本文介绍了如何使用SearchView、RecyclerView和GreenDao在Android中实现搜索功能。通过设置界面,初始化 Toolbar 和 RecyclerView,以及管理历史记录和热门搜索列表,实现了版本更新时的数据同步。在RecyclerView适配器中,设计了三种视图模式,并处理了点击事件。总结部分提到,虽然解释可能不够详尽,但提供了一个包含完整功能的Demo,通过运行和查看源码可以更好地理解实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单的界面效果如下

①打开搜索框显示的是历史和热词,历史可删除 ②点击某一项,显示搜索的单词和结果

③单词联想

布局如下,通过一个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));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值