Android-防止SearchView建议中的文本截断?
出现在myAndroid-防止SearchView建议中的文本截断?,android,customization,android-cursoradapter,searchview,search-suggestion,Android,Customization,Android Cursoradapter,Searchview,Search Suggestion,出现在mySearchView下的默认ListView中的建议包含被截断的文本。我希望文本能够完整显示(如有必要,可以多行显示) 我已经想出了两种可能的方法来解决这个问题,但是,由于在网上找不到任何例子,我希望这里的人能够提供帮助 方法#1/Q1:如何直接访问和修改包含建议列(文本)1和建议列(文本)1文本的文本视图的外观 方法#2/Q2:或者,SearchView有一个SetSuggestionAdapter(游标适配器)方法,看起来它可能比方法#1更合适。虽然我已经阅读了CursorAdap
SearchView
下的默认ListView
中的建议包含被截断的文本。我希望文本能够完整显示(如有必要,可以多行显示)
我已经想出了两种可能的方法来解决这个问题,但是,由于在网上找不到任何例子,我希望这里的人能够提供帮助
方法#1/Q1:如何直接访问和修改包含建议列(文本)1
和建议列(文本)1
文本的文本视图的外观
方法#2/Q2:或者,SearchView有一个SetSuggestionAdapter(游标适配器)
方法,看起来它可能比方法#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);
}
*/
}
}
这是我的搜索视图建议。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>
…结果是没有文本截断。:-) 您似乎希望为搜索视图结果提供自定义布局。我将尝试在下面概述一些明确的步骤: 要使搜索视图正常工作,我们需要在
res/xml文件夹中始终有一个searchable.xml
,以及一个content provider
searchable.xml示例:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:searchSuggestAuthority="com.example.searchProvider" ></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));
}
};
确保自定义适配器扩展游标适配器
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结果中充气
不过,请确保您使用的是内容提供商。没有搜索视图,searchView
就无法工作。即使只是临时缓存数据,也无所谓
希望这有帮助 我认为您需要创建一个CustomAdapter来访问您自己的布局来执行此操作。令人惊讶的是,这并没有变得更容易修改。SuggestionAdapter不是公共的,因此您无法扩展它。这并不漂亮,但最简单的解决方案是将SuggestionsAdapter.java复制到您自己的项目中,并将其指向您自己的res/layouts中的布局。我会再次复制Android源代码的版本,并根据您的需要进行修改
Android 5版本的修改更糟糕,因此这是KitKat版本:
更改此行以指向布局文件:
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
也在布局文件中:
<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 -->
android:layout\u width=“匹配父项”
android:layout\u height=“包装内容”
User251182的答案可能比这个快速破解更好 的答案是完美的,但该构造函数已被弃用
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));
您是说您有一个解决方案可以工作,但文本截断仍然是个问题吗?我的代码显示了这些建议,但它们被截断了。我需要一个解决方案来克服截断问题。建议的数据来自哪里?SearchableInfo
的文档中说,它应该用于在不同的应用程序之间进行搜索。如果光标适配器已启动并运行并显示建议,那么更改getView
中使用的布局就很简单了。也许您当前使用的是默认的android.R.layout…
oneThanks。您的代码包含一两个打字错误和一些不推荐的代码,但它确实为我指明了正确的方向-主要是searchView.setSuggestionAdapter()
。我将很快用我的工作代码更新我的问题。干杯!:-)很早以前使用过searchView,所以没有太多更新过的不推荐的方法。。很抱歉。让我知道有什么拼写错误,我当然可以更新答案。干杯
public ExampleAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
searchView.setSuggestionsAdapter(new ExampleAdapter(context,YOUR_DATA,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));