Android - 在SearchView建议中防止文本截断?

时间:2014-12-27 22:17:49

标签: android customization android-cursoradapter searchview search-suggestion

ListView下方默认SearchView中显示的建议包含截断的文字。我希望文本完整显示(如果需要,可以在多行上显示)。

我已经提出了两种可能的方法来解决这个问题但是,由于没有在网上找到的例子,我希望有人能够帮助...

方法#1 / Q1:如何直接访问和修改包含SUGGEST_COLUMN_TEXT_1SUGGEST_COLUMN_TEXT_1文字的TextView的外观?

方法#2 / Q2:或者,SearchView有一个setSuggestionsAdapter(CursorAdapter adapter)方法,看起来比方法#1更适合(更多?)。虽然我已经阅读了CursorAdapters并且已经在我的应用程序中实现了一个,但我不确定如何为SearchView配置一个(特别是在访问游标方面),所以任何人都可以帮我解决一些一般指导还是一个骨架的例子?

以下是我的SearchViewFragment类的现有代码:

public class SearchViewFragment extends Fragment {

public SearchViewFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View fragmentView =  inflater.inflate(R.layout.fragment_search_view, container, false);

    // Use the Search Manager to find the SearchableInfo related to this Activity
    SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());

    // Bind the Activity's SearchableInfo to the Search View
    SearchView searchView = (SearchView)fragmentView.findViewById(R.id.searchView);
    searchView.setSearchableInfo(searchableInfo);
    searchView.setIconifiedByDefault(false);
    searchView.setSubmitButtonEnabled(true);
    //searchView.setQueryRefinementEnabled(true);

    return fragmentView;
}

}

更新:已解决!

感谢接受的答案,我已经创建了这个代码非常干净并且做得很好......

public class SearchViewFragment extends Fragment {

    private static final String LOG_TAG = SearchViewFragment.class.getSimpleName();

    public SearchViewFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true); //todo - not working - Remember search term
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View fragmentView =  inflater.inflate(R.layout.fragment_search_view, container, false);

        // Use the Search Manager to find the SearchableInfo related to this Activity
        SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());

        // Bind the Activity's SearchableInfo to the Search View
        final SearchView searchView = (SearchView)fragmentView.findViewById(R.id.searchView);
        searchView.setSearchableInfo(searchableInfo);
        searchView.setIconifiedByDefault(false);
        searchView.setSubmitButtonEnabled(true);
        //searchView.setQueryRefinementEnabled(true);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
               //DO whatever you want here on text submit in the search View
                Log.d(LOG_TAG, "onQueryTextSubmit(" + s + ")");

                return true;
            }
            @Override
            public boolean onQueryTextChange(String textChange) {

                Log.d(LOG_TAG, "onQueryTextChange(" + textChange + ")");

                ContentResolver cr = getActivity().getContentResolver();

                Uri uri = DbContentProvider.CONTENT_URI_REAL_PRODUCTS;
                String[] projection = DbContentProvider.getProjectionIn(DbContentProvider.REAL_PRODUCTS_SUGGEST);
                String selection = DbContentProvider.getSelection(false);
                String[] selectionArgs = {Utilities.formatQueryString(textChange)};
                String sortOrder = null;
                Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
                Log.d(LOG_TAG, "Setting setSuggestionsAdapter. cursor: " + cursor);
                searchView.setSuggestionsAdapter(new SearchSuggestionsAdapter(getActivity(), cursor));

                return true;

            }
        });

        return fragmentView;
    }

    private static class SearchSuggestionsAdapter extends SimpleCursorAdapter {

        private static final String[] mVisible = {SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2};
        private static final int[] mViewIds = {R.id.product_name, R.id.product_shelf};

        public SearchSuggestionsAdapter(Context context, Cursor cursor) {

            super(context, R.layout.search_view_suggestions, cursor, mVisible, mViewIds, 0);

        }
        /*
        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            Log.d(LOG_TAG, "bindView(" + view + ", " + context + ", " + cursor + ")");
            super.bindView(view, context, cursor);

        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            Log.d(LOG_TAG, "newView(" + context + ", " + cursor + ", " + parent + ")");
            return super.newView(context, cursor, parent);

        }
        */

    }

}

这是我的search_view_suggestions.xml ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/product_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product Name placeholder"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/product_shelf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product Shelf placeholder" />

</LinearLayout>

...结果是没有文本截断。 : - )

3 个答案:

答案 0 :(得分:4)

您似乎想要一个自定义布局用于搜索视图结果。我将尝试概述以下一些明确的步骤:

要使搜索视图生效,我们需要searchable.xml中的res/xml foldercontent provider。{/ p>

searchable.xml示例:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:searchSuggestAuthority="com.example.searchProvider" ></searchable>

labelsearchSuggestAuthority是必需的。 searchSuggestAuthority应指向内容提供商的位置

将intent-filter元数据添加到清单中的活动。例如:

  <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>

在activity / fragment中,获取searchView对象并将其传递给自定义适配器以显示自定义布局。

searchView.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
        final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
               //DO whatever you want here on text submit in the search View

                return true;
            }
 @Override
            public boolean onQueryTextChange(String textChange) {


                searchView.setSuggestionsAdapter(new ExampleAdapter(context,yourData));

            }
        };

确保自定义适配器扩展了cursorAdapter。

public class ExampleAdapter extends CursorAdapter {

    private Cursor cursor;

    private TextView text;

    public ExampleAdapter(Context context, Cursor cursor) {

        super(context, cursor);

        this.cursor= cursor;

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        text.setText((cursor.getString(cursor.getColumnIndex(YOUR_STRING_KEY));));

    }

    @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.item, parent, false);

        text = (TextView) view.findViewById(R.id.text);

        return view;

    }

}

在上面的代码R.layout.item中,您可以使用自定义xml文件在搜索结果结果中进行扩充。

确保您使用的是content provider。如果没有searchView,{{1}}就无法运作。即使您只是临时缓存数据也没有关系。

希望这有帮助!

答案 1 :(得分:2)

user2511882的答案很完美,但该构造函数已被弃用。

Deprecated :
public ExampleAdapter(Context context, Cursor cursor) {
    super(context, cursor);
    this.cursor= cursor;
}

你可以使用下面的那个而不是那个。

public ExampleAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
}

然后像这样打电话。

searchView.setSuggestionsAdapter(new ExampleAdapter(context,YOUR_DATA,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));

https://developer.android.com/reference/android/widget/CursorAdapter.html

答案 2 :(得分:0)

我认为您需要创建一个可以访问您自己的布局的CustomAdapter来执行此操作。令人惊讶的是,这并没有变得容易修改。建议适配器不公开,因此您无法对其进行扩展。它并不漂亮,但最简单的解决方案是将SuggestionsAdapter.java复制到您自己的项目中,并将其指向您自己的res / layouts中的布局。我会再次从Android源代码中复制该版本并根据您的需要进行修改。

Android 5版本的修改更糟糕,所以这是KitKat版本: http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/widget/SuggestionsAdapter.java/?v=source

将此行更改为指向布局文件:

super(context,
        com.android.internal.R.layout.search_dropdown_item_icons_2line, // make it you layout xml (see below)
        null,   // no initial cursor
        true);  // auto-requery

http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/frameworks/base/core/res/res/layout/search_dropdown_item_icons_2line.xml/?v=source

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingStart="@dimen/dropdownitem_text_padding_left"
    android:paddingEnd="4dip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" > <!-- changed -->

同样在布局文件中:

<TextView android:id="@android:id/text2"
    style="?android:attr/dropDownItemStyle"
    android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle"
    <!-- android:singleLine="true" --> <!-- remove this line -->
    android:layout_width="match_parent"
    android:layout_height="wrap_content" <!-- changed -->

user2511882的答案可能比这个快速黑客更好。

相关问题