Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class SearchResultsActivity extends BaseActivity implements ViewHolderResponder {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
View emptyView = new View(this);
mAdapter = new SearchResultsAdapter(emptyView, new WeakReference<ViewHolderResponder>(this));
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void didClickOnView(View view, int position) {
Log.d("Clicked", "Clicked");
if (view.getId() == R.id.item_title) {
Log.d("Clicked Title", "Clicked Title");
}
}
@Override
public void didLongClickOnView(View view, int position) {
Log.d("Long Clicked", "Clicked");
}
public static class SearchResultsAdapter extends RecyclerView.Adapter<SearchResultsAdapter.SearchResultsViewHolder> {
private final View empty;
private List<Item> mItems = new ArrayList<>();
private WeakReference<ViewHolderResponder> mResponder;
public SearchResultsAdapter(final View empty, WeakReference<ViewHolderResponder> activity) {
this.empty = empty;
this.mResponder = activity;
}
@UiThread
public void addItem(final Item item) {
this.mItems.add(item);
this.dataSetChanged();
}
@UiThread
public void addItems(final List<Item> items) {
this.mItems.addAll(items);
this.dataSetChanged();
}
@UiThread
public void clearItems() {
this.mItems.clear();
this.dataSetChanged();
}
public SearchResultsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final ItemSearchResultBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_search_result, parent, false);
return new SearchResultsViewHolder(binding.getRoot(), binding, mResponder);
}
@Override
public void onBindViewHolder(SearchResultsViewHolder holder, int position) {
final Item item = this.mItems.get(position);
holder.bind(item, position);
}
@Override
public int getItemCount() {
return mItems.size();
}
private void dataSetChanged() {
this.notifyDataSetChanged();
this.empty.setVisibility(this.getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
public static class SearchResultsViewHolder extends RecyclerView.ViewHolder {
private final ItemSearchResultBinding binding;
private TextView title;
private Integer position;
private WeakReference<ViewHolderResponder> mResponder;
public SearchResultsViewHolder(final View view, final ItemSearchResultBinding binding, WeakReference<ViewHolderResponder> responder) {
super(view);
title = (TextView) view.findViewById(R.id.item_title);
this.binding = binding;
mResponder = responder;
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mResponder.get().didClickOnView(view, position);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mResponder.get().didClickOnView(view, position);
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mResponder.get().didClickOnView(view, position);
return true;
}
});
}
@UiThread
public void bind(final Item item, Integer position) {
this.binding.setItem(item);
this.position = position;
}
}
}
}
@RTimal
Copy link
Author

RTimal commented Sep 5, 2015


public interface ViewHolderResponder {
    public void didClickOnView(View view, int position);
    public void didLongClickOnView(View view, int position);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment