}
/**
-
@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
package com.lhw.jetpack.adapter;
import android.view.View;
import androidx.annotation.NonNull;
import com.lhw.common.adapter.BaseViewHolder;
import com.lhw.common.adapter.CommomBaseAdapter;
import com.lhw.jetpack.R;
import com.lhw.jetpack.model.AppInfo;
import java.util.List;
/**
-
@author LHW
-
@date 2020/11/5.
-
Description:
*/
public class AppInfoAdapter extends CommomBaseAdapter<AppInfo,BaseViewHolder> {
public AppInfoAdapter(List data, int layoutId) {
super(data,layoutId );
}
总结
这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
-
@date 2020/11/5.
-
Description:
*/
public class AppInfoAdapter extends CommomBaseAdapter<AppInfo,BaseViewHolder> {
public AppInfoAdapter(List data, int layoutId) {
super(data,layoutId );
}
总结
这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-NecXMqEb-1715591793908)]
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!