From 8fc6139ff8e91a16f064c749ca1b7ee20ddcc5b4 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 13 Sep 2023 22:12:15 +0300 Subject: [PATCH] addons: refactor reference activity (fixes #2369) (#2375) Co-authored-by: dogi --- app/build.gradle | 4 +- .../ui/references/ReferenceFragment.java | 46 ++++++++----------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f4f55b2fc6..4230042b7c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1030 - versionName "0.10.30" + versionCode 1031 + versionName "0.10.31" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/references/ReferenceFragment.java b/app/src/main/java/org/ole/planet/myplanet/ui/references/ReferenceFragment.java index 545a8ba7e8..d1f4108001 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/references/ReferenceFragment.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/references/ReferenceFragment.java @@ -6,8 +6,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; @@ -16,6 +14,8 @@ import org.ole.planet.myplanet.R; import org.ole.planet.myplanet.callback.OnHomeItemClickListener; +import org.ole.planet.myplanet.databinding.FragmentReferenceBinding; +import org.ole.planet.myplanet.databinding.RowReferenceBinding; import org.ole.planet.myplanet.model.Reference; import org.ole.planet.myplanet.ui.dictionary.DictionaryActivity; import org.ole.planet.myplanet.ui.map.OfflineMapActivity; @@ -24,7 +24,8 @@ import java.util.List; public class ReferenceFragment extends Fragment { - RecyclerView rvReference; + private FragmentReferenceBinding fragmentReferenceBinding; + private RowReferenceBinding rowReferenceBinding; OnHomeItemClickListener homeItemClickListener; public ReferenceFragment() { @@ -39,38 +40,35 @@ public void onAttach(Context context) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.fragment_reference, container, false); + fragmentReferenceBinding = FragmentReferenceBinding.inflate(inflater, container, false); List list = new ArrayList<>(); list.add(new Reference(getString(R.string.maps), android.R.drawable.ic_dialog_map)); list.add(new Reference(getString(R.string.engilsh_dictionary), R.drawable.ic_dictionary)); - rvReference = v.findViewById(R.id.rv_references); - rvReference.setLayoutManager(new GridLayoutManager(getActivity(), 3)); + fragmentReferenceBinding.rvReferences.setLayoutManager(new GridLayoutManager(getActivity(), 3)); setRecyclerAdapter(list); - return v; + return fragmentReferenceBinding.getRoot(); } private void setRecyclerAdapter(List list) { - rvReference.setAdapter(new RecyclerView.Adapter() { + fragmentReferenceBinding.rvReferences.setAdapter(new RecyclerView.Adapter() { @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - return new ViewHolderReference(LayoutInflater.from(getActivity()).inflate(R.layout.row_reference, parent, false)); + rowReferenceBinding = RowReferenceBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false); + return new ViewHolderReference(rowReferenceBinding.getRoot()); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { - if (holder instanceof ViewHolderReference) { - ((ViewHolderReference) holder).title.setText(list.get(position).getTitle()); - ((ViewHolderReference) holder).icon.setImageResource(list.get(position).getIcon()); - holder.itemView.setOnClickListener(view -> { - if (position == 0) - startActivity(new Intent(getActivity(), OfflineMapActivity.class)); - else { - startActivity(new Intent(getActivity(), DictionaryActivity.class)); - } - ; - }); - } + rowReferenceBinding.title.setText(list.get(position).getTitle()); + rowReferenceBinding.icon.setImageResource(list.get(position).getIcon()); + rowReferenceBinding.getRoot().setOnClickListener(view -> { + if (position == 0) + startActivity(new Intent(getActivity(), OfflineMapActivity.class)); + else { + startActivity(new Intent(getActivity(), DictionaryActivity.class)); + } + }); } @Override @@ -80,14 +78,10 @@ public int getItemCount() { }); } - class ViewHolderReference extends RecyclerView.ViewHolder { - TextView title; - ImageView icon; + static class ViewHolderReference extends RecyclerView.ViewHolder { public ViewHolderReference(View itemView) { super(itemView); - title = itemView.findViewById(R.id.title); - icon = itemView.findViewById(R.id.icon); } } }