Skip to content

Instantly share code, notes, and snippets.

@JulienGenoud
Last active October 20, 2016 09:33
Show Gist options
  • Save JulienGenoud/36f931197a2eb4bb026fb1945347c1a6 to your computer and use it in GitHub Desktop.
Save JulienGenoud/36f931197a2eb4bb026fb1945347c1a6 to your computer and use it in GitHub Desktop.
public class RecyclerArrayAdapter extends RecyclerView.Adapter<RecyclerArrayAdapter.ArrayAdaperViewHolder> {
private List<String> mObjects;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(String query);
}
public RecyclerArrayAdapter(final List<String> objects, OnItemClickListener listener) {
mObjects = objects;
mListener = listener;
}
static class ArrayAdaperViewHolder extends RecyclerView.ViewHolder {
ArrayAdaperViewHolder(View itemView) {
super(itemView);
}
}
@Override
public ArrayAdaperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_search_widget, parent, false);
return new ArrayAdaperViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerArrayAdapter.ArrayAdaperViewHolder holder, final int position) {
((TextView) holder.itemView.getRootView()).setText(mObjects.get(position));
holder.itemView.getRootView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(mObjects.get(position));
}
});
}
@Override
public int getItemCount() {
return mObjects.size();
}
public void addAll(@NonNull final List<String> objects) {
for (String object : objects) {
mObjects.add(object);
}
notifyDataSetChanged();
}
public void clear() {
mObjects.clear();
notifyDataSetChanged();
}
public void remove(String s) {
mObjects.remove(mObjects.indexOf(s));
notifyDataSetChanged();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment