From a247cc997ea0afb9309f3290db23950fdbdd5467 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 8 Dec 2020 08:55:26 +0100 Subject: [PATCH] Improve app remember position by using first visible item. --- .../exodusprivacy/MainActivity.java | 9 +------- .../adapters/ApplicationListAdapter.java | 4 ++-- .../fragments/AppListFragment.java | 23 ++++++++++++++++--- .../exodusprivacy/fragments/HomeFragment.java | 9 +------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/MainActivity.java b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/MainActivity.java index d0e32a1..8d811be 100644 --- a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/MainActivity.java +++ b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/MainActivity.java @@ -61,7 +61,6 @@ public class MainActivity extends AppCompatActivity { private String packageName; private MainBinding binding; private ApplicationListAdapter.OnAppClickListener onAppClickListener; - private int appPosition; @Override protected void onCreate(Bundle savedInstanceState) { @@ -72,7 +71,6 @@ public class MainActivity extends AppCompatActivity { getSupportActionBar().setTitle(R.string.app_title); } fragments = new ArrayList<>(); - appPosition = 0; NetworkListener networkListener = new NetworkListener() { @Override public void onSuccess() { @@ -121,11 +119,10 @@ public class MainActivity extends AppCompatActivity { .commit(); }; - onAppClickListener = (vm, position) -> { + onAppClickListener = (vm) -> { try { PackageManager pm = getPackageManager(); PackageInfo packageInfo = pm.getPackageInfo(vm.packageName, PackageManager.GET_PERMISSIONS); - appPosition = position; ReportFragment report = ReportFragment.newInstance(pm, vm, packageInfo, onTrackerClickListener); fragments.add(report); FragmentManager manager = getSupportFragmentManager(); @@ -168,10 +165,6 @@ public class MainActivity extends AppCompatActivity { else { getSupportFragmentManager().popBackStack(); fragments.remove(fragments.size() - 1); - Updatable updatable = fragments.get(fragments.size() - 1); - if (updatable instanceof HomeFragment) { - ((HomeFragment) updatable).scrollTo(appPosition); - } } } diff --git a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/adapters/ApplicationListAdapter.java b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/adapters/ApplicationListAdapter.java index 8ef1073..8e43271 100644 --- a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/adapters/ApplicationListAdapter.java +++ b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/adapters/ApplicationListAdapter.java @@ -86,7 +86,7 @@ public class ApplicationListAdapter extends RecyclerView.Adapter onAppClickListener.onAppClick(vm, position)); + holder.itemView.setOnClickListener(v -> onAppClickListener.onAppClick(vm)); } else //noinspection RedundantSuppression { //noinspection unused @@ -145,7 +145,7 @@ public class ApplicationListAdapter extends RecyclerView.Adapter(); Context context = applistBinding.getRoot().getContext(); //configure list - applistBinding.appList.setLayoutManager(new LinearLayoutManager(context)); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); + applistBinding.appList.setLayoutManager(linearLayoutManager); applistBinding.appList.setVerticalScrollBarEnabled(scrollbarEnabled); + applistBinding.appList.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + super.onScrollStateChanged(recyclerView, newState); + } + + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + firstVisiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition(); + } + }); adapter = new ApplicationListAdapter(onAppClickListener); adapter.displayAppList(applications); adapter.filter(filterType, filterObject); @@ -54,6 +69,8 @@ public class AppListFragment extends Fragment { public void setApplications(List applicationList) { applications = applicationList; if (adapter != null) + + adapter.displayAppList(applications); } @@ -93,8 +110,8 @@ public class AppListFragment extends Fragment { TRACKER } - public void scrollTo(int position) { - applistBinding.appList.scrollToPosition(position); + public void scrollTo() { + applistBinding.appList.scrollToPosition(firstVisiblePosition); } public enum Order { diff --git a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/HomeFragment.java b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/HomeFragment.java index 25abeb7..71c6241 100644 --- a/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/HomeFragment.java +++ b/app/src/main/java/org/eu/exodus_privacy/exodusprivacy/fragments/HomeFragment.java @@ -51,7 +51,6 @@ public class HomeFragment extends Fragment implements ComputeAppListTask.Listene private int lastResource = 0; private int lastProgress = 0; private int lastMaxProgress = 0; - private int scrollTo = 0; private String last_refresh; @Override @@ -112,15 +111,9 @@ public class HomeFragment extends Fragment implements ComputeAppListTask.Listene @Override public void onResume() { super.onResume(); - if (scrollTo > 0) { - appListFragment.scrollTo(scrollTo); - scrollTo = 0; - } + appListFragment.scrollTo(); } - public void scrollTo(int position) { - scrollTo = position; - } @Override public void onUpdateComplete() {