What is the right way of Android View Binding in the RecyclerView adapter class?

前端 未结 5 1794
时光说笑
时光说笑 2021-02-14 07:28

Here is the code I used in my RecycleView adapter class. I don\'t know this is the right way or not to use View Binding. If you have a better solution answer me. Th

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 07:50

    Here is full recycler view adapter class in java :

    public class NotesAdapter extends RecyclerView.Adapter {
    
        private List notes;
        private ItemNotesBinding notesBinding;
    
        public NotesAdapter(List notes) {
            this.notes = notes;
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        
            notesBinding = ItemNotesBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new MyViewHolder(notesBinding);
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    
            Note note = notes.get(position);
            notesBinding.tvTitle.setText(note.getNote());
        }
    
        @Override
        public int getItemCount() {
            return notes.size();
        }
    
        public static class MyViewHolder extends RecyclerView.ViewHolder {
    
            ItemNotesBinding notesBinding;
    
            public MyViewHolder(@NonNull ItemNotesBinding binding) {
                super(binding.getRoot());
                notesBinding = binding;
            }
        }
    }
    

提交回复
热议问题