Explorer grid layout

This commit is contained in:
Matéo Duparc 2022-04-09 15:09:14 +02:00
parent 91de54018d
commit 4f9aa55dfe
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A
11 changed files with 149 additions and 15 deletions

View File

@ -29,6 +29,7 @@ class ExplorerElementAdapter(
val dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault()) val dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault())
var explorerElements = listOf<ExplorerElement>() var explorerElements = listOf<ExplorerElement>()
val selectedItems: MutableList<Int> = ArrayList() val selectedItems: MutableList<Int> = ArrayList()
var isUsingListLayout = true
override fun getItemCount(): Int { override fun getItemCount(): Int {
return explorerElements.size return explorerElements.size
@ -197,7 +198,13 @@ class ExplorerElementAdapter(
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { 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) { return when (viewType) {
ExplorerElement.REGULAR_FILE_TYPE -> FileViewHolder(view) ExplorerElement.REGULAR_FILE_TYPE -> FileViewHolder(view)
ExplorerElement.DIRECTORY_TYPE -> DirectoryViewHolder(view) ExplorerElement.DIRECTORY_TYPE -> DirectoryViewHolder(view)

View File

@ -10,12 +10,14 @@ import android.os.IBinder
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.ImageButton
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
@ -60,6 +62,9 @@ open class BaseExplorerActivity : BaseActivity() {
protected var isStartingActivity = false protected var isStartingActivity = false
private var usf_open = false private var usf_open = false
protected var usf_keep_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 titleText: TextView
private lateinit var recycler_view_explorer: RecyclerView private lateinit var recycler_view_explorer: RecyclerView
private lateinit var refresher: SwipeRefreshLayout private lateinit var refresher: SwipeRefreshLayout
@ -106,9 +111,19 @@ open class BaseExplorerActivity : BaseActivity() {
explorerViewModel = ViewModelProvider(this).get(ExplorerViewModel::class.java) explorerViewModel = ViewModelProvider(this).get(ExplorerViewModel::class.java)
currentDirectoryPath = explorerViewModel.currentDirectoryPath currentDirectoryPath = explorerViewModel.currentDirectoryPath
setCurrentPath(currentDirectoryPath) setCurrentPath(currentDirectoryPath)
recycler_view_explorer.apply { linearLayoutManager = LinearLayoutManager(this@BaseExplorerActivity)
adapter = explorerAdapter recycler_view_explorer.adapter = explorerAdapter
layoutManager = LinearLayoutManager(this@BaseExplorerActivity) 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 { refresher.setOnRefreshListener {
setCurrentPath(currentDirectoryPath) setCurrentPath(currentDirectoryPath)
@ -121,6 +136,20 @@ open class BaseExplorerActivity : BaseActivity() {
var currentDirectoryPath = "" 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() { protected open fun init() {
setContentView(R.layout.activity_explorer_base) setContentView(R.layout.activity_explorer_base)
} }

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M4,11h5L9,5L4,5v6zM4,18h5v-6L4,12v6zM10,18h5v-6h-5v6zM16,18h5v-6h-5v6zM10,11h5L15,5h-5v6zM16,5v6h5L21,5h-5z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M4,14h4v-4L4,10v4zM4,19h4v-4L4,15v4zM4,9h4L8,5L4,5v4zM9,14h12v-4L9,10v4zM9,19h12v-4L9,15v4zM9,5v4h12L21,5L9,5z"/>
</vector>

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/selectable_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/icon_element"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_gravity="center_horizontal|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_element_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/adapter_text_size"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<TextView
android:id="@+id/text_element_mtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/details_text_size"
android:layout_marginEnd="7dp"/>
<TextView
android:id="@+id/text_element_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/details_text_size"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@ -35,14 +35,14 @@
<TextView <TextView
android:id="@+id/text_path" android:id="@+id/text_path"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="7dp" android:layout_marginEnd="7dp"
android:singleLine="true" android:singleLine="true"
android:ellipsize="middle" android:ellipsize="middle"
app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0" app:layout_constraintHorizontal_bias="0"
app:layout_constraintWidth_default="wrap" app:layout_constrainedWidth="true"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/text_info" app:layout_constraintEnd_toStartOf="@id/text_info"

View File

@ -1,28 +1,47 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="25dp" android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/infoBarBackgroundColor" android:background="?attr/infoBarBackgroundColor"
android:gravity="center_vertical"> android:paddingHorizontal="20dp">
<TextView <TextView
android:id="@+id/current_path_text" android:id="@+id/current_path_text"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
style="@style/infoBarTextView" style="@style/infoBarTextView"
android:singleLine="true"
android:ellipsize="start"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0"
app:layout_constrainedWidth="true"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/total_size_text"/> app:layout_constraintEnd_toStartOf="@id/total_size_text"/>
<TextView <TextView
android:id="@+id/total_size_text" android:id="@+id/total_size_text"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
style="@style/infoBarTextView" style="@style/infoBarTextView"
android:text="@string/default_total_size" android:text="@string/default_total_size"
android:layout_marginHorizontal="20dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/current_path_text"/> app:layout_constraintStart_toEndOf="@id/current_path_text"
app:layout_constraintEnd_toStartOf="@id/layout_icon"/>
<ImageButton
android:id="@+id/layout_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_view_grid"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/switch_display_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -233,4 +233,5 @@
<string name="invalid_number">Invalid number</string> <string name="invalid_number">Invalid number</string>
<string name="new_volume_name">New volume name:</string> <string name="new_volume_name">New volume name:</string>
<string name="volume_rename_failed">Failed to rename volume</string> <string name="volume_rename_failed">Failed to rename volume</string>
<string name="switch_display_layout">Switch display layout</string>
</resources> </resources>

View File

@ -59,6 +59,5 @@
<item name="android:ellipsize">start</item> <item name="android:ellipsize">start</item>
<item name="android:singleLine">true</item> <item name="android:singleLine">true</item>
<item name="android:textSize">15sp</item> <item name="android:textSize">15sp</item>
<item name="android:textAlignment">center</item>
</style> </style>
</resources> </resources>

View File

@ -6,7 +6,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { 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" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }