From 3eb48cb3b191b5f777c04dcf130b4d45469eda15 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 4 Dec 2016 00:29:55 +0100 Subject: [PATCH] remember the scroll state when browsing between folders --- .../filemanager/Constants.kt | 1 + .../filemanager/activities/MainActivity.kt | 21 +++++++++++++++---- .../filemanager/fragments/ItemsFragment.kt | 11 +++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/Constants.kt index 7c7c638c..a9f13587 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/Constants.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.filemanager val PATH = "path" +val SCROLL_STATE = "scroll_state" // shared preferences val PREFS_KEY = "File Manager" diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt index 34b81fe6..5ce8e9b4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -5,11 +5,13 @@ import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import android.os.Handler +import android.os.Parcelable import android.support.v4.app.ActivityCompat import android.view.Menu import android.view.MenuItem import com.simplemobiletools.filemanager.PATH import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.SCROLL_STATE import com.simplemobiletools.filemanager.fragments.ItemsFragment import com.simplemobiletools.filepicker.dialogs.StoragePickerDialog import com.simplemobiletools.filepicker.extensions.getInternalStoragePath @@ -19,9 +21,12 @@ import com.simplemobiletools.filepicker.models.FileDirItem import com.simplemobiletools.filepicker.views.Breadcrumbs import kotlinx.android.synthetic.main.activity_main.* import java.io.File +import java.util.* class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener { var mBasePath = getInternalStoragePath() + var latestFragment: ItemsFragment? = null + var mScrollStates = HashMap() companion object { private val STORAGE_PERMISSION = 1 @@ -59,10 +64,18 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br val bundle = Bundle() bundle.putString(PATH, path) - val fragment = ItemsFragment() - fragment.arguments = bundle - fragment.setListener(this) - supportFragmentManager.beginTransaction().replace(R.id.fragment_holder, fragment).addToBackStack(path).commitAllowingStateLoss() + if (mScrollStates.containsKey(path.trimEnd('/'))) { + bundle.putParcelable(SCROLL_STATE, mScrollStates[path.trimEnd('/')]) + } + + if (latestFragment != null) { + mScrollStates.put(latestFragment!!.mPath.trimEnd('/'), latestFragment!!.getScrollState()) + } + + latestFragment = ItemsFragment() + latestFragment!!.arguments = bundle + latestFragment!!.setListener(this) + supportFragmentManager.beginTransaction().replace(R.id.fragment_holder, latestFragment).addToBackStack(path).commitAllowingStateLoss() } override fun onCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt index e329e5cd..a4d421fb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt @@ -5,7 +5,9 @@ import android.content.Intent import android.graphics.Color import android.net.Uri import android.os.Bundle +import android.os.Parcelable import android.support.design.widget.Snackbar +import android.support.v7.widget.LinearLayoutManager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -13,6 +15,7 @@ import android.webkit.MimeTypeMap import com.simplemobiletools.filemanager.Config import com.simplemobiletools.filemanager.PATH import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.SCROLL_STATE import com.simplemobiletools.filemanager.activities.SimpleActivity import com.simplemobiletools.filemanager.adapters.ItemsAdapter import com.simplemobiletools.filemanager.dialogs.CreateNewItemDialog @@ -31,8 +34,8 @@ class ItemsFragment : android.support.v4.app.Fragment(), ItemsAdapter.ItemOperat lateinit var mConfig: Config lateinit var mToBeDeleted: MutableList - private var mPath = "" private var mShowHidden = false + var mPath = "" override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?) = inflater!!.inflate(R.layout.items_fragment, container, false)!! @@ -87,8 +90,14 @@ class ItemsFragment : android.support.v4.app.Fragment(), ItemsAdapter.ItemOperat } else { (currAdapter as ItemsAdapter).updateItems(mItems) } + + getRecyclerLayoutManager().onRestoreInstanceState(arguments.getParcelable(SCROLL_STATE)) } + fun getRecyclerLayoutManager() = (items_list.layoutManager as LinearLayoutManager) + + fun getScrollState() = getRecyclerLayoutManager().onSaveInstanceState() + fun setListener(listener: ItemInteractionListener) { mListener = listener }