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

时间:2016-03-02 12:04:12

标签: android onclick android-search search-suggestion

我在此question的答案后面包含了一个搜索视图。我创建了一个自定义建议项,其中包含文本和ImageButton。重要的代码如下:

过滤活动,在此课程中,搜索视图已初始化,我有OnQuery Listeners。

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。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分: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