Recycler View dengan OnItemClickListener
hello temen2,
yuk kita akhiri kelas Recycler View dengan metode OnItemClickListener
1. langkah pertama kita buka kelas CardViewHeroAdapter,
package com.example.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
public class CardViewHeroAdapter extends RecyclerView.Adapter<CardViewHeroAdapter.CardViewViewHolder> {
private ArrayList<Hero> listHero;
public CardViewHeroAdapter(ArrayList<Hero> list){
this.listHero = list;
}
@NonNull
@Override
public CardViewViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_cardview_hero, viewGroup, false);
return new CardViewViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final CardViewViewHolder holder, int position) {
Hero hero = listHero.get(position);
Glide.with(holder.itemView.getContext())
.load(hero.getPhoto())
.apply(new RequestOptions().override(350, 550))
.into(holder.imgPhoto);
holder.tvName.setText(hero.getName());
holder.tvDetail.setText(hero.getDetail());
holder.btnFavorite.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(holder.itemView.getContext(), "Favorite" +
listHero.get(holder.getAdapterPosition()).getName(), Toast.LENGTH_SHORT).show();
}
});
holder.btnShare.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(holder.itemView.getContext(), "Share" +
listHero.get(holder.getAdapterPosition()).getName(), Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(holder.itemView.getContext(), "Kamu memilih" + listHero.get(holder.getAdapterPosition()).getName(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return listHero.size();
}
public class CardViewViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName, tvDetail;
Button btnFavorite, btnShare;
public CardViewViewHolder(@NonNull View itemView){
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
tvName = itemView.findViewById(R.id.tv_item);
tvDetail = itemView.findViewById(R.id.tv_item_detail);
btnFavorite = itemView.findViewById(R.id.btn_set_favorite);
btnShare = itemView.findViewById(R.id.btn_set_share);
}
}
}
2. langkah kedua, kita buka kelas ListHeroAdapter,
package com.example.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class ListHeroAdapter extends RecyclerView.Adapter<ListHeroAdapter.ListViewHolder> {
private ArrayList<Hero> listHero;
public ListHeroAdapter(ArrayList<Hero> list) {
this.listHero = list;
}
private OnItemClickCallback onItemClickCallback;
public void setOnItemClickCallback(OnItemClickCallback onItemClickCallback){
this.onItemClickCallback = onItemClickCallback;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
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) {
Hero hero = listHero.get(position);
Glide.with(holder.itemView.getContext())
.load(hero.getPhoto())
.apply(new RequestOptions().override(55, 55))
.into(holder.imgPhoto);
holder.tvName.setText(hero.getName());
holder.tvDetail.setText(hero.getDetail());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickCallback.onItemClicked(listHero.get(holder.getAdapterPosition()));
}
});
}
@Override
public int getItemCount() {
return listHero.size();
}
class ListViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName, tvDetail;
ListViewHolder(View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
tvName = itemView.findViewById(R.id.tv_item);
tvDetail = itemView.findViewById(R.id.tv_item_detail);
}
}
public interface OnItemClickCallback{
void onItemClicked(Hero data);
}
}
3. langkah ketiga, kita buka kelas GridHeroAdapter
package com.example.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
public class GridHeroAdapter extends RecyclerView.Adapter<GridHeroAdapter.GridViewHolder> {
private ArrayList<Hero> listHero;
public GridHeroAdapter(ArrayList<Hero> list) {
this.listHero = list;
}
private OnItemClickCallback onItemClickCallback;
public void setOnItemClickCallback(OnItemClickCallback onItemClickCallback){
this.onItemClickCallback = onItemClickCallback;
}
@NonNull
@Override
public GridViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grid_hero, viewGroup, false);
return new GridViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final GridViewHolder holder, int position) {
Glide.with(holder.itemView.getContext())
.load(listHero.get(position).getPhoto())
.apply(new RequestOptions().override(350, 550))
.into(holder.imgPhoto);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickCallback.onItemClicked(listHero.get(holder.getAdapterPosition()));
}
});
}
@Override
public int getItemCount() {
return listHero.size();
}
class GridViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
GridViewHolder(View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
}
}
public interface OnItemClickCallback{
void onItemClicked(Hero data);
}
}
4. langkah keempat, kita buka kelas activity main,
package com.example.myrecyclerview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView rvHeroes;
private ArrayList<Hero> list = new ArrayList<>();
private void setActionBarTitle(String title){
if (getSupportActionBar() !=null){
getSupportActionBar().setTitle(title);
}
}
private String title = "Mode List";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBarTitle(title);
rvHeroes = findViewById(R.id.rv_heroes);
rvHeroes.setHasFixedSize(true);
list.addAll(HeroesData.getListData());
showRecyclerList();
}
private void showSelectedHero(Hero hero){
Toast.makeText(this, "Kamu memilih " + hero.getName(),Toast.LENGTH_SHORT).show();
}
private void showRecyclerCardView(){
rvHeroes.setLayoutManager(new LinearLayoutManager(this));
CardViewHeroAdapter cardViewHeroAdapter = new CardViewHeroAdapter(list);
rvHeroes.setAdapter(cardViewHeroAdapter);
}
private void showRecyclerGrid(){
rvHeroes.setLayoutManager(new GridLayoutManager(this,2));
GridHeroAdapter gridHeroAdapter = new GridHeroAdapter(list);
rvHeroes.setAdapter(gridHeroAdapter);
gridHeroAdapter.setOnItemClickCallback(new GridHeroAdapter.OnItemClickCallback() {
@Override
public void onItemClicked(Hero data) {
showSelectedHero(data);
}
});
}
private void showRecyclerList(){
rvHeroes.setLayoutManager(new LinearLayoutManager(this));
ListHeroAdapter listHeroAdapter = new ListHeroAdapter(list);
rvHeroes.setAdapter(listHeroAdapter);
listHeroAdapter.setOnItemClickCallback(new ListHeroAdapter.OnItemClickCallback() {
@Override
public void onItemClicked(Hero data) {
showSelectedHero(data);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
setMode(item.getItemId());
return super.onOptionsItemSelected(item);
}
public void setMode(int selectedMode){
switch (selectedMode){
case R.id.action_list:
title = "Mode List";
showRecyclerList();
break;
case R.id.action_grid:
title = "Mode Grid";
showRecyclerGrid();
break;
case R.id.action_cardview:
title = "Mode CardView";
showRecyclerCardView();
break;
}
setActionBarTitle(title);
}
}
dan bila sudah bisa kita run,
dan akan tampak tampilan seperti berikut...
Komentar
Posting Komentar