From 4f9aa55dfe3ed96a5b8ab321e2c869f10bf127f8 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sat, 9 Apr 2022 15:09:14 +0200 Subject: [PATCH] Explorer grid layout --- .../adapters/ExplorerElementAdapter.kt | 9 ++- .../droidfs/explorers/BaseExplorerActivity.kt | 37 ++++++++++-- app/src/main/res/drawable/icon_view_grid.xml | 10 ++++ app/src/main/res/drawable/icon_view_list.xml | 10 ++++ .../layout/adapter_explorer_element_grid.xml | 59 +++++++++++++++++++ ....xml => adapter_explorer_element_list.xml} | 0 app/src/main/res/layout/adapter_volume.xml | 4 +- app/src/main/res/layout/explorer_info_bar.xml | 31 ++++++++-- app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/styles.xml | 1 - build.gradle | 2 +- 11 files changed, 149 insertions(+), 15 deletions(-) create mode 100644 app/src/main/res/drawable/icon_view_grid.xml create mode 100644 app/src/main/res/drawable/icon_view_list.xml create mode 100644 app/src/main/res/layout/adapter_explorer_element_grid.xml rename app/src/main/res/layout/{adapter_explorer_element.xml => adapter_explorer_element_list.xml} (100%) diff --git a/app/src/main/java/sushi/hardcore/droidfs/adapters/ExplorerElementAdapter.kt b/app/src/main/java/sushi/hardcore/droidfs/adapters/ExplorerElementAdapter.kt index 6b5d223..b7c5921 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/adapters/ExplorerElementAdapter.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/adapters/ExplorerElementAdapter.kt @@ -29,6 +29,7 @@ class ExplorerElementAdapter( val dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault()) var explorerElements = listOf() val selectedItems: MutableList = ArrayList() + var isUsingListLayout = true override fun getItemCount(): Int { return explorerElements.size @@ -197,7 +198,13 @@ class ExplorerElementAdapter( } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { - val view = activity.layoutInflater.inflate(R.layout.adapter_explorer_element, parent, false) + val view = activity.layoutInflater.inflate( + if (isUsingListLayout) { + R.layout.adapter_explorer_element_list + } else { + R.layout.adapter_explorer_element_grid + }, parent, false + ) return when (viewType) { ExplorerElement.REGULAR_FILE_TYPE -> FileViewHolder(view) ExplorerElement.DIRECTORY_TYPE -> DirectoryViewHolder(view) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index dfa140e..175a003 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -10,12 +10,14 @@ import android.os.IBinder import android.view.Menu import android.view.MenuItem import android.view.View +import android.widget.ImageButton import android.widget.TextView import android.widget.Toast import androidx.core.content.ContextCompat import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout @@ -60,6 +62,9 @@ open class BaseExplorerActivity : BaseActivity() { protected var isStartingActivity = false private var usf_open = false protected var usf_keep_open = false + private lateinit var linearLayoutManager: LinearLayoutManager + private var isUsingListLayout = true + private lateinit var layoutIcon: ImageButton private lateinit var titleText: TextView private lateinit var recycler_view_explorer: RecyclerView private lateinit var refresher: SwipeRefreshLayout @@ -103,12 +108,22 @@ open class BaseExplorerActivity : BaseActivity() { ::onExplorerItemLongClick, sharedPrefs.getLong(ConstValues.THUMBNAIL_MAX_SIZE_KEY, ConstValues.DEFAULT_THUMBNAIL_MAX_SIZE)*1000, ) - explorerViewModel= ViewModelProvider(this).get(ExplorerViewModel::class.java) + explorerViewModel = ViewModelProvider(this).get(ExplorerViewModel::class.java) currentDirectoryPath = explorerViewModel.currentDirectoryPath setCurrentPath(currentDirectoryPath) - recycler_view_explorer.apply { - adapter = explorerAdapter - layoutManager = LinearLayoutManager(this@BaseExplorerActivity) + linearLayoutManager = LinearLayoutManager(this@BaseExplorerActivity) + recycler_view_explorer.adapter = explorerAdapter + isUsingListLayout = sharedPrefs.getBoolean("useListLayout", true) + layoutIcon = findViewById(R.id.layout_icon) + setRecyclerViewLayout() + layoutIcon.setOnClickListener { + isUsingListLayout = !isUsingListLayout + setRecyclerViewLayout() + recycler_view_explorer.recycledViewPool.clear() + with (sharedPrefs.edit()) { + putBoolean("useListLayout", isUsingListLayout) + apply() + } } refresher.setOnRefreshListener { setCurrentPath(currentDirectoryPath) @@ -121,6 +136,20 @@ open class BaseExplorerActivity : BaseActivity() { var currentDirectoryPath = "" } + private fun setRecyclerViewLayout() { + layoutIcon.setImageResource(if (isUsingListLayout) { + recycler_view_explorer.layoutManager = linearLayoutManager + explorerAdapter.isUsingListLayout = true + R.drawable.icon_view_list + } else { + val displayMetrics = resources.displayMetrics + val columnsNumber = (displayMetrics.widthPixels / displayMetrics.density / 200 + 0.5).toInt() + recycler_view_explorer.layoutManager = GridLayoutManager(this, columnsNumber) + explorerAdapter.isUsingListLayout = false + R.drawable.icon_view_grid + }) + } + protected open fun init() { setContentView(R.layout.activity_explorer_base) } diff --git a/app/src/main/res/drawable/icon_view_grid.xml b/app/src/main/res/drawable/icon_view_grid.xml new file mode 100644 index 0000000..78298cd --- /dev/null +++ b/app/src/main/res/drawable/icon_view_grid.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/icon_view_list.xml b/app/src/main/res/drawable/icon_view_list.xml new file mode 100644 index 0000000..9884aeb --- /dev/null +++ b/app/src/main/res/drawable/icon_view_list.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/adapter_explorer_element_grid.xml b/app/src/main/res/layout/adapter_explorer_element_grid.xml new file mode 100644 index 0000000..1accf31 --- /dev/null +++ b/app/src/main/res/layout/adapter_explorer_element_grid.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/adapter_explorer_element.xml b/app/src/main/res/layout/adapter_explorer_element_list.xml similarity index 100% rename from app/src/main/res/layout/adapter_explorer_element.xml rename to app/src/main/res/layout/adapter_explorer_element_list.xml diff --git a/app/src/main/res/layout/adapter_volume.xml b/app/src/main/res/layout/adapter_volume.xml index 8a5efd8..76f01d5 100644 --- a/app/src/main/res/layout/adapter_volume.xml +++ b/app/src/main/res/layout/adapter_volume.xml @@ -35,14 +35,14 @@ + android:paddingHorizontal="20dp"> + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintStart_toEndOf="@id/current_path_text" + app:layout_constraintEnd_toStartOf="@id/layout_icon"/> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3336aca..7decd99 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -233,4 +233,5 @@ Invalid number New volume name: Failed to rename volume + Switch display layout diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index abbd0f0..7fc1cfe 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -59,6 +59,5 @@ start true 15sp - center \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4b2644d..b036d8f 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.1.2' + classpath 'com.android.tools.build:gradle:7.1.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }