标签云

微信群

扫码加入我们

WeChat QR Code

I'm including a search view following the answer to this question. I've created a custom suggestion item which includes a text and an ImageButton. The important code is below:Filter Activity, in this class the search view is been initialize, and I have the OnQuery Listeners.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){@Overridepublic boolean onQueryTextSubmit(String query){if (Utils.isQueryOk(query)){....}return true;}@Overridepublic boolean onQueryTextChange(String newText){if (newText.length() > 1){new getSuggestionsFromServer().execute(newText);} return true;}});private class getSuggestionsFromServer extends AsyncTask<String, Void, Void>{List<String> suggestions;@Overrideprotected Void doInBackground(String... params){// Retrieve suggestions from serverreturn null;}@Overrideprotected void onPostExecute(Void unused){String[] columns = new String[] { "_id", "text" };Object[] temp = new Object[] { 0, "default" };MatrixCursor cursor = new MatrixCursor(columns);for(int i = 0; i < suggestions.size(); i++){temp[0] = i;temp[1] = suggestions.get(i);cursor.addRow(temp);}SearchManager manager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);final SearchView search = (SearchView)mMenu.findItem(R.id.menu_item_search).getActionView();search.setOnSuggestionListener(new SearchView.OnSuggestionListener(){@Overridepublic boolean onSuggestionSelect(int position){return true;}@Overridepublic boolean onSuggestionClick(int position){// This is not getting called!Log.d(TAG, "SuggestClick: " + search.getSuggestionsAdapter().getCursor().getString(1));return true;}});search.setSuggestionsAdapter(new SuggestionAdapter(FilterUI.this, cursor, suggestions));search.getSuggestionsAdapter().notifyDataSetChanged();}} /* [END getSuggestionsFromServer] */SuggestionAdapter classpublic class SuggestionAdapter extends CursorAdapter{private List<String> items;private TextView text;private ImageButton include;public SuggestionAdapter(Context context, Cursor cursor, List<String> items){super(context, cursor, false);this.items = items;}@Overridepublic void bindView(View view, Context context, Cursor cursor){text.setText(items.get(cursor.getPosition()));}@Overridepublic View newView(Context context, Cursor cursor, ViewGroup parent){LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View view = inflater.inflate(R.layout.suggestion_item, parent, false);text= (TextView)view.findViewById(R.id.suggestion_item);include = (ImageButton)view.findViewById(R.id.suggestion_include);return view;}}Suggestion Item XML<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/white"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:background="@drawable/suggestion_bottom_border"android:layout_marginLeft="16dp"><TextViewandroid:id="@+id/suggestion_item"android:layout_width="0dp"android:layout_weight="0.8"android:layout_height="wrap_content"android:textSize="14dp"android:padding="16dp"android:textColor="@color/colorMediumText"/><ImageButtonandroid:id="@+id/suggestion_include"android:layout_width="24dp"android:layout_height="24dp"android:layout_margin="16dp"android:scaleType="centerCrop"android:src="@drawable/ic_call_made_black"android:alpha="0.4"android:background="@android:color/transparent"/></LinearLayout></LinearLayout>So, what I want to do is listen whether it's clicked on the textview, or the ImageButton. How can I achieve this?


Please, can you elaborate more your answer?

2019年04月20日52分58秒