Fixed wrong text being saved in settings, also implemented orientation changes

This commit is contained in:
Deniz Düzgören
2019-05-17 11:10:59 +02:00
parent 3119fdac73
commit 3b7a89b512
6 changed files with 72 additions and 13 deletions
@@ -1,6 +1,7 @@
package com.denizd.substitutionplan;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
@@ -14,6 +15,7 @@ import com.google.android.material.snackbar.Snackbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
@@ -45,7 +47,9 @@ public class FragmentPlan extends Fragment {
private RecyclerView recyclerView;
private CardAdapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()),
linearLayoutManager = new LinearLayoutManager(getContext()),
gridLayoutManager = new GridLayoutManager(getContext(), 2);
private ArrayList<Subst> planCardList;
private TextView bottomSheetText;
private SubstViewModel substViewModel;
@@ -56,6 +60,19 @@ public class FragmentPlan extends Fragment {
return inflater.inflate(R.layout.plan, null);
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
layoutManager = linearLayoutManager;
recyclerView.setLayoutManager(layoutManager);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
layoutManager = gridLayoutManager;
recyclerView.setLayoutManager(layoutManager);
}
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
@@ -68,8 +85,15 @@ public class FragmentPlan extends Fragment {
planCardList = new ArrayList<>();
recyclerView = getView().findViewById(R.id.linearRecycler);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
layoutManager = linearLayoutManager;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
layoutManager = gridLayoutManager;
}
recyclerView.setLayoutManager(layoutManager);
mAdapter = new CardAdapter(planCardList);
recyclerView.setAdapter(mAdapter);