SearchView結合Toolbar 篩選RecyclerView中的內容,相似知乎、google play實現的效果

轉載請註明出處:http://www.wangxinarhat.com/2016/03/18/2016-03-18-meterial-design-search-view/android

效果

圖片描述

圖片描述

代碼已上傳至githubgit

項目裏要作一個對列表內容的篩選,想到了知乎以及google play的效果,肯定了是用Toolbar、SearchView、RecyclerView實現github


Toolbar不用多說,若有不懂我也上傳到了github
Recycler我以前也寫過Demo,一樣上傳到了github
難搞的是SearchView,畢竟如今都流行Material Design了嘛,自帶的那個效果實在不敢恭維,網上搜了下,已有大神實現了MaterialDesign效果的SearchView,因此拿來用咯ide

代碼

初始化操做

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        initView();
    }


    private void initView() {

        initToolBar();
        initRecyclerView();
        initSearchView();

    }


    /**
     * init Toolbar
     */
    private void initToolBar() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }


    /**
     * init RecyclerView
     */
    private void initRecyclerView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        mPeopleList = new ArrayList<>();

        String[] peopleName = {"Kaka", "Modric", "Rooney", "Ibla", "Bale", "死神", "Maurice Moss", "Roy Trenneman", "林夕", "sina", "google", "ecust"};
        String[] peopleDes = {"The best player", "莫德里奇是最好的後腰", "魯尼踢得很差", "伊貝拉是誰?", "貝爾跑得真快", "Aaron", "Oh, four, I mean five, I mean fire!", "哈哈", "是個藝術家", "weibo", "android", "china"};

        for (int i = 0; i < peopleName.length; i++) {
            mPeopleList.add(new People(peopleName[i], peopleDes[i]));
        }

        mAdapter = new SearchPeopleAdapter(mPeopleList);
        recyclerView.setAdapter(mAdapter);
        HeaderAdapter headerAdapter = new HeaderAdapter(mAdapter);

        recyclerView.setAdapter(headerAdapter);
        recyclerView.addOnItemTouchListener(new RecyclerOnItemClickListener(this, recyclerView, this));


    }

    /**
     * init SearchView
     */
    private void initSearchView() {

        searchView.setVoiceSearch(false);
        searchView.setCursorDrawable(R.drawable.custom_cursor);
        searchView.setEllipsize(true);
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                final List<People> filteredModelList = filter(mPeopleList, newText);

                //reset
                mAdapter.setFilter(filteredModelList);
                mAdapter.animateTo(filteredModelList);
                recyclerView.scrollToPosition(0);
                return true;
            }
        });

        searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
            @Override
            public void onSearchViewShown() {
            }

            @Override
            public void onSearchViewClosed() {
                mAdapter.setFilter(mPeopleList);
            }
        });

    }
    
    
     /**
     * 搜索按鈕
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        searchView.setMenuItem(item);

        return true;
    }

篩選邏輯

/**
     * 篩選邏輯
     * @param peoples
     * @param query
     * @return
     */
    private List<People> filter(List<People> peoples, String query) {
        query = query.toLowerCase();

        final List<People> filteredModelList = new ArrayList<>();
        for (People people : peoples) {

            final String nameEn = people.getName().toLowerCase();
            final String desEn = people.getDescription().toLowerCase();
            final String name = people.getName();
            final String des = people.getDescription();

            if (name.contains(query) || des.contains(query) || nameEn.contains(query) || desEn.contains(query)) {

                filteredModelList.add(people);
            }
        }
        return filteredModelList;
    }
相關文章
相關標籤/搜索