在这里插入代码片
```public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
private Context context;
private List<HomeshopsBean> homeshopBeanList;//首页商品集合
public HomeAdapter(Context context, List<HomeshopsBean> homeshopBeanList) {
this.context = context;
this.homeshopBeanList = homeshopBeanList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//具体的每一件商品
HomeshopsBean homeshopsBean = homeshopBeanList.get(position);
//设置数据
holder.bookname.setText(homeshopsBean.getBookname());
holder.appearance.setText(homeshopsBean.getAppearance());
holder.location.setText(homeshopsBean.getLocation());
String[] images = homeshopsBean.getImages().split(",");
holder.image.setImageURI(images[0]);
//点击条目跳转到详情页面
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送粘性事件
EventBus.getDefault().postSticky(homeshopsBean);
Intent intent = new Intent(context, DetailActivity.class);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return homeshopBeanList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.image)
SimpleDraweeView image;
@BindView(R.id.bookname)
TextView bookname;
@BindView(R.id.appearance)
TextView appearance;
@BindView(R.id.location)
TextView location;
public ViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
首页的适配器1
最新推荐文章于 2024-03-13 12:18:29 发布