razituli

Untitled

Oct 14th, 2021
699
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.basicandroid.day4.recyclerview;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9.  
  10. import androidx.annotation.NonNull;
  11. import androidx.recyclerview.widget.RecyclerView;
  12.  
  13. import com.example.basicandroid.R;
  14.  
  15. import java.util.ArrayList;
  16.  
  17. public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
  18.     private ArrayList<Item> items;
  19.     private CallbackAdapter callbackAdapter;
  20.  
  21.     public ItemAdapter(ArrayList<Item> items,CallbackAdapter callbackAdapter){
  22.         this.items = items;
  23.         this.callbackAdapter = callbackAdapter;
  24.     }
  25.  
  26.     @NonNull
  27.     @Override
  28.     public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  29.         Context context = parent.getContext();
  30.         LayoutInflater inflater = LayoutInflater.from(context);
  31.         View view = inflater.inflate(R.layout.day4_recyclerview_item, parent, false);
  32.         ViewHolder viewHolder = new ViewHolder(view);
  33.         return viewHolder;
  34.     }
  35.  
  36.     @Override
  37.     public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  38.         Item item = items.get(position);
  39.  
  40.         holder.tvName.setText(item.getName());
  41.         holder.tvDescription.setText(item.getDescription());
  42.  
  43.         holder.itemView.setOnClickListener(new View.OnClickListener() {
  44.             @Override
  45.             public void onClick(View view) {
  46.                 callbackAdapter.onClick(item);
  47.             }
  48.         });
  49.     }
  50.  
  51.     @Override
  52.     public int getItemCount() {
  53.         return items.size();
  54.     }
  55.  
  56.     public class ViewHolder extends RecyclerView.ViewHolder{
  57.  
  58.         TextView tvName;
  59.         TextView tvDescription;
  60.  
  61.         public ViewHolder(@NonNull View itemView) {
  62.             super(itemView);
  63.             tvName = itemView.findViewById(R.id.rc_tv_name);
  64.             tvDescription = itemView.findViewById(R.id.rc_tv_description);
  65. //            itemView.setOnClickListener(this);
  66.         }
  67.  
  68. //        @Override
  69. //        public void onClick(View v) {
  70. ////            int position = getAbsoluteAdapterPosition(); // gets item position
  71. ////            if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
  72. ////                Item item = items.get(position);
  73. ////                // We can access the data within the views
  74. ////                Toast.makeText(v.getContext(), tvName.getText(), Toast.LENGTH_SHORT).show();
  75. ////            }
  76. //        }
  77.     }
  78.  
  79.    public interface  CallbackAdapter{
  80.         public void onClick(Item item);
  81.     }
  82.  
  83. }
  84.  
RAW Paste Data