package fpt.gstpro.myapi.service; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import java.util.List; import fpt.gstpro.myapi.HeroInformationActivity; import fpt.gstpro.myapi.R; import fpt.gstpro.myapi.model.Hero; public class HeroAdapter extends RecyclerView.Adapter { private List heroes; public HeroAdapter(List heroes) { this.heroes = heroes; } public class ViewHolder extends RecyclerView.ViewHolder { public final TextView lbHeroName; public final ImageView imgAvatar; public ViewHolder(View itemView) { super(itemView); lbHeroName = itemView.findViewById(R.id.lbHeroName); imgAvatar = itemView.findViewById(R.id.imgAvatar); } } @Override public HeroAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.marvel_hero_item, viewGroup, false); return new HeroAdapter.ViewHolder(v); } @Override public void onBindViewHolder(HeroAdapter.ViewHolder viewHolder, int i) { Hero hero = heroes.get(i); viewHolder.lbHeroName.setText(hero.getName()); Glide.with(viewHolder.itemView.getContext()) .load(hero.getImageUrl()) .centerCrop() .placeholder(R.drawable.ic_launcher_background) .into(viewHolder.imgAvatar); viewHolder.lbHeroName.setOnClickListener( v -> { Intent intent = new Intent(v.getContext(), HeroInformationActivity.class); intent.putExtra("hero", hero); v.getContext().startActivity(intent); }); } @Override public int getItemCount() { return heroes != null ? heroes.size() : 0; } }