Use view binding inside ``PreferenceViewHolder``

This commit is contained in:
litetex 2022-01-24 21:08:06 +01:00
parent f55e8ea3aa
commit 82de35d724
1 changed files with 18 additions and 22 deletions

View File

@ -4,12 +4,11 @@ import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import org.schabi.newpipe.R; import org.schabi.newpipe.databinding.SettingsPreferencesearchListItemResultBinding;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,37 +21,38 @@ class PreferenceSearchAdapter
@NonNull @NonNull
@Override @Override
public PreferenceSearchAdapter.PreferenceViewHolder onCreateViewHolder( public PreferenceViewHolder onCreateViewHolder(
@NonNull final ViewGroup parent, @NonNull final ViewGroup parent,
final int viewType final int viewType
) { ) {
return new PreferenceViewHolder( return new PreferenceViewHolder(
LayoutInflater SettingsPreferencesearchListItemResultBinding.inflate(
.from(parent.getContext()) LayoutInflater.from(parent.getContext()),
.inflate(R.layout.settings_preferencesearch_list_item_result, parent, false)); parent,
false));
} }
@Override @Override
public void onBindViewHolder( public void onBindViewHolder(
@NonNull final PreferenceSearchAdapter.PreferenceViewHolder holder, @NonNull final PreferenceViewHolder holder,
final int position final int position
) { ) {
final PreferenceSearchItem item = dataset.get(position); final PreferenceSearchItem item = dataset.get(position);
holder.title.setText(item.getTitle()); holder.binding.title.setText(item.getTitle());
if (TextUtils.isEmpty(item.getSummary())) { if (TextUtils.isEmpty(item.getSummary())) {
holder.summary.setVisibility(View.GONE); holder.binding.summary.setVisibility(View.GONE);
} else { } else {
holder.summary.setVisibility(View.VISIBLE); holder.binding.summary.setVisibility(View.VISIBLE);
holder.summary.setText(item.getSummary()); holder.binding.summary.setText(item.getSummary());
} }
if (TextUtils.isEmpty(item.getBreadcrumbs())) { if (TextUtils.isEmpty(item.getBreadcrumbs())) {
holder.breadcrumbs.setVisibility(View.GONE); holder.binding.breadcrumbs.setVisibility(View.GONE);
} else { } else {
holder.breadcrumbs.setVisibility(View.VISIBLE); holder.binding.breadcrumbs.setVisibility(View.VISIBLE);
holder.breadcrumbs.setText(item.getBreadcrumbs()); holder.binding.breadcrumbs.setText(item.getBreadcrumbs());
} }
holder.itemView.setOnClickListener(v -> { holder.itemView.setOnClickListener(v -> {
@ -77,15 +77,11 @@ class PreferenceSearchAdapter
} }
static class PreferenceViewHolder extends RecyclerView.ViewHolder { static class PreferenceViewHolder extends RecyclerView.ViewHolder {
final TextView title; final SettingsPreferencesearchListItemResultBinding binding;
final TextView summary;
final TextView breadcrumbs;
PreferenceViewHolder(final View v) { PreferenceViewHolder(final SettingsPreferencesearchListItemResultBinding binding) {
super(v); super(binding.getRoot());
title = v.findViewById(R.id.title); this.binding = binding;
summary = v.findViewById(R.id.summary);
breadcrumbs = v.findViewById(R.id.breadcrumbs);
} }
} }
} }