Skip to content

Commit

Permalink
addons: refactor reference activity (fixes #2369) (#2375)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Sep 13, 2023
1 parent 81dda0c commit 8fc6139
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -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<Reference> 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<Reference> 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
Expand All @@ -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);
}
}
}

0 comments on commit 8fc6139

Please sign in to comment.