-
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;
Li
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
nkedHashSet 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 );
}
@Override
protected void setViewContent(@NonNull BaseViewHolder holder, int position, AppInfo item) {
holder.setText(R.id.tv_name,item.getName())
.setImageView(R.id.iv_icon,item.getIcon());
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements PagerGridLayoutManager.PageListener{
private RecyclerView rv_app_list;
private AppInfoAdapter adapter;
private LinearLayout llt_dots;
private PagerGridLayoutManager pagerGridLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=MainActivity.this;
getLifecycle().addObserver(new MyLifecycle());
rv_app_list = findViewById(R.id.rv_app_list);
indicator_view = findViewById(R.id.indicator_view);
pagerGridLayoutManager = new
PagerGridLayoutManager(3,4,PagerGridLayoutManager.HORIZONTAL);
pagerGridLayoutManager.setPageListener(this);