Replace Butterknife with Viewbinding in ItemAdapter

This commit is contained in:
Christoph Loy 2020-02-25 17:33:02 +01:00 committed by Niedermann IT-Dienstleistungen
parent 61a2341893
commit 077c303a71

View file

@ -14,9 +14,9 @@ import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemNotesListNoteItemBinding;
import it.niedermann.owncloud.notes.databinding.ItemNotesListSectionItemBinding;
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION; import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
@ -111,7 +111,7 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public void clearSelection(RecyclerView recyclerView) { public void clearSelection(RecyclerView recyclerView) {
for (Integer i : getSelected()) { for (Integer i : getSelected()) {
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i); RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
if(viewHolder != null) { if (viewHolder != null) {
viewHolder.itemView.setSelected(false); viewHolder.itemView.setSelected(false);
} else { } else {
Log.w(TAG, "Could not found viewholder to remove selection"); Log.w(TAG, "Could not found viewholder to remove selection");
@ -168,32 +168,23 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} }
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener { public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
@BindView(R.id.noteSwipeable)
public View noteSwipeable; public View noteSwipeable;
final View noteSwipeFrame; private final ItemNotesListNoteItemBinding b;
final ImageView noteFavoriteLeft; private final TextView noteTitle;
final ImageView noteDeleteRight; private final TextView noteCategory;
final TextView noteTitle; private final TextView noteExcerpt;
@BindView(R.id.noteCategory) private final ImageView noteStatus;
TextView noteCategory; private final ImageView noteFavorite;
@BindView(R.id.noteExcerpt)
TextView noteExcerpt;
@BindView(R.id.noteStatus)
ImageView noteStatus;
@BindView(R.id.noteFavorite)
ImageView noteFavorite;
private NoteViewHolder(View v) { private NoteViewHolder(View v) {
super(v); super(v);
this.noteSwipeFrame = v.findViewById(R.id.noteSwipeFrame); b = ItemNotesListNoteItemBinding.bind(v);
this.noteSwipeable = v.findViewById(R.id.noteSwipeable); this.noteSwipeable = b.noteSwipeable;
this.noteFavoriteLeft = v.findViewById(R.id.noteFavoriteLeft); this.noteTitle = b.noteTitle;
this.noteDeleteRight = v.findViewById(R.id.noteDeleteRight); this.noteCategory = b.noteCategory;
this.noteTitle = v.findViewById(R.id.noteTitle); this.noteExcerpt = b.noteExcerpt;
this.noteCategory = v.findViewById(R.id.noteCategory); this.noteStatus = b.noteStatus;
this.noteExcerpt = v.findViewById(R.id.noteExcerpt); this.noteFavorite = b.noteFavorite;
this.noteStatus = v.findViewById(R.id.noteStatus);
this.noteFavorite = v.findViewById(R.id.noteFavorite);
v.setOnClickListener(this); v.setOnClickListener(this);
v.setOnLongClickListener(this); v.setOnLongClickListener(this);
} }
@ -212,19 +203,19 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} }
public void showSwipe(boolean left) { public void showSwipe(boolean left) {
noteFavoriteLeft.setVisibility(left ? View.VISIBLE : View.INVISIBLE); b.noteFavoriteLeft.setVisibility(left ? View.VISIBLE : View.INVISIBLE);
noteDeleteRight.setVisibility(left ? View.INVISIBLE : View.VISIBLE); b.noteDeleteRight.setVisibility(left ? View.INVISIBLE : View.VISIBLE);
noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention); b.noteSwipeFrame.setBackgroundResource(left ? R.color.bg_warning : R.color.bg_attention);
} }
} }
public static class SectionViewHolder extends RecyclerView.ViewHolder { public static class SectionViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.sectionTitle) private TextView sectionTitle;
TextView sectionTitle;
private SectionViewHolder(View view) { private SectionViewHolder(View view) {
super(view); super(view);
ButterKnife.bind(this, view); ItemNotesListSectionItemBinding binding = ItemNotesListSectionItemBinding.bind(view);
this.sectionTitle = binding.sectionTitle;
} }
} }
} }