Android RecyclerView适配器简单抽取(封装)
程序员文章站
2024-03-17 09:31:52
...
这里我们主要对RecyclerView进行抽取, 以此达到减少代码量
话不多说 直接上代码 照常使用即可 new适配器时不需要传上下文
package com.example.text_1.utils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public abstract class BaseAdapter<T> extends RecyclerView.Adapter {
private List<T> list;
private Context context;
public BaseAdapter(List<T> list) {
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
return new ViewHolder(LayoutInflater.from(context).inflate(setLayout(), parent, false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder holder1 = (ViewHolder) holder;
onBindView(list, list.get(position), position, holder1, context, holder1.itemView);
if (onItemClick != null) {
holder1.itemView.setOnClickListener(v -> {
onItemClick.OnClick();
onItemClick.OnClick(position);
});
holder1.itemView.setOnLongClickListener(v -> {
onItemClick.OnLongClick();
onItemClick.OnLongClick(position);
return true;
});
}
}
public abstract void onBindView(List<T> list, T data, int pos, RecyclerView.ViewHolder holder, Context context, View view);
@Override
public int getItemCount() {
return list.size();
}
public abstract int setLayout();
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
public void showToast(Object o) {
Toast.makeText(context, o.toString(), Toast.LENGTH_SHORT).show();
}
public OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
default void OnClick() {
}
void OnClick(int pos);
default void OnLongClick() {
}
default void OnLongClick(int pos) {
}
}
}
具体用法:
package com.example.text_1.adapter;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.text_1.R;
import com.example.text_1.bean.ProjectBean;
import com.example.text_1.utils.BaseAdapter;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ProjectAdapter extends BaseAdapter<ProjectBean.FreeBean.CourseBean> {
@BindView(R.id.tv_teacher_name)
TextView tvTeacherName;
@BindView(R.id.tv_title)
TextView tvTitle;
public ProjectAdapter(List<ProjectBean.FreeBean.CourseBean> list) {
super(list);
}
@Override
public void onBindView(List<ProjectBean.FreeBean.CourseBean> list, ProjectBean.FreeBean.CourseBean data, int pos, RecyclerView.ViewHolder holder, Context context, View view) {
ButterKnife.bind(this,view);
tvTitle.setText("题目:" + data.title);
tvTeacherName.setText("讲师:" + data.teacherName);
}
@Override
public int setLayout() {
return R.layout.item_project_layout;
}
}
点击事件监听:
adapter.setOnItemClick(pos -> {
//此处处理逻辑即可
Toast.makeText(getContext(), list.get(pos).title, Toast.LENGTH_SHORT).show();
});
上一篇: 使用API28报错问题及解决
下一篇: Android6.0下的权限请求问题