我按照this示例在Android应用中显示youtube视频 我坚持在其中实现一个搜索栏。如何实现搜索栏的逻辑?我试图实现可搜索接口,但在设置部分失败了。
这是适配器:
public class PlaylistCardAdapter extends RecyclerView.Adapter<PlaylistCardAdapter.ViewHolder> {
.......
public static class ViewHolder extends RecyclerView.ViewHolder {
public PlaylistCardAdapter(PlaylistVideos playlistVideos, YouTubeRecyclerViewFragment.LastItemReachedListener lastItemReachedListener) {
mPlaylistVideos = playlistVideos;
mListener = lastItemReachedListener;
}
// Create new views (invoked by the layout manager)
@Override
public PlaylistCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// inflate a card layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_video_card, parent, false);
// populate the viewholder
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (mPlaylistVideos.size() == 0) {
return;
}
final Video video = mPlaylistVideos.get(position);
final VideoSnippet videoSnippet = video.getSnippet();
holder.mTitleText.setText(videoSnippet.getTitle());
if (mListener != null) {
// get the next playlist page if we're at the end of the current page and we have another page to get
final String nextPageToken = mPlaylistVideos.getNextPageToken();
if (!isEmpty(nextPageToken) && position == mPlaylistVideos.size() - 1) {
holder.itemView.post(new Runnable() {
@Override
public void run() {
mListener.onLastItem(position, nextPageToken);
}
});
}
}
}
@Override
public int getItemCount() {
return mPlaylistVideos.size();
}
private boolean isEmpty(String s) {
if (s == null || s.length() == 0) {
return true;
}
return false;
}
这是片段类的链接 - link
答案 0 :(得分:0)
此解决方案包括在适配器中具有两个播放列表副本。一个是传递给适配器的完整原始列表,另一个是将具有从应用搜索中选择的项目的列表。
第二个列表是用户提交搜索时将更新的列表,也是Recycler视图中显示的列表。
在以下示例中:
Xxxxx
将是您案例中的播放列表项。
originalData
保留完整的项目列表。
setData()
以使用新的项列表更新RecyclerView。
setSearchText()
来传递文本以过滤回收者视图项目
这可以使用要搜索的完整文本进行调用,或者每次用户在键入EditText时释放密钥。
refreshData()
调用applySearch()
进行过滤,然后使用找到的项目更新屏幕上的RecyclerView。
public class XxxxxsRecyclerAdapter extends RecyclerView.Adapter<XxxxxsRecyclerAdapter.ViewHolder> {
private ArrayList<Xxxxx> data;
private ArrayList<Xxxxx> originalData;
private Context context;
private String searchText;
public XxxxxsRecyclerAdapter(Context context, ArrayList<Xxxxx> data) {
super();
this.context = context;
this.data = data;
this.originalData = new ArrayList<>();
if(this.data != null) {
this.originalData.addAll(this.data);
}
}
public void setData(ArrayList<Xxxxx> data) {
this.data = data;
this.originalData.clear();
if (data != null) {
this.originalData.addAll(this.data);
}
refreshData();
}
public void setSearchText(String searchText){
this.searchText = searchText;
refreshData();
}
private void applySearch(){
if(data != null) {
if (searchText != null && !searchText.equals("")) {
ArrayList<Xxxxx> tmp = new ArrayList<>();
tmp.addAll(data);
data.clear();
for (Xxxxx xxxxx : tmp) {
if (xxxxx.getNombre().toUpperCase().contains(searchText.toUpperCase())) {
data.add(xxxxx);
}
}
}
}
}
private void refreshData(){
this.applySearch();
this.notifyDataSetChanged();
}
...
}