Improve app remember position by using first visible item.
This commit is contained in:
parent
8209559012
commit
a247cc997e
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ public class ApplicationListAdapter extends RecyclerView.Adapter<RecyclerView.Vi
|
|||
final ApplicationListViewHolder holder = (ApplicationListViewHolder) viewHolder;
|
||||
ApplicationViewModel vm = applicationViewModels.get(position);
|
||||
holder.setViewModel(vm);
|
||||
holder.itemView.setOnClickListener(v -> 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<RecyclerView.Vi
|
|||
}
|
||||
|
||||
public interface OnAppClickListener {
|
||||
void onAppClick(ApplicationViewModel vm, int position);
|
||||
void onAppClick(ApplicationViewModel vm);
|
||||
}
|
||||
|
||||
static class ApplicationEmptyViewHolder extends RecyclerView.ViewHolder {
|
||||
|
|
|
@ -10,6 +10,7 @@ import androidx.annotation.NonNull;
|
|||
import androidx.databinding.DataBindingUtil;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.eu.exodus_privacy.exodusprivacy.R;
|
||||
import org.eu.exodus_privacy.exodusprivacy.adapters.ApplicationListAdapter;
|
||||
|
@ -28,6 +29,7 @@ public class AppListFragment extends Fragment {
|
|||
private Type filterType = Type.NAME;
|
||||
private Object filterObject = "";
|
||||
private boolean scrollbarEnabled = true;
|
||||
private static int firstVisiblePosition = 0;
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
|
@ -38,8 +40,21 @@ public class AppListFragment extends Fragment {
|
|||
applications = new ArrayList<>();
|
||||
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<ApplicationViewModel> 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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue