Last active
June 29, 2019 05:58
-
-
Save shishirthedev/26f8d51bfb4c6bcf7babdcb0498bf8d8 to your computer and use it in GitHub Desktop.
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 abstract class BaseAdapter<D> extends RecyclerView.Adapter<BaseViewHolder> { | |
public static final int VIEW_TYPE_ITEM = 1; | |
public static final int VIEW_TYPE_FOOTER = 2; | |
protected ArrayList<D> dataList; | |
protected Context context; | |
protected LayoutInflater layoutInflater; | |
private BaseAdapter(){} | |
public BaseAdapter(Context context){ | |
this.context = context; | |
dataList = new ArrayList<>(); | |
layoutInflater = LayoutInflater.from(context); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return dataList.get(position) == null ? VIEW_TYPE_FOOTER : VIEW_TYPE_ITEM; | |
} | |
public void add(D singleData){ | |
dataList.add(singleData); | |
notifyItemInserted(dataList.size() -1); | |
} | |
public void addAll(ArrayList<D> dataList){ | |
for(D singleData: dataList){ | |
add(singleData); | |
} | |
} | |
public D getItem(int position) { | |
return dataList.get(position); | |
} | |
public void addLoadingFooter() { | |
dataList.add(null); | |
notifyItemInserted(dataList.size() - 1); | |
} | |
public void removeLoadingFooter() { | |
dataList.remove(dataList.size() - 1); | |
notifyItemRemoved(dataList.size()); | |
} | |
public void remove(D singleData) { | |
int position = dataList.indexOf(singleData); | |
if (position > -1) { | |
dataList.remove(position); | |
notifyItemRemoved(position); | |
} | |
} | |
public void remove(int position){ | |
if(!dataList.isEmpty()){ | |
dataList.remove(position); | |
notifyItemRemoved(position); | |
} | |
} | |
public void clear() { | |
while (getItemCount() > 0) { | |
remove(getItem(0)); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return dataList.isEmpty() ? 0 : dataList.size(); | |
} | |
static class FooterViewHolder extends BaseViewHolder{ | |
FooterViewHolder(@NonNull ViewDataBinding viewDataBinding) { | |
super(viewDataBinding); | |
} | |
@Override | |
public void bind(int position) { | |
} | |
} | |
public BaseViewHolder getFooterViewHolder(@NonNull ViewGroup viewGroup){ | |
return new FooterViewHolder(DataBindingUtil.inflate(layoutInflater, | |
R.layout.looding_footer, | |
viewGroup, | |
false)); | |
} | |
} | |
////// Base View Holder.......................... | |
public abstract class BaseViewHolder extends RecyclerView.ViewHolder { | |
private final ViewDataBinding viewDataBinding; | |
protected BaseViewHolder(@NonNull ViewDataBinding viewDataBinding) { | |
super(viewDataBinding.getRoot()); | |
this.viewDataBinding = viewDataBinding; | |
} | |
public abstract void bind(int position); | |
protected ViewDataBinding getViewBinding(){ return viewDataBinding;} | |
} | |
// Usage of this Base Adapter In Adapter Class..................... | |
public class MyAdapter extends BaseAdapter<String>{ | |
public MyAdapter(Context context) { | |
super(context); | |
} | |
@NonNull | |
@Override | |
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { | |
if(viewType == BaseAdapter.VIEW_TYPE_FOOTER){ | |
return super.getFooterViewHolder(viewGroup); | |
}else{ | |
return new ItemViewHolder(DataBindingUtil.inflate(layoutInflater, | |
R.layout.item_view, | |
viewGroup, | |
false)); | |
} | |
} | |
@Override | |
public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int position) { | |
if(viewHolder instanceof ItemViewHolder){ | |
viewHolder.bind(position); | |
} | |
} | |
public class ItemViewHolder extends BaseViewHolder{ | |
ItemViewHolder(@NonNull ViewDataBinding viewDataBinding) { | |
super(viewDataBinding); | |
} | |
@Override | |
public void bind(int position) { | |
if(dataList.get(position) != null){ | |
getViewBinding().setVariable(BR.item, dataList.get(position)); | |
getViewBinding().executePendingBindings(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment