Adapter中
public class SearchGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
List<ShopBean.DatasBean.GoodsListBean> goodslist;
boolean flag;
OnItemClickListener listener;
public interface OnItemClickListener{
public void OnItemClick(View view,int position,String title,String pc,String price);
}
public void setOnItemClick(OnItemClickListener listener){
this.listener=listener;
}
public SearchGoodsAdapter(Context context, List<ShopBean.DatasBean.GoodsListBean> goodslist, boolean flag) {
this.context = context;
this.goodslist = goodslist;
this.flag = flag;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if(flag){
view=View.inflate(context,R.layout.goodslist_recycler_item,null);
return new GoodsListViewHolder(view);
}else{
view=View.inflate(context,R.layout.goodsgrid_recycler_item,null);
return new GoodsGridViewHolder(view);
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if(flag){
((GoodsListViewHolder)holder).tv_shop_title.setText(goodslist.get(position).getGoods_name());
((GoodsListViewHolder)holder).tv_shop_text.setText(goodslist.get(position).getStore_name());
((GoodsListViewHolder)holder).tv_shop_price.setText(goodslist.get(position).getGoods_price());
new BitmapUtil().getpic(goodslist.get(position).getGoods_image_url(),((GoodsListViewHolder) holder).iv_shop_biao);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener!=null){
listener.OnItemClick(view,position,((GoodsListViewHolder) holder).tv_shop_title.getText().toString(),goodslist.get(position).getGoods_image_url(),((GoodsListViewHolder) holder).tv_shop_price.getText().toString());
}
}
});
}else{
((GoodsGridViewHolder)holder).tv_shop_title.setText(goodslist.get(position).getGoods_name());
((GoodsGridViewHolder)holder).tv_shop_text.setText(goodslist.get(position).getStore_name());
((GoodsGridViewHolder)holder).tv_shop_price.setText(goodslist.get(position).getGoods_price());
new BitmapUtil().getpic(goodslist.get(position).getGoods_image_url(),((GoodsGridViewHolder) holder).iv_shop_biao);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener!=null){
listener.OnItemClick(view,position,((GoodsGridViewHolder) holder).tv_shop_title.getText().toString(),goodslist.get(position).getGoods_image_url(),((GoodsGridViewHolder) holder).tv_shop_price.getText().toString());
}
}
});
}
}
@Override
public int getItemCount() {
return goodslist!=null?goodslist.size():0;
}
class GoodsListViewHolder extends RecyclerView.ViewHolder{
ImageView iv_shop_biao;
TextView tv_shop_title;
TextView tv_shop_price;
TextView tv_shop_text;
public GoodsListViewHolder(View itemView) {
super(itemView);
iv_shop_biao=itemView.findViewById(R.id.iv_shop_biao);
tv_shop_title=itemView.findViewById(R.id.tv_shop_title);
tv_shop_price=itemView.findViewById(R.id.tv_shop_price);
tv_shop_text=itemView.findViewById(R.id.tv_shop_text);
}
}
class GoodsGridViewHolder extends RecyclerView.ViewHolder{
ImageView iv_shop_biao;
TextView tv_shop_title;
TextView tv_shop_price;
TextView tv_shop_text;
public GoodsGridViewHolder(View itemView) {
super(itemView);
iv_shop_biao=itemView.findViewById(R.id.iv_shop_biao);
tv_shop_title=itemView.findViewById(R.id.tv_shop_title);
tv_shop_price=itemView.findViewById(R.id.tv_shop_price);
tv_shop_text=itemView.findViewById(R.id.tv_shop_text);
}
}
}
Activity中
searchGoodsAdapter.setOnItemClick(new SearchGoodsAdapter.OnItemClickListener() {
@Override
public void OnItemClick(View view, int position, String title, String pc, String price) {
Intent intent = new Intent(SearchGoodsActivity.this, DetailsActivity.class);
intent.putExtra("title", title);
intent.putExtra("pc", pc);
intent.putExtra("price", price);
startActivity(intent);
return;
}
});