简体   繁体   中英

How to add listener for item click in MaterialSearchView

How to add listener for item click in 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();

    }
});

I tried this code but item click listener is not working

I do have a problem like this before, and I find out the soulution from this guys

Long short story I just put this on my code

searchView.setSubmitOnClick(true);

and it'll submit the sugestion text as query or you can add this to get the listener

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

    @Override
    public void onSearchViewClosed() {

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM