Skip to content

Instantly share code, notes, and snippets.

@alorma
Last active March 3, 2021 20:29
Show Gist options
  • Save alorma/4cc4b4c26653aa4d6031af7abf5bf5f3 to your computer and use it in GitHub Desktop.
Save alorma/4cc4b4c26653aa4d6031af7abf5bf5f3 to your computer and use it in GitHub Desktop.
public class GlueItemsAdapter extends RecyclerArrayAdapter<GlueItem, GlueItemsAdapter.Holder> {
public GlueItemsAdapter(LayoutInflater inflater) {
super(inflater);
}
@Override
protected Holder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(android.R.layout.simple_list_item_1, parent, false));
}
@Override
protected void onBindViewHolder(Holder holder, GlueItem glueItem) {
holder.textView.setText(glueItem.getText());
}
public class Holder extends RecyclerView.ViewHolder {
@BindView(android.R.id.text1) TextView textView;
public Holder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public abstract class RecyclerArrayAdapter<ItemType, ViewHolder extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<ViewHolder> {
private static final int DEFAULT_VIEW_TYPE = 0;
private static final int LAZY_LOAD_MIN_COUNT = 1;
public List<ItemType> items;
private LayoutInflater inflater;
private RecyclerAdapterContentListener recyclerAdapterContentListener;
private ItemCallback<ItemType> callback;
private boolean returnResult;
public RecyclerArrayAdapter(LayoutInflater inflater) {
this.inflater = inflater;
items = new ArrayList<>();
}
@Override
public int getItemCount() {
return items.size();
}
public ItemType getItem(int position) {
return items.get(position);
}
public void add(ItemType itemType) {
items.add(itemType);
notifyDataSetChanged();
}
public void addAll(Collection<? extends ItemType> items) {
this.items.addAll(items);
notifyDataSetChanged();
}
public void clear() {
items.clear();
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return onCreateViewHolder(getInflater(), parent, viewType);
}
protected abstract ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
onBindViewHolder(holder, getItem(position));
if (recyclerAdapterContentListener != null && position + lazyLoadCount() >= getItemCount()) {
recyclerAdapterContentListener.loadMoreItems();
}
}
@Override
public int getItemViewType(int position) {
return getItemViewType(items.get(position));
}
protected int getItemViewType(ItemType itemType) {
return DEFAULT_VIEW_TYPE;
}
protected abstract void onBindViewHolder(ViewHolder holder, ItemType type);
protected int lazyLoadCount() {
return LAZY_LOAD_MIN_COUNT;
}
public LayoutInflater getInflater() {
return inflater;
}
public void setInflater(LayoutInflater inflater) {
this.inflater = inflater;
}
public void remove(ItemType itemType) {
items.remove(itemType);
notifyDataSetChanged();
}
public List<ItemType> getItems() {
return items;
}
public void setRecyclerAdapterContentListener(RecyclerAdapterContentListener recyclerAdapterContentListener) {
this.recyclerAdapterContentListener = recyclerAdapterContentListener;
}
public ItemCallback<ItemType> getCallback() {
if (callback == null) {
callback = item -> {
};
}
return callback;
}
public void setCallback(ItemCallback<ItemType> callback) {
this.callback = callback;
}
public void setReturn(boolean aReturn) {
this.returnResult = aReturn;
}
public boolean isReturnResult() {
return returnResult;
}
public void setReturnResult(boolean returnResult) {
this.returnResult = returnResult;
}
public interface RecyclerAdapterContentListener {
void loadMoreItems();
}
public interface ItemCallback<ItemType> {
void onItemSelected(ItemType item);
}
}
@raulh82vlc
Copy link

raulh82vlc commented Aug 9, 2016

to improve the performance I would introduce the following on remove:

 public void remove(int position) {
    items.remove(position);
    notifyItemRemoved(position);
  }

as well as on add:

  public void add(ItemType itemType) {
    int position = items.size();    
    items.add(itemType);
    notifyItemInserted(position);
  }

Both notifyItemXXX are performing a less costly operation when notifying the adapter that something has changed, in addition to this, the animation is still happening.

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