From f55e8ea3aad21b2897e7bcecb84bb3d077d514b3 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Sun, 9 Jan 2022 17:40:16 +0100 Subject: [PATCH] Use ViewBinding --- .../PreferenceSearchFragment.java | 31 ++++++------------- .../settings_preferencesearch_fragment.xml | 2 +- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java index 38c87ea76..f0944876e 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceSearchFragment.java @@ -10,13 +10,11 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import org.schabi.newpipe.R; +import org.schabi.newpipe.databinding.SettingsPreferencesearchFragmentBinding; import java.util.ArrayList; import java.util.List; -import java.util.Objects; /** * Displays the search results. @@ -26,7 +24,7 @@ public class PreferenceSearchFragment extends Fragment { private PreferenceSearcher searcher; - private SearchViewHolder viewHolder; + private SettingsPreferencesearchFragmentBinding binding; private PreferenceSearchAdapter adapter; public void setSearcher(final PreferenceSearcher searcher) { @@ -40,17 +38,16 @@ public class PreferenceSearchFragment extends Fragment { @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState ) { - final View rootView = - inflater.inflate(R.layout.settings_preferencesearch_fragment, container, false); + // SettingsPreferenceSearchFragmentBinding. + binding = SettingsPreferencesearchFragmentBinding.inflate(inflater, container, false); - viewHolder = new SearchViewHolder(rootView); - viewHolder.recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + binding.searchResults.setLayoutManager(new LinearLayoutManager(getContext())); adapter = new PreferenceSearchAdapter(); adapter.setOnItemClickListener(this::onItemClicked); - viewHolder.recyclerView.setAdapter(adapter); + binding.searchResults.setAdapter(adapter); - return rootView; + return binding.getRoot(); } public void updateSearchResults(final String keyword) { @@ -69,8 +66,8 @@ public class PreferenceSearchFragment extends Fragment { } private void setEmptyViewShown(final boolean shown) { - viewHolder.emptyStateView.setVisibility(shown ? View.VISIBLE : View.GONE); - viewHolder.recyclerView.setVisibility(shown ? View.GONE : View.VISIBLE); + binding.emptyStateView.setVisibility(shown ? View.VISIBLE : View.GONE); + binding.searchResults.setVisibility(shown ? View.GONE : View.VISIBLE); } public void onItemClicked(final PreferenceSearchItem item) { @@ -81,14 +78,4 @@ public class PreferenceSearchFragment extends Fragment { ((PreferenceSearchResultListener) getActivity()).onSearchResultClicked(item); } - - private static class SearchViewHolder { - private final RecyclerView recyclerView; - private final View emptyStateView; - - SearchViewHolder(final View root) { - recyclerView = Objects.requireNonNull(root.findViewById(R.id.list)); - emptyStateView = Objects.requireNonNull(root.findViewById(R.id.empty_state_view)); - } - } } diff --git a/app/src/main/res/layout/settings_preferencesearch_fragment.xml b/app/src/main/res/layout/settings_preferencesearch_fragment.xml index b8aaa60f6..89a25b217 100644 --- a/app/src/main/res/layout/settings_preferencesearch_fragment.xml +++ b/app/src/main/res/layout/settings_preferencesearch_fragment.xml @@ -41,7 +41,7 @@