|
@@ -0,0 +1,184 @@
|
|
|
+package com.sunwin.visitorapp.adapter;
|
|
|
+
|
|
|
+
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * recyclerview适配器
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * recyclerview适配器
|
|
|
+ */
|
|
|
+
|
|
|
+public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
|
|
|
+
|
|
|
+// protected Context context;//上下文
|
|
|
+ protected List<T> mList = new ArrayList<>();//数据源
|
|
|
+
|
|
|
+
|
|
|
+ public interface ItemClickListener<T> {
|
|
|
+ void click(int position, T item, BaseViewHolder holder);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ItemClickListener<T> itemClickListener;
|
|
|
+ public void setItemClick(ItemClickListener<T> itemClickListener) {
|
|
|
+ this.itemClickListener = itemClickListener;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ItemClickListener<T> itemLongClickListener;
|
|
|
+ public void setItemLongClick(ItemClickListener<T> itemLongClickListener) {
|
|
|
+ this.itemLongClickListener = itemLongClickListener;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public BaseRecyclerAdapter(){
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemCount() {
|
|
|
+ return mList == null ? 0 : mList.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
+
|
|
|
+// View view = LayoutInflater.from(context).inflate(getLayoutId(viewType), parent, false);
|
|
|
+ return BaseViewHolder.getRecyclerHolder(getLayoutView(parent,viewType));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBindViewHolder(final BaseViewHolder holder, int position) {
|
|
|
+ if (itemClickListener != null && holder != null) {
|
|
|
+ holder.itemView.setOnClickListener(view -> {
|
|
|
+ itemClickListener.click(position, mList.get(position), holder);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (itemLongClickListener != null && holder != null) {
|
|
|
+ holder.itemView.setOnLongClickListener(view -> {
|
|
|
+ itemLongClickListener.click(position, mList.get(position), holder);
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ bind(position, mList.get(position), holder, getItemViewType(position));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected abstract View getLayoutView(ViewGroup parent, int viewType);
|
|
|
+
|
|
|
+ public abstract void bind(int position, T data, BaseViewHolder holder, int viewType);
|
|
|
+
|
|
|
+ public void setArray(T[] list) {
|
|
|
+ if (list != null) {
|
|
|
+ mList = Arrays.asList(list);
|
|
|
+ } else {
|
|
|
+ mList = new ArrayList<>();
|
|
|
+ }
|
|
|
+ notifyDataSetChanged();
|
|
|
+ }
|
|
|
+ //数据
|
|
|
+ public void setList(List<T> list) {
|
|
|
+ if (list != null) {
|
|
|
+ mList = list;
|
|
|
+ } else {
|
|
|
+ mList = new ArrayList<>();
|
|
|
+ }
|
|
|
+ notifyDataSetChanged();
|
|
|
+ }
|
|
|
+ public void setItem(int pos,T item){
|
|
|
+ mList.set(pos,item);
|
|
|
+ notifyItemChanged(pos);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void add(T item){
|
|
|
+ int position = mList.size();
|
|
|
+ mList.add(item);
|
|
|
+ notifyItemInserted(position);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //增加数据
|
|
|
+ public void addList(List<T> list) {
|
|
|
+ int position = mList.size();
|
|
|
+ if (list != null) {
|
|
|
+ mList.addAll(list);
|
|
|
+ notifyItemRangeInserted(position,list.size());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //清除数据
|
|
|
+ public void clear() {
|
|
|
+// if (mList == null) {
|
|
|
+ mList = new ArrayList<>();
|
|
|
+// }
|
|
|
+// mList.removeAll(mList);
|
|
|
+
|
|
|
+ notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ public T get(int index){
|
|
|
+ if (mList == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return mList.get(index);
|
|
|
+ }
|
|
|
+ //得到数据
|
|
|
+ public List<T> getList() {
|
|
|
+ if (mList == null) {
|
|
|
+ mList = new ArrayList<>();
|
|
|
+ }
|
|
|
+ return mList;
|
|
|
+ }
|
|
|
+
|
|
|
+ //插入
|
|
|
+ public void insert(T item, int position) {
|
|
|
+ if (mList == null) {
|
|
|
+ mList = new ArrayList<T>();
|
|
|
+ }
|
|
|
+ mList.add(position, item);
|
|
|
+ notifyItemInserted(position);
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除
|
|
|
+ public void remove(int position) {
|
|
|
+ if (mList == null) {
|
|
|
+ mList = new ArrayList<T>();
|
|
|
+ }
|
|
|
+ mList.remove(position);
|
|
|
+ notifyItemRemoved(position);
|
|
|
+ notifyItemRangeChanged(position, mList.size());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public boolean contains(AdapterInterface<T> adapterInterface,T item){
|
|
|
+ if(mList==null)return false;
|
|
|
+ boolean exist = false;
|
|
|
+ Iterator<T> iterator = mList.iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ T citem = iterator.next();
|
|
|
+ exist = adapterInterface.contrast(citem,item);
|
|
|
+ }
|
|
|
+ return exist;
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface AdapterInterface<T>{
|
|
|
+ boolean contrast(T a, T b);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|