rewrite scrolling to improve performance

This commit is contained in:
tibbi 2018-01-06 16:10:13 +01:00
parent ad1a787a15
commit 4123f67ad3
9 changed files with 59 additions and 16 deletions

View file

@ -42,7 +42,7 @@ ext {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:3.5.0' implementation 'com.simplemobiletools:commons:3.5.9'
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.9.0' implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.9.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0' implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0'
implementation 'com.android.support:multidex:1.0.2' implementation 'com.android.support:multidex:1.0.2'

View file

@ -402,12 +402,16 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
private fun increaseColumnCount() { private fun increaseColumnCount() {
directories_vertical_fastscroller.measureRecyclerViewOnRedraw()
directories_horizontal_fastscroller.measureRecyclerViewOnRedraw()
config.dirColumnCnt = ++(directories_grid.layoutManager as GridLayoutManager).spanCount config.dirColumnCnt = ++(directories_grid.layoutManager as GridLayoutManager).spanCount
invalidateOptionsMenu() invalidateOptionsMenu()
directories_grid.adapter?.notifyDataSetChanged() directories_grid.adapter?.notifyDataSetChanged()
} }
private fun reduceColumnCount() { private fun reduceColumnCount() {
directories_vertical_fastscroller.measureRecyclerViewOnRedraw()
directories_horizontal_fastscroller.measureRecyclerViewOnRedraw()
config.dirColumnCnt = --(directories_grid.layoutManager as GridLayoutManager).spanCount config.dirColumnCnt = --(directories_grid.layoutManager as GridLayoutManager).spanCount
invalidateOptionsMenu() invalidateOptionsMenu()
directories_grid.adapter?.notifyDataSetChanged() directories_grid.adapter?.notifyDataSetChanged()
@ -533,6 +537,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
directories_empty_text_label.beVisibleIf(dirs.isEmpty() && !isFromCache) directories_empty_text_label.beVisibleIf(dirs.isEmpty() && !isFromCache)
directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache) directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache)
directories_grid.beVisibleIf(directories_empty_text_label.isGone())
val allowHorizontalScroll = config.scrollHorizontally && config.viewTypeFiles == VIEW_TYPE_GRID
directories_vertical_fastscroller.beVisibleIf(directories_grid.isVisible() && !allowHorizontalScroll)
directories_horizontal_fastscroller.beVisibleIf(directories_grid.isVisible() && allowHorizontalScroll)
checkLastMediaChanged() checkLastMediaChanged()
if (dirs.hashCode() == mDirs.hashCode()) { if (dirs.hashCode() == mDirs.hashCode()) {
@ -559,7 +568,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
val currAdapter = directories_grid.adapter val currAdapter = directories_grid.adapter
if (currAdapter == null) { if (currAdapter == null) {
initZoomListener() initZoomListener()
DirectoryAdapter(this, mDirs, this, directories_grid, isPickIntent(intent) || isGetAnyContentIntent(intent)) { val fastscroller = if (config.scrollHorizontally) directories_horizontal_fastscroller else directories_vertical_fastscroller
DirectoryAdapter(this, mDirs, this, directories_grid, isPickIntent(intent) || isGetAnyContentIntent(intent), fastscroller) {
itemClicked((it as Directory).path) itemClicked((it as Directory).path)
}.apply { }.apply {
setupZoomListener(mZoomListener) setupZoomListener(mZoomListener)

View file

@ -182,7 +182,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
val currAdapter = media_grid.adapter val currAdapter = media_grid.adapter
if (currAdapter == null) { if (currAdapter == null) {
initZoomListener() initZoomListener()
MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid) { val fastscroller = if (config.scrollHorizontally) media_horizontal_fastscroller else media_vertical_fastscroller
MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller) {
itemClicked((it as Medium).path) itemClicked((it as Medium).path)
}.apply { }.apply {
setupZoomListener(mZoomListener) setupZoomListener(mZoomListener)
@ -466,12 +467,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
private fun increaseColumnCount() { private fun increaseColumnCount() {
media_vertical_fastscroller.measureRecyclerViewOnRedraw()
media_horizontal_fastscroller.measureRecyclerViewOnRedraw()
config.mediaColumnCnt = ++(media_grid.layoutManager as GridLayoutManager).spanCount config.mediaColumnCnt = ++(media_grid.layoutManager as GridLayoutManager).spanCount
invalidateOptionsMenu() invalidateOptionsMenu()
media_grid.adapter?.notifyDataSetChanged() media_grid.adapter?.notifyDataSetChanged()
} }
private fun reduceColumnCount() { private fun reduceColumnCount() {
media_vertical_fastscroller.measureRecyclerViewOnRedraw()
media_horizontal_fastscroller.measureRecyclerViewOnRedraw()
config.mediaColumnCnt = --(media_grid.layoutManager as GridLayoutManager).spanCount config.mediaColumnCnt = --(media_grid.layoutManager as GridLayoutManager).spanCount
invalidateOptionsMenu() invalidateOptionsMenu()
media_grid.adapter?.notifyDataSetChanged() media_grid.adapter?.notifyDataSetChanged()
@ -548,6 +553,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
media_empty_text_label.beVisibleIf(media.isEmpty() && !isFromCache) media_empty_text_label.beVisibleIf(media.isEmpty() && !isFromCache)
media_empty_text.beVisibleIf(media.isEmpty() && !isFromCache) media_empty_text.beVisibleIf(media.isEmpty() && !isFromCache)
media_grid.beVisibleIf(media_empty_text_label.isGone())
val allowHorizontalScroll = config.scrollHorizontally && config.viewTypeFiles == VIEW_TYPE_GRID
media_vertical_fastscroller.beVisibleIf(media_grid.isVisible() && !allowHorizontalScroll)
media_horizontal_fastscroller.beVisibleIf(media_grid.isVisible() && allowHorizontalScroll)
checkLastMediaChanged() checkLastMediaChanged()
if (mLastDrawnHashCode == 0) if (mLastDrawnHashCode == 0)

View file

@ -11,6 +11,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog
@ -24,7 +25,8 @@ import java.io.File
import java.util.* import java.util.*
class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList<Directory>, val listener: DirOperationsListener?, recyclerView: MyRecyclerView, class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList<Directory>, val listener: DirOperationsListener?, recyclerView: MyRecyclerView,
val isPickIntent: Boolean, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { val isPickIntent: Boolean, fastScroller: FastScroller? = null, itemClick: (Any) -> Unit) :
MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
private val config = activity.config private val config = activity.config
private val isListViewType = config.viewTypeFolders == VIEW_TYPE_LIST private val isListViewType = config.viewTypeFolders == VIEW_TYPE_LIST

View file

@ -14,7 +14,7 @@ import kotlinx.android.synthetic.main.item_manage_folder.view.*
import java.util.* import java.util.*
class ManageFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList<String>, val isShowingExcludedFolders: Boolean, val listener: RefreshRecyclerViewListener?, class ManageFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList<String>, val isShowingExcludedFolders: Boolean, val listener: RefreshRecyclerViewListener?,
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private val config = activity.config private val config = activity.config

View file

@ -16,7 +16,7 @@ import java.io.File
import java.util.* import java.util.*
class ManageHiddenFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList<String>, val listener: RefreshRecyclerViewListener?, class ManageHiddenFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList<String>, val listener: RefreshRecyclerViewListener?,
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private val config = activity.config private val config = activity.config

View file

@ -1,6 +1,8 @@
package com.simplemobiletools.gallery.adapters package com.simplemobiletools.gallery.adapters
import android.net.Uri import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.view.Menu import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -12,22 +14,26 @@ import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.isActivityDestroyed import com.simplemobiletools.commons.extensions.isActivityDestroyed
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.photo_video_item_list.view.* import kotlinx.android.synthetic.main.photo_video_item_grid.view.*
import java.io.File import java.io.File
import java.util.* import java.util.*
class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean,
val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, fastScroller: FastScroller? = null,
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
private val config = activity.config private val config = activity.config
private val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST private val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST
private var skipConfirmationDialog = false private var skipConfirmationDialog = false
private var visibleItemPaths = ArrayList<String>()
private var delayHandler = Handler(Looper.getMainLooper())
private var scrollHorizontally = config.scrollHorizontally private var scrollHorizontally = config.scrollHorizontally
private var animateGifs = config.animateGifs private var animateGifs = config.animateGifs
@ -51,6 +57,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val medium = media[position] val medium = media[position]
visibleItemPaths.add(medium.path)
val view = holder.bindView(medium, !allowMultiplePicks) { itemView, layoutPosition -> val view = holder.bindView(medium, !allowMultiplePicks) { itemView, layoutPosition ->
setupView(itemView, medium) setupView(itemView, medium)
} }
@ -92,7 +99,9 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
override fun onViewRecycled(holder: ViewHolder?) { override fun onViewRecycled(holder: ViewHolder?) {
super.onViewRecycled(holder) super.onViewRecycled(holder)
if (!activity.isActivityDestroyed()) { if (!activity.isActivityDestroyed()) {
Glide.with(activity).clear(holder?.itemView?.medium_thumbnail) val itemView = holder?.itemView
visibleItemPaths.remove(itemView?.photo_name?.tag)
Glide.with(activity).clear(itemView?.medium_thumbnail)
} }
} }
@ -259,7 +268,15 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
play_outline.beVisibleIf(medium.video) play_outline.beVisibleIf(medium.video)
photo_name.beVisibleIf(displayFilenames || isListViewType) photo_name.beVisibleIf(displayFilenames || isListViewType)
photo_name.text = medium.name photo_name.text = medium.name
activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) photo_name.tag = medium.path
medium_thumbnail.isHorizontalScrolling = scrollHorizontally
delayHandler.postDelayed({
val isVisible = visibleItemPaths.contains(medium.path)
if (isVisible) {
activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails)
}
}, 200)
if (isListViewType) { if (isListViewType) {
photo_name.setTextColor(textColor) photo_name.setTextColor(textColor)

View file

@ -36,7 +36,7 @@
<com.simplemobiletools.commons.views.MyRecyclerView <com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/directories_grid" android:id="@+id/directories_grid"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:scrollbars="none" android:scrollbars="none"
app:layoutManager="android.support.v7.widget.GridLayoutManager" app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="@integer/directory_columns_vertical_scroll"/> app:spanCount="@integer/directory_columns_vertical_scroll"/>
@ -48,7 +48,8 @@
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:paddingLeft="@dimen/normal_margin" android:paddingLeft="@dimen/normal_margin"
android:paddingStart="@dimen/normal_margin"> android:paddingStart="@dimen/normal_margin"
android:visibility="gone">
<include layout="@layout/fastscroller_handle_vertical"/> <include layout="@layout/fastscroller_handle_vertical"/>
@ -61,7 +62,8 @@
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:paddingTop="@dimen/normal_margin"> android:paddingTop="@dimen/normal_margin"
android:visibility="gone">
<include layout="@layout/fastscroller_handle_horizontal"/> <include layout="@layout/fastscroller_handle_horizontal"/>

View file

@ -36,7 +36,7 @@
<com.simplemobiletools.commons.views.MyRecyclerView <com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/media_grid" android:id="@+id/media_grid"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:scrollbars="none" android:scrollbars="none"
app:layoutManager="android.support.v7.widget.GridLayoutManager" app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="@integer/media_columns_vertical_scroll"/> app:spanCount="@integer/media_columns_vertical_scroll"/>
@ -48,7 +48,8 @@
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:paddingLeft="@dimen/normal_margin" android:paddingLeft="@dimen/normal_margin"
android:paddingStart="@dimen/normal_margin"> android:paddingStart="@dimen/normal_margin"
android:visibility="gone">
<include layout="@layout/fastscroller_handle_vertical"/> <include layout="@layout/fastscroller_handle_vertical"/>
@ -61,7 +62,8 @@
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:paddingTop="@dimen/normal_margin"> android:paddingTop="@dimen/normal_margin"
android:visibility="gone">
<include layout="@layout/fastscroller_handle_horizontal"/> <include layout="@layout/fastscroller_handle_horizontal"/>