implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
public class HeroesData {
public static String[][] data = new String[][]{
{"Captain America", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-09-512.png"},
{"Dead Pool", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-15-512.png"},
{"Iron Man", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-14-512.png"},
{"Ant Man", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-10-512.png"},
{"Bat Man", "DC Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-22-512.png"},
{"Thor", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-20-512.png"},
{"Hulk", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-03-512.png"},
{"Flash", "DC Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-18-512.png"},
{"Spiderman", "Marvel Comics", "https://cdn0.iconfinder.com/data/icons/famous-character-vol-1-colored/48/JD-16-512.png"},
};
public static ArrayList<Hero> getListData() {
ArrayList<Hero> list = new ArrayList<>();
for (String[] aData : data) {
Hero hero = new Hero();
hero.setName(aData[0]);
hero.setFrom(aData[1]);
hero.setPhoto(aData[2]);
list.add(hero);
}
return list;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img_item_photo"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
tools:src="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/img_item_photo"
android:layout_toRightOf="@id/img_item_photo"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Nama Pahlawan"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_item_from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="Asal" />
</LinearLayout>
</RelativeLayout>
public class HeroListAdapter extends Adapter<HeroListAdapter.ListViewHolder> {
private ArrayList<Hero> listHero;
public HeroListAdapter(ArrayList<Hero> list) {
this.listHero = list;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_row_hero, viewGroup, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ListViewHolder holder, int position) {
final Hero hero = listHero.get(position);
Glide.with(holder.context)
.load(hero.getPhoto())
.apply(new RequestOptions().override(55, 55))
.into(holder.imgPhoto);
holder.tvName.setText(hero.getName());
holder.tvFrom.setText(hero.getFrom());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(holder.context,""+hero.getName(),Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return listHero.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName, tvFrom;
Context context;
ListViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
imgPhoto = itemView.findViewById(R.id.img_item_photo);
tvName = itemView.findViewById(R.id.tv_item_name);
tvFrom = itemView.findViewById(R.id.tv_item_from);
}
}
}
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private ArrayList<Hero> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
list.addAll(HeroesData.getListData());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
HeroListAdapter heroListAdapter = new HeroListAdapter(list);
recyclerView.setAdapter(heroListAdapter);
}
}