如何在MaterialSearchView中添加项目单击的侦听器

时间:2017-07-18 16:19:53

标签: java android onclicklistener searchview onitemclicklistener

如何在MaterialSearchView中添加项目点击的监听器

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grids);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


        sharedPreferences = getSharedPreferences(MyPref, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        locationStatus =sharedPreferences.getString(locationStatus,"");
searchView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s=(String)parent.getItemAtPosition(position);
        Toast.makeText(getBaseContext(),s,Toast.LENGTH_LONG).show();

    }
});

我尝试了这段代码,但项目点击监听器无效

1 个答案:

答案 0 :(得分:0)

我以前确实有这样的问题,我从this guys

中找到了解决方案。

长话短说,我只是将其放在代码中

  

searchView.setSubmitOnClick(true);

它将提交提示文本作为查询,或者您可以添加它以获取侦听器

searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
    @Override
    public void onSearchViewShown() {
        //Do some magic
    }

    @Override
    public void onSearchViewClosed() {

    }
});