2016-03-02 38 views
0

我在回答这个question后包含搜索视图。我创建了一个包含文本和ImageButton的自定义建议项目。重要的代码如下:使用自定义建议项目处理点击听众

过滤器活动,在这个类中搜索视图已初始化,并且我有OnQuery监听器。

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
{ 
    @Override 
    public boolean onQueryTextSubmit(String query) 
    { 
     if (Utils.isQueryOk(query)) 
     { 
      .... 
     } 

     return true; 
    } 

    @Override 
    public 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; 

    @Override 
    protected Void doInBackground(String... params) 
    { 
     // Retrieve suggestions from server 

     return null; 
    } 

    @Override 
    protected 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() 
     { 
      @Override 
      public boolean onSuggestionSelect(int position) 
      { 
       return true; 
      } 

      @Override 
      public 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类

public 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; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) 
    { 
     text.setText(items.get(cursor.getPosition())); 
    } 

    @Override 
    public 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; 
    } 
} 

建议项目XML

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@drawable/suggestion_bottom_border" 
     android:layout_marginLeft="16dp"> 

     <TextView 
      android: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"/> 

     <ImageButton 
      android: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> 

所以,我想要做的就是听它是否点击了TextView的,或者ImageButton的真实。我怎样才能做到这一点?

回答

0
public 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; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) 
    { 
     text.setText(items.get(cursor.getPosition())); 
    } 

    @Override 
    public 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; 
    } 
} 


public class Filter extends Activity{ 


searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
{ 
    @Override 
    public boolean onQueryTextSubmit(String query) 
    { 
     if (Utils.isQueryOk(query)) 
     { 
      .... 
     } 

     return true; 
    } 

    @Override 
    public 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; 

    @Override 
    protected Void doInBackground(String... params) 
    { 
     // Retrieve suggestions from server 

     return null; 
    } 

} 


Implemet Suggesion Adapter within the Filter Activity 
+0

请问,您能详细解释一下您的答案吗? – cuoka