2.创建条目点击事件和子条目点击事件接口
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public interface OnItemLongClickListener {
boolean onItemLongClick(@NonNull CommomBaseAdapter adapter, @NonNull View view, int position);
}
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public interface OnItemClickListener {
void onItemClickListener(@NonNull CommomBaseAdapter<?, ?> adapter, @NonNull View view, int position);
}
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public interface OnItemChildLongClickListener {
/**
-
callback method to be invoked when an item in this view has been
-
click and held
-
@param adapter this CommomBaseAdapter adapter
-
@param view The childView whihin the itemView that was clicked and held.
-
@param position The position of the view int the adapter
-
@return true if the callback consumed the long click ,false otherwise
*/
boolean onItemChildLongClick(@NonNull CommomBaseAdapter adapter, @NonNull View view, int position);
}
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public interface OnItemChildClickListener {
/**
-
callback method to be invoked when an item in this view has been
-
click and held
-
@param adapter this CommomBaseAdapter adapter
-
@param view The childView whihin the itemView that was clicked and held.
-
@param position The position of the view int the adapter
-
@return true if the callback consumed the long click ,false otherwise
*/
boolean onItemChildLongClick(@NonNull CommomBaseAdapter adapter, @NonNull View view, int position);
}
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public interface BaseListenerImp {
void setOnItemClickListener(@Nullable OnItemClickListener listener);
void setOnItemLongClickListener(@Nullable OnItemLongClickListener listener);
void setOnItemChildClickListener(@Nullable OnItemChildClickListener listener);
void setOnItemChildLongClickListener(@Nullable OnItemChildLongClickListener listener);
}
3. 实现万能适配器抽象类 CommomBaseAdapter
package com.lhw.common.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.lhw.common.adapter.listener.OnItemChildClickListener;
import com.lhw.common.adapter.listener.OnItemChildLongClickListener;
import com.lhw.common.adapter.listener.OnItemClickListener;
import com.lhw.common.adapter.listener.OnItemLongClickListener;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public abstract class CommomBaseAdapter<T, VH extends BaseViewHolder> extends RecyclerView.Adapter implements BaseListenerImp {
private List mData;//填充数据列表
private Context mContext;
private int mLayoutId;
private RecyclerView mRecyclerView;
private OnItemChildClickListener mOnItemChildClickListener;
private OnItemClickListener mOnItemClickListener;
private OnItemChildLongClickListener mOnItemChildLongClickListener;
private OnItemLongClickListener mOnItemLongClickListener;
LinkedHashSet childClickViewIds = new LinkedHashSet<>();//用于保存需要设置点击事件的 item
LinkedHashSet childLongClickViewIds =new LinkedHashSet<>();//用于保存需要设置长按点击事件的 item
public CommomBaseAdapter(List data, int layoutId) {
this.mData = data;
this.mLayoutId = layoutId;
}
/**
-
定义数据填充抽象函数
-
@param holder
-
@param position
-
@param item
*/
protected abstract void setViewContent(@NonNull VH holder, int position, T item);
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mContext = recyclerView.getContext();
mRecyclerView = recyclerView;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//创建绑定ViewHolder
BaseViewHolder viewHolder = BaseViewHolder.createViewHolder(mContext, parent, viewType, mLayoutId);
//绑定点击事件
bindViewClickListener((VH) viewHolder, viewType);
return (VH) viewHolder;
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
//实现抽象函数的数据绑定
setViewContent(holder, position,(T) getData().get(position));
}
/**
-
根据位置回去条目对象
-
@param position
-
@return
*/
public T getItem(int position){
if (!mData.isEmpty())
return mData.get(position);
return null;
}
/**对象获取条目是否存在
-
如果返回 -1,表示不存在
-
@param item T?
-
@return Int
*/
public int getItemPosition(T item) {
if (item != null && !mData.isEmpty())
return mData.indexOf(item);
return -1;
}
public LinkedHashSet getChildClickViewIds() {
return childClickViewIds;
}
public LinkedHashSet getChildLongClickViewIds() {
return childLongClickViewIds;
}
/**
-
添加子条目点击事件
-
@param viewIds
*/
public void addChildClickViewIds(int … viewIds){
for (int viewId : viewIds) {
childClickViewIds.add(viewId);
}
}
/**
-
添加子条目长按点击事件
-
@param viewIds
*/
public void addChildLongClickViewIds(int … viewIds){
for (int viewId : viewIds) {
childLongClickViewIds.add(viewId);
}
}
/**
-
绑定 item 点击事件
-
@param holder VH
*/
protected void bindViewClickListener(final VH holder, int viewType) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setOnItemClick(holder.itemView, holder.getAdapterPosition());
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return setOnItemLongClick(holder.itemView, holder.getAdapterPosition());
}
});
for (Integer id : getChildClickViewIds()) {
holder.itemView.findViewById(id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setOnItemChildClick(v,holder.getAdapterPosition());
}
});
}
for (Integer id : getChildLongClickViewIds()) {
holder.itemView.findViewById(id).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return setOnItemChildLongClick(v,holder.getAdapterPosition());
}
});
}
}
public List getData() {
if (null == mData) {
return new ArrayList<>();
}
return mData;
}
public void setData(List mData) {
this.mData = mData;
}
@Override
public int getItemCount() {
return mData.size();
}
/**
-
override this method if you want to override click event logic
-
-
如果你想重新实现 item 点击事件逻辑,请重写此方法
-
@param v
-
@param position
*/
protected void setOnItemClick(View v, int position) {
mOnItemClickListener.onItemClickListener(this, v, position);
}
/**
-
override this method if you want to override longClick event logic
-
如果你想重新实现 item 长按事件逻辑,请重写此方法
-
@param v
-
@param position
-
@return
*/
protected boolean setOnItemLongClick(View v, int position) {
return mOnItemLongClickListener.onItemLongClick(this, v, position);
}
protected void setOnItemChildClick(View v, int position) {
mOnItemChildClickListener.onItemChildLongClick(this, v, position);
}
protected boolean setOnItemChildLongClick(View v, int position) {
return mOnItemChildLongClickListener.onItemChildLongClick(this, v, position);
}
@Override
public void setOnItemChildClickListener(@Nullable OnItemChildClickListener listener) {
mOnItemChildClickListener = listener;
}
@Override
public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
mOnItemClickListener = listener;
}
@Override
public void setOnItemChildLongClickListener(@Nullable OnItemChildLongClickListener listener) {
mOnItemChildLongClickListener = listener;
}
@Override
public void setOnItemLongClickListener(@Nullable OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
}
public OnItemChildClickListener getOnItemChildClickListener() {
return mOnItemChildClickListener;
}
public OnItemClickListener getOnItemClickListener() {
return mOnItemClickListener;
}
public OnItemChildLongClickListener getOnItemChildLongClickListener() {
return mOnItemChildLongClickListener;
}
public OnItemLongClickListener getOnItemLongClickListener() {
return mOnItemLongClickListener;
}
}
4.使用演示 AppInfoAdapter 实现万能适配器
AppInfo.java
public class AppInfo {
private String name;
private Drawable icon;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}
AppInfoAdapter.java
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
on) {
this.icon = icon;
}
}
AppInfoAdapter.java
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-PUUZml0i-1715880836220)]
[外链图片转存中…(img-dh5vjOMu-1715880836222)]
[外链图片转存中…(img-FMUNMvDA-1715880836224)]
[外链图片转存中…(img-rUkRqYky-1715880836225)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!