Created
July 24, 2017 03:44
-
-
Save iChintanSoni/4e94574b7e4c6b4a8d156c6662525f85 to your computer and use it in GitHub Desktop.
BaseRecyclerAdapter, a generic Recycler adapter, is an extended version from https://gist.github.com/chintansoni202/3c61aea787ae4bd49f26adee9dd40a08 This version should be used along with Data-binding. You can even pass empty view layout, if there is no list item.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseRecyclerAdapter<T, MVH extends BaseViewHolder<T>, EVH extends BaseViewHolder<T>> extends RecyclerView.Adapter<BaseViewHolder<T>> { | |
private static final int VIEW_TYPE_EMPTY = 0; | |
private static final int VIEW_TYPE_DATA = 1; | |
private List<T> list = new ArrayList<>(); | |
@LayoutRes | |
private int emptyViewLayoutResource; | |
private Class<EVH> emptyViewHolder; | |
@LayoutRes | |
private int dataLayoutResource; | |
private Class<MVH> dataViewHolder; | |
public BaseRecyclerAdapter(int emptyViewLayoutResource, Class<EVH> emptyViewHolder, int dataLayoutResource, Class<MVH> dataViewHolder) { | |
this.emptyViewLayoutResource = emptyViewLayoutResource; | |
this.emptyViewHolder = emptyViewHolder; | |
this.dataLayoutResource = dataLayoutResource; | |
this.dataViewHolder = dataViewHolder; | |
} | |
@Override | |
public BaseViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) { | |
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); | |
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType == VIEW_TYPE_EMPTY ? emptyViewLayoutResource : dataLayoutResource, parent, false); | |
try { | |
return viewType == VIEW_TYPE_EMPTY ? emptyViewHolder.getConstructor(ViewDataBinding.class).newInstance(binding) : dataViewHolder.getConstructor(View.class).newInstance(binding); | |
} catch (NoSuchMethodException e) { | |
throw new RuntimeException(e); | |
} catch (InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} catch (InstantiationException e) { | |
throw new RuntimeException(e); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public void onBindViewHolder(BaseViewHolder<T> holder, int position) { | |
int viewType = getItemViewType(position); | |
if (viewType == VIEW_TYPE_EMPTY) { | |
} else { | |
holder.bind(list.get(position)); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return list.size() == 0 ? 1 : list.size(); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return list.size() == 0 ? VIEW_TYPE_EMPTY : VIEW_TYPE_DATA; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseViewHolder extends RecyclerView.ViewHolder { | |
private final ViewDataBinding binding; | |
public BaseViewHolder(ViewDataBinding binding) { | |
super(binding.getRoot()); | |
this.binding = binding; | |
} | |
public void bind(Object object) { | |
binding.setVariable(BR.obj, object); | |
binding.executePendingBindings(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity{ | |
// Example Usage of BaseRecyclerAdapter | |
private BaseRecyclerAdapter<Model, EmptyViewHolder, ModelViewHolder> baseRecyclerAdapter | |
= new BaseRecyclerAdapter<>(R.layout.layout_empty, EmptyViewHolder.class, R.layout.list_item, ModelViewHolder.class); | |
public class EmptyViewHolder extends BaseViewHolder { | |
public EmptyViewHolder(ViewDataBinding binding) { | |
super(binding.getRoot()); | |
} | |
} | |
public class ModelViewHolder extends BaseViewHolder { | |
public ModelViewHolder(ViewDataBinding binding){ | |
super(binding.getRoot()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment