区块链技术博客
www.b2bchain.cn

Android – 简单方便含基础功能的RecyclerView.Adapter求职学习资料

D0b2wT.gif

本文介绍了Android – 简单方便含基础功能的RecyclerView.Adapter求职学习资料,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

直接放代码

Adapter

/*  * Copyright 2020 Alex  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;  import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;  import org.jetbrains.annotations.NotNull;  import java.util.List; /**  * ================================================  * Description:  * <p>  * Created by Alex on 2020/10/22 0022  * <p>  * 页面内容介绍: 基类 {@link RecyclerView.Adapter}, 如果需要实现非常复杂的 {@link RecyclerView}, 请尽量使用其他优秀的三方库  * <p>  * ================================================  */ public abstract class DefaultAdapter<T> extends RecyclerView.Adapter<BaseHolder<T>> {     protected List<T> mInfos;     private OnRecyclerViewItemClickListener mOnItemClickListener = null;     private OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener = null;     protected int dataType = 0;      public DefaultAdapter(List<T> infos) {         super();         this.mInfos = infos;     }      /**      * 遍历所有 {@link BaseHolder}, 释放他们需要释放的资源      *      * @param recyclerView {@link RecyclerView}      */     public static void releaseAllHolder(RecyclerView recyclerView) {         if (recyclerView == null) {             return;         }         for (int i = recyclerView.getChildCount() - 1; i >= 0; i--) {             final View view = recyclerView.getChildAt(i);             RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);             if (viewHolder instanceof BaseHolder) {                 ((BaseHolder) viewHolder).onRelease();             }         }     }      /**      * 创建 {@link BaseHolder}      *      * @param parent   父容器      * @param viewType 布局类型      * @return {@link BaseHolder}      */     @NotNull     @Override     public BaseHolder<T> onCreateViewHolder(ViewGroup parent, final int viewType) {         View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);         BaseHolder<T> mHolder = getHolder(view, viewType);         //设置Item点击事件         mHolder.setOnItemClickListener((view1, position) -> {             if (mOnItemClickListener != null && mInfos.size() > 0) {                 //noinspection unchecked                 mOnItemClickListener.onItemClick(view1, viewType, mInfos.get(position), position);             }         });         mHolder.setOnViewLongClickListener(new BaseHolder.OnViewLongClickListener() {             @Override             public void onViewLongClick(View view, int position) {                 if (onRecyclerViewItemLongClickListener != null && mInfos.size() > 0) {                     //noinspection unchecked                     onRecyclerViewItemLongClickListener.onItemLongClick(view, viewType, mInfos.get(position), position);                 }             }         });         return mHolder;     }       /**      * 绑定数据      *      * @param holder   {@link BaseHolder}      * @param position 在 RecyclerView 中的位置      */     @Override     public void onBindViewHolder(BaseHolder<T> holder, int position) {         holder.setData(mInfos.get(position), position);     }      /**      * 返回数据总个数      *      * @return 数据总个数      */     @Override     public int getItemCount() {         return mInfos.size();     }      /**      * 返回数据集合      *      * @return 数据集合      */     public List<T> getInfos() {         return mInfos;     }      /**      * 获得 RecyclerView 中某个 position 上的 item 数据      *      * @param position 在 RecyclerView 中的位置      * @return 数据      */     public T getItem(int position) {         return mInfos == null ? null : mInfos.get(position);     }      /**      * 让子类实现用以提供 {@link BaseHolder}      *      * @param v        用于展示的 {@link View}      * @param viewType 布局类型      * @return {@link BaseHolder}      */     @NonNull     public abstract BaseHolder<T> getHolder(@NonNull View v, int viewType);      /**      * 提供用于 item 布局的 {@code layoutId}      *      * @param viewType 布局类型      * @return 布局 id      */     public abstract int getLayoutId(int viewType);      /**      * 设置 item 点击事件      *      * @param listener 点击事件监听      */     public void setOnItemClickListener(OnRecyclerViewItemClickListener<T> listener) {         this.mOnItemClickListener = listener;     }      /**      * 设置 item 长按点击事件      *      * @param listener 长按点击事件监听      */     public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener<T> listener) {         this.onRecyclerViewItemLongClickListener = listener;     }      /**      * item 点击事件      *      * @param <T>      */     public interface OnRecyclerViewItemClickListener<T> {          /**          * item 被点击          *          * @param view     被点击的 {@link View}          * @param viewType 布局类型          * @param data     数据          * @param position 在 RecyclerView 中的位置          */         void onItemClick(@NonNull View view, int viewType, @NonNull T data, int position);       }      public interface OnRecyclerViewItemLongClickListener<T> {         /**          * item 被长按点击          *          * @param view     被点击的 {@link View}          * @param viewType 布局类型          * @param data     数据          * @param position 在 RecyclerView 中的位置          */         void onItemLongClick(@NonNull View view, int viewType, @NonNull T data, int position);     }      public void setData(List<T> list) {         if (list != null) {             mInfos.clear();             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void setData(List<T> list, int dataType) {         if (list != null) {             this.dataType = dataType;             mInfos.clear();             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void addData(List<T> list) {         if (list != null) {             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void addData(List<T> list, int dataType) {         if (list != null) {             this.dataType = dataType;             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }  }

ViewHolder

/*  * Copyright 2020 Alex  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */  import android.content.Context; import android.view.View;  import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;  /**  * ================================================  * 基类 {@link RecyclerView.ViewHolder}  * <p>  * ================================================  */ public abstract class BaseHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {     protected final String TAG = this.getClass().getSimpleName();     protected OnViewClickListener mOnViewClickListener = null;     protected OnViewLongClickListener onViewLongClickListener = null;     protected Context mContext;      public BaseHolder(View itemView) {         super(itemView);         //点击事件         itemView.setOnClickListener(this);         itemView.setOnLongClickListener(this);         mContext = itemView.getContext();     }      /**      * 设置数据      *      * @param data     数据      * @param position 在 RecyclerView 中的位置      */     public abstract void setData(@NonNull T data, int position);      /**      * 在 Activity 的 onDestroy 中使用 {@link DefaultAdapter#releaseAllHolder(RecyclerView)} 方法 (super.onDestroy() 之前)      * {@link BaseHolder#onRelease()} 才会被调用, 可以在此方法中释放一些资源      */     protected void onRelease() {      }      @Override     public void onClick(View view) {         if (mOnViewClickListener != null) {             mOnViewClickListener.onViewClick(view, this.getLayoutPosition());         }     }      /**      * 设置item的点击事件      *      * @param listener 点击事件监听      */     public void setOnItemClickListener(OnViewClickListener listener) {         this.mOnViewClickListener = listener;     }      /**      * 设置item的长按点击事件      *      * @param listener 长按点击事件监听      */     public void setOnViewLongClickListener(OnViewLongClickListener listener) {         this.onViewLongClickListener = listener;     }      @Override     public boolean onLongClick(View view) {         if (onViewLongClickListener != null) {             onViewLongClickListener.onViewLongClick(view, this.getLayoutPosition());         }         //return true, 不再触发onClick事件         return true;     }      /**      * item 点击事件      */     public interface OnViewClickListener {          /**          * item 被点击          *          * @param view     被点击的 {@link View}          * @param position 在 RecyclerView 中的位置          */         void onViewClick(View view, int position);      }      public interface OnViewLongClickListener {         /**          * item 长按          *          * @param view     被点击的 {@link View}          * @param position 在 RecyclerView 中的位置          */         void onViewLongClick(View view, int position);     }  }

注意:

如果@NonNull爆红,请引入implementation 'org.jetbrains:annotations:16.0.1'

使用方式

新建一个Adapter类 继承DefaultAdapter并标明泛型,这里泛型是单个data的泛型,勿使用List<T>当做泛型填入
新建一个ViewHolder类,继承BaseHolder,泛型和adapter处理方式相同
adapter方法简单调用:

recyclerView.setAdapter(adapter); adapter.setData(dataList); adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<DBData>() {             @Override             public void onItemClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {                 switch (view.getId()) {                     case R.id.into_go:                         //跳转                         openApp(data.getString("type"));                         break;                     case R.id.into_content:                         //获取剪贴板管理器:                         ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);                         ClipData mClipData = ClipData.newPlainText("Label", data.getString("kouling"));                         cm.setPrimaryClip(mClipData);                         toast("复制口令成功");                         //复制                         break;                 }             }         });         adapter.setOnItemLongClickListener(new DefaultAdapter.OnRecyclerViewItemLongClickListener<DBData>() {             @Override             public void onItemLongClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {                 MessageDialog.show(me, "提示", "是否删除当前口令")                         .setOnOkButtonClickListener(new OnDialogButtonClickListener() {                             @Override                             public boolean onClick(BaseDialog baseDialog, View v) {                                 deleteData(data, position);                                 return false;                             }                         });             }         });

Demo

今天刚写的一个APP,其中使用了这个功能,:
口令宝链接

Android - 简单方便含基础功能的RecyclerView.Adapter

直接放代码

Adapter

/*  * Copyright 2020 Alex  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;  import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;  import org.jetbrains.annotations.NotNull;  import java.util.List; /**  * ================================================  * Description:  * <p>  * Created by Alex on 2020/10/22 0022  * <p>  * 页面内容介绍: 基类 {@link RecyclerView.Adapter}, 如果需要实现非常复杂的 {@link RecyclerView}, 请尽量使用其他优秀的三方库  * <p>  * ================================================  */ public abstract class DefaultAdapter<T> extends RecyclerView.Adapter<BaseHolder<T>> {     protected List<T> mInfos;     private OnRecyclerViewItemClickListener mOnItemClickListener = null;     private OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener = null;     protected int dataType = 0;      public DefaultAdapter(List<T> infos) {         super();         this.mInfos = infos;     }      /**      * 遍历所有 {@link BaseHolder}, 释放他们需要释放的资源      *      * @param recyclerView {@link RecyclerView}      */     public static void releaseAllHolder(RecyclerView recyclerView) {         if (recyclerView == null) {             return;         }         for (int i = recyclerView.getChildCount() - 1; i >= 0; i--) {             final View view = recyclerView.getChildAt(i);             RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);             if (viewHolder instanceof BaseHolder) {                 ((BaseHolder) viewHolder).onRelease();             }         }     }      /**      * 创建 {@link BaseHolder}      *      * @param parent   父容器      * @param viewType 布局类型      * @return {@link BaseHolder}      */     @NotNull     @Override     public BaseHolder<T> onCreateViewHolder(ViewGroup parent, final int viewType) {         View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);         BaseHolder<T> mHolder = getHolder(view, viewType);         //设置Item点击事件         mHolder.setOnItemClickListener((view1, position) -> {             if (mOnItemClickListener != null && mInfos.size() > 0) {                 //noinspection unchecked                 mOnItemClickListener.onItemClick(view1, viewType, mInfos.get(position), position);             }         });         mHolder.setOnViewLongClickListener(new BaseHolder.OnViewLongClickListener() {             @Override             public void onViewLongClick(View view, int position) {                 if (onRecyclerViewItemLongClickListener != null && mInfos.size() > 0) {                     //noinspection unchecked                     onRecyclerViewItemLongClickListener.onItemLongClick(view, viewType, mInfos.get(position), position);                 }             }         });         return mHolder;     }       /**      * 绑定数据      *      * @param holder   {@link BaseHolder}      * @param position 在 RecyclerView 中的位置      */     @Override     public void onBindViewHolder(BaseHolder<T> holder, int position) {         holder.setData(mInfos.get(position), position);     }      /**      * 返回数据总个数      *      * @return 数据总个数      */     @Override     public int getItemCount() {         return mInfos.size();     }      /**      * 返回数据集合      *      * @return 数据集合      */     public List<T> getInfos() {         return mInfos;     }      /**      * 获得 RecyclerView 中某个 position 上的 item 数据      *      * @param position 在 RecyclerView 中的位置      * @return 数据      */     public T getItem(int position) {         return mInfos == null ? null : mInfos.get(position);     }      /**      * 让子类实现用以提供 {@link BaseHolder}      *      * @param v        用于展示的 {@link View}      * @param viewType 布局类型      * @return {@link BaseHolder}      */     @NonNull     public abstract BaseHolder<T> getHolder(@NonNull View v, int viewType);      /**      * 提供用于 item 布局的 {@code layoutId}      *      * @param viewType 布局类型      * @return 布局 id      */     public abstract int getLayoutId(int viewType);      /**      * 设置 item 点击事件      *      * @param listener 点击事件监听      */     public void setOnItemClickListener(OnRecyclerViewItemClickListener<T> listener) {         this.mOnItemClickListener = listener;     }      /**      * 设置 item 长按点击事件      *      * @param listener 长按点击事件监听      */     public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener<T> listener) {         this.onRecyclerViewItemLongClickListener = listener;     }      /**      * item 点击事件      *      * @param <T>      */     public interface OnRecyclerViewItemClickListener<T> {          /**          * item 被点击          *          * @param view     被点击的 {@link View}          * @param viewType 布局类型          * @param data     数据          * @param position 在 RecyclerView 中的位置          */         void onItemClick(@NonNull View view, int viewType, @NonNull T data, int position);       }      public interface OnRecyclerViewItemLongClickListener<T> {         /**          * item 被长按点击          *          * @param view     被点击的 {@link View}          * @param viewType 布局类型          * @param data     数据          * @param position 在 RecyclerView 中的位置          */         void onItemLongClick(@NonNull View view, int viewType, @NonNull T data, int position);     }      public void setData(List<T> list) {         if (list != null) {             mInfos.clear();             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void setData(List<T> list, int dataType) {         if (list != null) {             this.dataType = dataType;             mInfos.clear();             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void addData(List<T> list) {         if (list != null) {             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void addData(List<T> list, int dataType) {         if (list != null) {             this.dataType = dataType;             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }  }

ViewHolder

/*  * Copyright 2020 Alex  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */  import android.content.Context; import android.view.View;  import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;  /**  * ================================================  * 基类 {@link RecyclerView.ViewHolder}  * <p>  * ================================================  */ public abstract class BaseHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {     protected final String TAG = this.getClass().getSimpleName();     protected OnViewClickListener mOnViewClickListener = null;     protected OnViewLongClickListener onViewLongClickListener = null;     protected Context mContext;      public BaseHolder(View itemView) {         super(itemView);         //点击事件         itemView.setOnClickListener(this);         itemView.setOnLongClickListener(this);         mContext = itemView.getContext();     }      /**      * 设置数据      *      * @param data     数据      * @param position 在 RecyclerView 中的位置      */     public abstract void setData(@NonNull T data, int position);      /**      * 在 Activity 的 onDestroy 中使用 {@link DefaultAdapter#releaseAllHolder(RecyclerView)} 方法 (super.onDestroy() 之前)      * {@link BaseHolder#onRelease()} 才会被调用, 可以在此方法中释放一些资源      */     protected void onRelease() {      }      @Override     public void onClick(View view) {         if (mOnViewClickListener != null) {             mOnViewClickListener.onViewClick(view, this.getLayoutPosition());         }     }      /**      * 设置item的点击事件      *      * @param listener 点击事件监听      */     public void setOnItemClickListener(OnViewClickListener listener) {         this.mOnViewClickListener = listener;     }      /**      * 设置item的长按点击事件      *      * @param listener 长按点击事件监听      */     public void setOnViewLongClickListener(OnViewLongClickListener listener) {         this.onViewLongClickListener = listener;     }      @Override     public boolean onLongClick(View view) {         if (onViewLongClickListener != null) {             onViewLongClickListener.onViewLongClick(view, this.getLayoutPosition());         }         //return true, 不再触发onClick事件         return true;     }      /**      * item 点击事件      */     public interface OnViewClickListener {          /**          * item 被点击          *          * @param view     被点击的 {@link View}          * @param position 在 RecyclerView 中的位置          */         void onViewClick(View view, int position);      }      public interface OnViewLongClickListener {         /**          * item 长按          *          * @param view     被点击的 {@link View}          * @param position 在 RecyclerView 中的位置          */         void onViewLongClick(View view, int position);     }  }

注意:

如果@NonNull爆红,请引入implementation 'org.jetbrains:annotations:16.0.1'

使用方式

新建一个Adapter类 继承DefaultAdapter并标明泛型,这里泛型是单个data的泛型,勿使用List<T>当做泛型填入
新建一个ViewHolder类,继承BaseHolder,泛型和adapter处理方式相同
adapter方法简单调用:

recyclerView.setAdapter(adapter); adapter.setData(dataList); adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<DBData>() {             @Override             public void onItemClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {                 switch (view.getId()) {                     case R.id.into_go:                         //跳转                         openApp(data.getString("type"));                         break;                     case R.id.into_content:                         //获取剪贴板管理器:                         ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);                         ClipData mClipData = ClipData.newPlainText("Label", data.getString("kouling"));                         cm.setPrimaryClip(mClipData);                         toast("复制口令成功");                         //复制                         break;                 }             }         });         adapter.setOnItemLongClickListener(new DefaultAdapter.OnRecyclerViewItemLongClickListener<DBData>() {             @Override             public void onItemLongClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {                 MessageDialog.show(me, "提示", "是否删除当前口令")                         .setOnOkButtonClickListener(new OnDialogButtonClickListener() {                             @Override                             public boolean onClick(BaseDialog baseDialog, View v) {                                 deleteData(data, position);                                 return false;                             }                         });             }         });

Demo

今天刚写的一个APP,其中使用了这个功能,:
口令宝链接

Android - 简单方便含基础功能的RecyclerView.Adapter

直接放代码

Adapter

/*  * Copyright 2020 Alex  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;  import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;  import org.jetbrains.annotations.NotNull;  import java.util.List; /**  * ================================================  * Description:  * <p>  * Created by Alex on 2020/10/22 0022  * <p>  * 页面内容介绍: 基类 {@link RecyclerView.Adapter}, 如果需要实现非常复杂的 {@link RecyclerView}, 请尽量使用其他优秀的三方库  * <p>  * ================================================  */ public abstract class DefaultAdapter<T> extends RecyclerView.Adapter<BaseHolder<T>> {     protected List<T> mInfos;     private OnRecyclerViewItemClickListener mOnItemClickListener = null;     private OnRecyclerViewItemLongClickListener onRecyclerViewItemLongClickListener = null;     protected int dataType = 0;      public DefaultAdapter(List<T> infos) {         super();         this.mInfos = infos;     }      /**      * 遍历所有 {@link BaseHolder}, 释放他们需要释放的资源      *      * @param recyclerView {@link RecyclerView}      */     public static void releaseAllHolder(RecyclerView recyclerView) {         if (recyclerView == null) {             return;         }         for (int i = recyclerView.getChildCount() - 1; i >= 0; i--) {             final View view = recyclerView.getChildAt(i);             RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);             if (viewHolder instanceof BaseHolder) {                 ((BaseHolder) viewHolder).onRelease();             }         }     }      /**      * 创建 {@link BaseHolder}      *      * @param parent   父容器      * @param viewType 布局类型      * @return {@link BaseHolder}      */     @NotNull     @Override     public BaseHolder<T> onCreateViewHolder(ViewGroup parent, final int viewType) {         View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);         BaseHolder<T> mHolder = getHolder(view, viewType);         //设置Item点击事件         mHolder.setOnItemClickListener((view1, position) -> {             if (mOnItemClickListener != null && mInfos.size() > 0) {                 //noinspection unchecked                 mOnItemClickListener.onItemClick(view1, viewType, mInfos.get(position), position);             }         });         mHolder.setOnViewLongClickListener(new BaseHolder.OnViewLongClickListener() {             @Override             public void onViewLongClick(View view, int position) {                 if (onRecyclerViewItemLongClickListener != null && mInfos.size() > 0) {                     //noinspection unchecked                     onRecyclerViewItemLongClickListener.onItemLongClick(view, viewType, mInfos.get(position), position);                 }             }         });         return mHolder;     }       /**      * 绑定数据      *      * @param holder   {@link BaseHolder}      * @param position 在 RecyclerView 中的位置      */     @Override     public void onBindViewHolder(BaseHolder<T> holder, int position) {         holder.setData(mInfos.get(position), position);     }      /**      * 返回数据总个数      *      * @return 数据总个数      */     @Override     public int getItemCount() {         return mInfos.size();     }      /**      * 返回数据集合      *      * @return 数据集合      */     public List<T> getInfos() {         return mInfos;     }      /**      * 获得 RecyclerView 中某个 position 上的 item 数据      *      * @param position 在 RecyclerView 中的位置      * @return 数据      */     public T getItem(int position) {         return mInfos == null ? null : mInfos.get(position);     }      /**      * 让子类实现用以提供 {@link BaseHolder}      *      * @param v        用于展示的 {@link View}      * @param viewType 布局类型      * @return {@link BaseHolder}      */     @NonNull     public abstract BaseHolder<T> getHolder(@NonNull View v, int viewType);      /**      * 提供用于 item 布局的 {@code layoutId}      *      * @param viewType 布局类型      * @return 布局 id      */     public abstract int getLayoutId(int viewType);      /**      * 设置 item 点击事件      *      * @param listener 点击事件监听      */     public void setOnItemClickListener(OnRecyclerViewItemClickListener<T> listener) {         this.mOnItemClickListener = listener;     }      /**      * 设置 item 长按点击事件      *      * @param listener 长按点击事件监听      */     public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener<T> listener) {         this.onRecyclerViewItemLongClickListener = listener;     }      /**      * item 点击事件      *      * @param <T>      */     public interface OnRecyclerViewItemClickListener<T> {          /**          * item 被点击          *          * @param view     被点击的 {@link View}          * @param viewType 布局类型          * @param data     数据          * @param position 在 RecyclerView 中的位置          */         void onItemClick(@NonNull View view, int viewType, @NonNull T data, int position);       }      public interface OnRecyclerViewItemLongClickListener<T> {         /**          * item 被长按点击          *          * @param view     被点击的 {@link View}          * @param viewType 布局类型          * @param data     数据          * @param position 在 RecyclerView 中的位置          */         void onItemLongClick(@NonNull View view, int viewType, @NonNull T data, int position);     }      public void setData(List<T> list) {         if (list != null) {             mInfos.clear();             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void setData(List<T> list, int dataType) {         if (list != null) {             this.dataType = dataType;             mInfos.clear();             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void addData(List<T> list) {         if (list != null) {             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }      public void addData(List<T> list, int dataType) {         if (list != null) {             this.dataType = dataType;             mInfos.addAll(list);             this.notifyDataSetChanged();         }     }  }

ViewHolder

/*  * Copyright 2020 Alex  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *      http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */  import android.content.Context; import android.view.View;  import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;  /**  * ================================================  * 基类 {@link RecyclerView.ViewHolder}  * <p>  * ================================================  */ public abstract class BaseHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {     protected final String TAG = this.getClass().getSimpleName();     protected OnViewClickListener mOnViewClickListener = null;     protected OnViewLongClickListener onViewLongClickListener = null;     protected Context mContext;      public BaseHolder(View itemView) {         super(itemView);         //点击事件         itemView.setOnClickListener(this);         itemView.setOnLongClickListener(this);         mContext = itemView.getContext();     }      /**      * 设置数据      *      * @param data     数据      * @param position 在 RecyclerView 中的位置      */     public abstract void setData(@NonNull T data, int position);      /**      * 在 Activity 的 onDestroy 中使用 {@link DefaultAdapter#releaseAllHolder(RecyclerView)} 方法 (super.onDestroy() 之前)      * {@link BaseHolder#onRelease()} 才会被调用, 可以在此方法中释放一些资源      */     protected void onRelease() {      }      @Override     public void onClick(View view) {         if (mOnViewClickListener != null) {             mOnViewClickListener.onViewClick(view, this.getLayoutPosition());         }     }      /**      * 设置item的点击事件      *      * @param listener 点击事件监听      */     public void setOnItemClickListener(OnViewClickListener listener) {         this.mOnViewClickListener = listener;     }      /**      * 设置item的长按点击事件      *      * @param listener 长按点击事件监听      */     public void setOnViewLongClickListener(OnViewLongClickListener listener) {         this.onViewLongClickListener = listener;     }      @Override     public boolean onLongClick(View view) {         if (onViewLongClickListener != null) {             onViewLongClickListener.onViewLongClick(view, this.getLayoutPosition());         }         //return true, 不再触发onClick事件         return true;     }      /**      * item 点击事件      */     public interface OnViewClickListener {          /**          * item 被点击          *          * @param view     被点击的 {@link View}          * @param position 在 RecyclerView 中的位置          */         void onViewClick(View view, int position);      }      public interface OnViewLongClickListener {         /**          * item 长按          *          * @param view     被点击的 {@link View}          * @param position 在 RecyclerView 中的位置          */         void onViewLongClick(View view, int position);     }  }

注意:

如果@NonNull爆红,请引入implementation 'org.jetbrains:annotations:16.0.1'

使用方式

新建一个Adapter类 继承DefaultAdapter并标明泛型,这里泛型是单个data的泛型,勿使用List<T>当做泛型填入
新建一个ViewHolder类,继承BaseHolder,泛型和adapter处理方式相同
adapter方法简单调用:

recyclerView.setAdapter(adapter); adapter.setData(dataList); adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener<DBData>() {             @Override             public void onItemClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {                 switch (view.getId()) {                     case R.id.into_go:                         //跳转                         openApp(data.getString("type"));                         break;                     case R.id.into_content:                         //获取剪贴板管理器:                         ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);                         ClipData mClipData = ClipData.newPlainText("Label", data.getString("kouling"));                         cm.setPrimaryClip(mClipData);                         toast("复制口令成功");                         //复制                         break;                 }             }         });         adapter.setOnItemLongClickListener(new DefaultAdapter.OnRecyclerViewItemLongClickListener<DBData>() {             @Override             public void onItemLongClick(@NonNull View view, int viewType, @NonNull DBData data, int position) {                 MessageDialog.show(me, "提示", "是否删除当前口令")                         .setOnOkButtonClickListener(new OnDialogButtonClickListener() {                             @Override                             public boolean onClick(BaseDialog baseDialog, View v) {                                 deleteData(data, position);                                 return false;                             }                         });             }         });

Demo

今天刚写的一个APP,其中使用了这个功能,:
口令宝链接

Android - 简单方便含基础功能的RecyclerView.Adapter

部分转自互联网,侵权删除联系

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » Android – 简单方便含基础功能的RecyclerView.Adapter求职学习资料
分享到: 更多 (0)
D0b2wT.gif

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们