• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

krishnakapil/MaterialSeachView: Android Material Design Style Custom Search View

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

krishnakapil/MaterialSeachView

开源软件地址(OpenSource Url):

https://github.com/krishnakapil/MaterialSeachView

开源编程语言(OpenSource Language):

Java 100.0%

开源软件介绍(OpenSource Introduction):

MaterialSeachView

Android Material Design Style Custom Search View

![Screenshot 1] (https://github.com/krishnakapil/MaterialSeachView/blob/master/material-search.png) ![Screenshot 2] (https://github.com/krishnakapil/MaterialSeachView/blob/master/material-search2.png)

#Usage Copy the material-search.aar file into your project libs folder. Add the following dependency into gradle.

repositories {
        flatDir {
            dirs 'libs'
        }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name:'material-search', ext:'aar')
}

Add MaterialSearchView to your layout file

<RelativeLayout 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"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/darker_gray"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <com.search.material.library.MaterialSearchView
        android:id="@+id/search_view"
        style="@style/MaterialSearchViewStyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Get reference to it in the Activity and set adapter. Adapter should implement Filterable.

(Check MaterialSeachView/app/src/main/java/com/search/material/materialsearch)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ....
        MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
        
        SearchAdapter adapter = new SearchAdapter();
        searchView.setAdapter(adapter);
        
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

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

            }

            @Override
            public void onSearchViewClosed() {

            }
        });

        searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        MenuItem item = menu.findItem(R.id.action_search);
        searchView.setMenuItem(item);

        return true;
    }

Handle Voice search

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (matches != null && matches.size() > 0) {
                String searchWrd = matches.get(0);
                if (!TextUtils.isEmpty(searchWrd)) {
                    searchView.setQuery(searchWrd, false);
                }
            }

            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

Closing Search when back pressed

@Override
    public void onBackPressed() {
        if (searchView.isSearchOpen()) {
            searchView.closeSearch();
        } else {
            super.onBackPressed();
        }
    }

#Styling Material Search View

<style name="MaterialSearchViewStyle">
        <!-- Background for the search bar-->
        <item name="searchBackground">@android:color/white</item>
        
        <!-- Change voice icon-->
        <item name="searchVoiceIcon">@drawable/ic_action_voice_search</item>
        
        <!-- Change clear text icon-->
        <item name="searchCloseIcon">@drawable/ic_action_navigation_close</item>
        
        <!-- Change up icon-->
        <item name="searchBackIcon">@drawable/ic_action_navigation_arrow_back</item>
        
        <!-- Change background for the suggestions list view-->
        <item name="searchSuggestionBackground">...</item>
        
        <!-- Change text color for edit text. This will also be the color of the cursor-->
        <item name="android:textColor">@android:color/black</item>
        
        <!-- Change hint text color for edit text-->
        <item name="android:textColorHint">@android:color/black</item>
        
        <!-- Hint for edit text-->
        <item name="android:hint">@string/search_hint</item>
 </style>



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap