Android - 如何防止SearchView建议中的文本截断?

7
默认的ListView搜索建议下面的文本被截断了,我希望能显示完整的文本(如果需要的话可以显示在多行上)。
我想到两种解决方法,但是在网上找不到任何示例,希望这里的某个人能够帮助我...
方法一/ Q1:如何直接访问和修改包含"SUGGEST_COLUMN_TEXT_1"和“SUGGEST_COLUMN_TEXT_2”文本的TextView的外观?
方法二 / Q2:另外,SearchView有一个 "setSuggestionsAdapter(CursorAdapter adapter)" 方法,看起来可能比方法一更合适。虽然我已经在我的应用中实现了CursorAdapter,并且已经阅读了相关内容,但我不确定如何为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>

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


你是在说你有一个可行的解决方案,但文本截断仍然是问题吗? - user2511882
我的代码显示了建议,但它们被截断了。我需要一个解决方案来克服这个截断问题。 - ban-geoengineering
建议内容的数据来自何处?SearchableInfo 的文档说它应该用于在不同的应用程序之间进行搜索。如果您已经使用游标适配器并显示建议,那么更改 getView 中使用的布局就很简单了。也许您目前正在使用默认的 android.R.layout... 布局。 - darnmason
3个回答

5

看起来您想要自定义搜索视图结果的布局。我会试着概述一些明确的步骤:

为了使搜索视图正常工作,我们需要在res/xml文件夹中拥有一个searchable.xml和一个内容提供者

示例searchable.xml:

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

在清单文件中的活动中添加intent-filter元数据。例如:

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

在活动/片段中,获取您的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 文件,您可以用它来填充搜索视图结果。请确保使用了内容提供程序,否则 searchView 将无法工作,即使您只是在临时基础上缓存数据也没有关系。希望这可以帮助到您!

1
谢谢您的帮助。您的代码中有一两个错别字和一些已弃用的代码,但它确实指引了我正确的方向 - 主要是 searchView.setSuggestionsAdapter()。我很快就会更新我的问题并分享我的可行代码。谢谢! :-) - ban-geoengineering
我之前曾经使用过searchView,但是对于那些已经被弃用的方法并不是很了解。非常抱歉。如果有任何拼写错误,请告诉我,我一定会更新答案。谢谢! - user2511882

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


0

我认为您需要创建一个CustomAdapter来访问您自己的布局来完成这个任务。令人惊讶的是,修改这一点并不容易。SuggestionsAdapter不是公共的,所以您无法扩展它。虽然不太美观,但最简单的解决方案是将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的回答可能比这个快速解决方案更好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接