使用自定义建议项处理点击侦听器



我在回答这个问题后包含一个搜索视图。我创建了一个自定义建议项,其中包括文本和图像按钮。重要代码如下:

过滤器活动,在此类中,搜索视图已初始化,并且我有 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] */

建议适配器类

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>

所以,我想做的是听听它是点击文本视图还是图像按钮。我怎样才能做到这一点?

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

最新更新