Skip to content

Instantly share code, notes, and snippets.

@Sirelon
Created April 22, 2016 08:22
Show Gist options
  • Save Sirelon/f5580a47c9c138785e855941998d82c1 to your computer and use it in GitHub Desktop.
Save Sirelon/f5580a47c9c138785e855941998d82c1 to your computer and use it in GitHub Desktop.
Parent class fot my ModelAdapter which showing progress, empty view.
package com.sirelon;
import android.content.Context;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.spit16.R;
import com.spit16.model.Model;
import com.spit16.model.OnModelChooseListener;
import com.spit16.widget.RecyclerItemMotionListener;
import java.util.List;
/**
* @author romanishin
* @since 23.11.15.
*/
public abstract class AbsModelAdapter<T extends Model, VH extends AbsModelAdapter.AbsViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int LOADING_TYPE = 102;
private static final int EMPTY_TYPE = 103;
private static final int NORMAL_TYPE = 1;
protected final List<T> mContentList;
private boolean isLoading;
public AbsModelAdapter(List<T> mContentList) {
this.mContentList = mContentList;
setLoading(true);
}
public void setLoading(boolean loading) {
if (isLoading == loading)
return;
this.isLoading = loading;
if (loading) {
mContentList.add(null);
notifyItemInserted(mContentList.size() - 1);
} else {
mContentList.remove(mContentList.size() - 1);
notifyItemRemoved(mContentList.size());
}
}
public void setOnItemChooseListener(RecyclerView recyclerView, final OnModelChooseListener<T> listener) {
// Check for NPE
if (recyclerView == null || listener == null)
return;
Context context = recyclerView.getContext();
recyclerView.addOnItemTouchListener(
new RecyclerItemMotionListener(context,
new RecyclerItemMotionListener.SimpleItemMotionEventListener() {
@Override
public void onItemClick(View view, int position) {
if (!mContentList.isEmpty()) {
listener.onModelChoose(getItem(position));
}
}
@Override
public void onItemLongClick(View view, int position) {
}
}));
}
public void replaceItems(List<? extends T> items) {
notifyForLoading();
notifyDataSetChanged();
mContentList.clear();
mContentList.addAll(items);
}
private void notifyForLoading() {
if (isLoading)
setLoading(false);
else if (mContentList.isEmpty()) {
// mContentList.add(null);
// notifyItemInserted(0);
}
}
public void insertItems(List<T> items) {
notifyForLoading();
mContentList.addAll(items);
notifyItemInserted(mContentList.size() - 1);
}
public void insertItem(T item) {
notifyForLoading();
mContentList.add(0, item);
notifyItemInserted(0);
}
public void removeItem(T item) {
notifyForLoading();
int position = mContentList.indexOf(item);
mContentList.remove(item);
notifyItemRemoved(position);
}
public T getItem(int position) {
return mContentList.get(position);
}
@Override
public int getItemCount() {
int count = mContentList.size();
if (!isLoading && count == 0)
return 1;
return count;
}
@Override
public int getItemViewType(int position) {
if (isLoading) {
if (position == mContentList.size() - 1)
return LOADING_TYPE;
} else {
if (mContentList.isEmpty())
return EMPTY_TYPE;
}
return NORMAL_TYPE;
}
@Override
public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == LOADING_TYPE)
return new LoadingViewHolder(inflater.inflate(R.layout.view_progress, parent, false));
else if (viewType == EMPTY_TYPE)
return new LoadingViewHolder(createEmptyView(inflater));
else
return onInnerCreateViewHolder(inflater, parent, viewType);
}
protected View createEmptyView(LayoutInflater inflater) {
View view = inflater.inflate(R.layout.view_empty, null, false);
TextView emptyText = (TextView) view.findViewById(R.id.view_empty);
emptyText.setText(getTextForEmpty());
return view;
}
protected
@StringRes
int getTextForEmpty() {
return R.string.prompt_empty;
}
@Override
public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof LoadingViewHolder) {
return;
}
T model = getItem(position);
if (model != null)
onInnerBindViewHolder((VH) holder, model);
}
protected abstract void onInnerBindViewHolder(final VH holder, final T model);
protected abstract VH onInnerCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);
public static class LoadingViewHolder extends RecyclerView.ViewHolder {
public LoadingViewHolder(View itemView) {
super(itemView);
}
}
public static class AbsViewHolder extends RecyclerView.ViewHolder {
public AbsViewHolder(View itemView) {
super(itemView);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment