Skip to content

Commit

Permalink
Use ViewBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
litetex committed Jan 9, 2022
1 parent 0aedee9 commit 27bed01
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
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.
Expand All @@ -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) {
Expand All @@ -40,17 +38,16 @@ public View onCreateView(
@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) {
Expand All @@ -69,8 +66,8 @@ public void updateSearchResults(final String keyword) {
}

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) {
Expand All @@ -81,14 +78,4 @@ public void onItemClicked(final PreferenceSearchItem item) {

((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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:id="@+id/searchResults"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
Expand Down

0 comments on commit 27bed01

Please sign in to comment.