我正在尝试在列表视图中实现搜索视图。我有1个活动,其中包含带有歌曲的文件夹的列表视图。在searchview中,我试图搜索可以出现在任何文件夹中的歌曲。我尝试实现Filterable但我很困惑如何实现。我知道使用适配器和过滤器的常规搜索。但不像MX播放器那样。附加屏幕截图。
如图所示,搜索视图正在搜索任何文件夹中的歌曲。
以下是我尝试的代码:
适配器类:
private ArrayList<VideosModel> videos;
private ArrayList<VideosModel> totalVideos;
public VideoHomeAdapter(Context c, ArrayList<VideosModel> video,ArrayList<VideosModel> totalVideo) {
super();
this.mContext = c;
this.videos = video;
this.glossariesListForSearch = totalVideo;
this.totalVideos = totalVideo;
}
@Override
public int getCount() {
return videos.size();
}
@Override
public Filter getFilter() {
return myFilter;
}
Filter myFilter = new Filter() {
@SuppressWarnings("unchecked")
@Override
public void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null) {
totalVideos = (ArrayList<VideosModel>) results.values;
if (results.count > 0) {
// no idea what to write here .
// totalVideos is the list with all the videos
} else {
}
}
}
@Override
public FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<VideosModel> tempGlossaryList = new ArrayList<VideosModel>();
if (constraint != null && glossariesListForSearch != null) {
int length = glossariesListForSearch.size();
int i = 0;
while (i < length) {
VideosModel item = glossariesListForSearch.get(i);
// Real filtering:
if (item.getVideoTitle()
.toLowerCase(Locale.getDefault())
.contains(
constraint.toString().toLowerCase(
Locale.getDefault()))) {
tempGlossaryList.add(item);
}
i++;
}
filterResults.values = tempGlossaryList;
filterResults.count = tempGlossaryList.size();
}
return filterResults;
}
};
答案 0 :(得分:0)
最后使用此链接使用AutoCompleteTextView而不是SearchView解决了它:AutoCompleteTextView
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
ActionBar actionBar = getSherlockActivity().getSupportActionBar(); // you
// can
// use
// ABS
// or
// the
// non-bc
// ActionBar
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP); // what's mainly important here
// is DISPLAY_SHOW_CUSTOM.
// the rest is optional
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the view that we created before
final View view = inflater.inflate(R.layout.actionbar_search, null);
final ImageView searchIcon = (ImageView) view
.findViewById(R.id.search_icon);
final ClearableAutoCompleteTextView searchBox = (ClearableAutoCompleteTextView) view
.findViewById(R.id.search_box);
// start with the text view hidden in the action bar
searchBox.setVisibility(View.INVISIBLE);
searchIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleSearch(false, view);
}
});
searchBox.setOnClearListener(new OnClearListener() {
@Override
public void onClear() {
toggleSearch(true, view);
}
});
searchBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// handle clicks on search results here
Intent intent = new Intent(getSherlockActivity(),
SearchVideoResult.class);
VideosModel model = searchAdapter.getItem(position);
// intent.putExtra("folderPath",model.getFolderFullPath());
intent.putExtra("videoId", model.getVideoId());
startActivity(intent);
searchBox.setText("");
searchBox.setVisibility(View.INVISIBLE);
searchIcon.setVisibility(View.VISIBLE);
}
});
actionBar.setCustomView(view);
}