daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Mar 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ExampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  2.  
  3.     private List<HouseRent> mData;
  4.     private Activity mContext;
  5.    
  6.     //interface for callback
  7.     private OnItemClickListener mOnItemClickListener;
  8.     public interface OnItemClickListener {
  9.         void onItemClick(View view, HouseRent houseRent, int position);
  10.     }
  11.     public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
  12.         this.mOnItemClickListener = mItemClickListener;
  13.     }
  14.     //interface code ends
  15.  
  16.    
  17.     public ExampleAdapter(Activity context, List<HouseRent> data) {
  18.         this.mData = data;
  19.         this.mContext = context;
  20.     }
  21.  
  22.     @Override
  23.     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  24.        
  25.         View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.house_rent_item, parent, false);
  26.         return new HouseRentViewHolder(view);
  27.        
  28.     }
  29.  
  30.     @Override
  31.     public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
  32.         final HouseRent houseRent = mData.get(position);
  33.         int viewType = holder.getItemViewType();
  34.         if (viewType == HouseRent.HOUSE_RENT_TYPE) {
  35.             HouseRentViewHolder houseRentViewHolder = (HouseRentViewHolder) holder;
  36.             houseRentViewHolder.lyt_parent.setOnClickListener(new View.OnClickListener() {
  37.                 @Override
  38.                 public void onClick(View v) {
  39.                     //pass onclick to interface
  40.                     mOnItemClickListener.onItemClick(v,houseRent,position);
  41.                 }
  42.             });
  43.         }
  44.  
  45.     }
  46.    
  47.     @Override
  48.     public int getItemCount() {
  49.         return mData.size();
  50.     }
  51.  
  52.  
  53.     // stores and recycles views as they are scrolled off screen
  54.     public class HouseRentViewHolder extends RecyclerView.ViewHolder {
  55.  
  56.         ConstraintLayout lyt_parent;
  57.         TextView txtInfo;
  58.  
  59.         HouseRentViewHolder(View itemView) {
  60.             super(itemView);
  61.             txtInfo = itemView.findViewById(R.id.txt_info);
  62.             lyt_parent = itemView.findViewById(R.id.lyt_parent);
  63.         }
  64.     }
  65.    
  66.     HouseRent getItem(int id) {
  67.         return mData.get(id);
  68.     }
  69.  
  70.    
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top