android RecyclerView SearchView基本用法2

Android AUmaWSsSoi 2周前 (05-06) 40次浏览 0个评论
文章目录[隐藏]

背景:

上文中介绍了RecyclerView SearchView配合使用的常用用法,本文会在上文的基础上加入Toolbar,Toolbar用来充当actionbar,代表标题栏那块区域的内容,SearchView就放置在Toolbar里面(这种交互场景还是多的)。

最终的效果图

android RecyclerView SearchView基本用法2

解决方案:

先看布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp">

        <android.support.v7.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_marginTop="2dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Toolbar的子元素是SearchView,由于在布局中使用了Toolbar,那么最好将当前Activity的主题变为NoActionbar。

style.xml代码

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

在onCreate方法中处理Toolbar,设置到Actionbar中

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

设置默认展示效果

//        SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//        searchAutoComplete.setHintTextColor(getResources().getColor(R.color.menu_text));
//        searchAutoComplete.setTextColor(getResources().getColor(R.color.menu_text));
        searchView.setIconified(false);
        searchView.setIconifiedByDefault(false);

完整的代码如下:

package me.star.searchview;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private SearchView searchView;
    private RecyclerView recyclerView;
    private Toolbar toolbar;
    private Adapter adapter;
    private List<String> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        searchView = findViewById(R.id.search_view);
        recyclerView = findViewById(R.id.recycler_view);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        adapter = new Adapter();
        datas = generateDatas();
        adapter.setDatas(datas);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    adapter.setDatas(datas);
                    adapter.notifyDataSetChanged();
                } else {
                    adapter.setDatas(search(newText));
                    adapter.notifyDataSetChanged();
                }
                return false;
            }
        });
        final ImageView searchIcon = searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
        searchView.post(new Runnable() {
            @Override
            public void run() {
                searchIcon.setImageDrawable(null);
                searchIcon.setVisibility(View.GONE);
            }
        });
//        SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//        searchAutoComplete.setHintTextColor(getResources().getColor(R.color.menu_text));
//        searchAutoComplete.setTextColor(getResources().getColor(R.color.menu_text));
        searchView.setIconified(false);
        searchView.setIconifiedByDefault(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private List<String> search(String query) {
        List<String> filterDatas = new ArrayList<>();
        for (String source : datas) {
            if (source.contains(query)) {
                filterDatas.add(source);
            }
        }
        return filterDatas;
    }

    private List<String> generateDatas() {
        List<String> list = new ArrayList<>();
        for (int index = 0; index < 10; index++) {
            list.add("Hello World " + (index + 1));
        }
        return list;
    }

    class Adapter extends RecyclerView.Adapter {
        private List<String> datas = new ArrayList<>();

        public void setDatas(List<String> datas) {
            this.datas = datas;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
            ViewHolder holder = (ViewHolder) viewHolder;
            holder.textView.setText(datas.get(position));
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(android.R.id.text1);
        }
    }
}

代码下载地址:

链接:https://pan.baidu.com/s/1XW74iJoJH9lmbhfdygS9vA 密码:kyhh


java问与答 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:http://javaexception.com/archives/83
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址