public class Showadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int one = 0;
public static final int tow = 1;
//集合和上下文
private List<ShowBean.ResultBean.MlssBean.CommodityListBeanXX> commodityList;
private Context context;
//有参
public Showadapter(List<ShowBean.ResultBean.MlssBean.CommodityListBeanXX> commodityList, Context context) {
this.commodityList = commodityList;
this.context = context;
}
@NonNull
@Override //找布局 判断条目
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
if (i==one){
view=LayoutInflater.from(context).inflate(R.layout.z1,null,false);
myviewholder myviewholder = new myviewholder(view);
return myviewholder;
}else
{
view=LayoutInflater.from(context).inflate(R.layout.zz1,null,false);
myviewholder2 myviewholder2 = new myviewholder2(view);
return myviewholder2;
}
}
@Override //多条目及点击事件
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
if (holder instanceof myviewholder){
((myviewholder) holder).kt1.setText(commodityList.get(i).getCommodityName());
ImageLoader.getInstance().displayImage(commodityList.get(i).getMasterPic(),((myviewholder) holder).kimge1);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onclickLogin.getclike(i);
}
});
}else if (holder instanceof myviewholder2){
((myviewholder2) holder).kt2.setText(commodityList.get(i).getCommodityId()+"");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onclickLogin.getclike(i);
}
});
}
}
@Override
public int getItemViewType(int position) {
if (position%2==0){
return one;
}
return tow;
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class myviewholder extends RecyclerView.ViewHolder{
private final ImageView kimge1;
private final TextView kt1;
public myviewholder(@NonNull View itemView) {
super(itemView);
kimge1 = itemView.findViewById(R.id.image1);
kt1 = itemView.findViewById(R.id.text1);
}
}
public class myviewholder2 extends RecyclerView.ViewHolder{
private final TextView kt2;
public myviewholder2(@NonNull View itemView) {
super(itemView);
kt2 = itemView.findViewById(R.id.text2);
}
}
//接口回调
public interface OnclickLogin{
void getclike(int i);
void getlongclike(int i);
}
private OnclickLogin onclickLogin;
public void setOnclickLogin(OnclickLogin onclickLogin){
this.onclickLogin=onclickLogin;
}
}
获取点击activity 的方法 与接口回调相同 适配器的名字找set中的方法
直接在返回的方法中写需求
普通的RecyclerView使用方法
public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.viewholder> {
Context context;
List<Bean3.ResultBean> result;
public ShowAdapter(Context context, List<Bean3.ResultBean> result) {
this.context = context;
this.result = result;
}
/
@NonNull
@Override
public viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.z1,null,false);
viewholder viewholder = new viewholder(view);
return viewholder;
}
/
@Override
public void onBindViewHolder(@NonNull viewholder itemView, int i) {
ImageLoader.getInstance().displayImage(result.get(i).getImageUrl(),itemView.kimage);
itemView.kt1.setText(result.get(i).getTitle());
}
/
@Override
public int getItemCount() {
return result.size();
}
public class viewholder extends RecyclerView.ViewHolder{
private final ImageView kimage;
private final TextView kt1;
public viewholder(@NonNull View itemView) {
super(itemView);
kimage = itemView.findViewById(R.id.image);
kt1 = itemView.findViewById(R.id.text1);
}
}