Implemented zoom listener to dynamically change span count

This commit is contained in:
merkost 2023-07-12 18:30:03 +10:00
parent c0037073d1
commit 2e889f2093
2 changed files with 83 additions and 34 deletions

View File

@ -7,13 +7,11 @@ import android.graphics.drawable.Icon
import android.net.Uri import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import android.util.TypedValue import android.util.TypedValue
import android.view.Menu import android.view.*
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
@ -42,21 +40,27 @@ class ContactsAdapter(
recyclerView: MyRecyclerView, recyclerView: MyRecyclerView,
highlightText: String = "", highlightText: String = "",
private val refreshItemsListener: RefreshItemsListener? = null, private val refreshItemsListener: RefreshItemsListener? = null,
private val viewType: Int = VIEW_TYPE_LIST, var viewType: Int = VIEW_TYPE_LIST,
private val showDeleteButton: Boolean = true, private val showDeleteButton: Boolean = true,
private val enableDrag: Boolean = false, private val enableDrag: Boolean = false,
itemClick: (Any) -> Unit itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), ItemTouchHelperContract { ) : MyRecyclerViewAdapter(activity, recyclerView, itemClick),
ItemTouchHelperContract, MyRecyclerView.MyZoomListener {
private var textToHighlight = highlightText private var textToHighlight = highlightText
private var fontSize = activity.getTextSize() private var fontSize = activity.getTextSize()
private var touchHelper: ItemTouchHelper? = null private var touchHelper: ItemTouchHelper? = null
private var startReorderDragListener: StartReorderDragListener? = null private var startReorderDragListener: StartReorderDragListener? = null
var onDragEndListener: (() -> Unit)? = null var onDragEndListener: (() -> Unit)? = null
var onSpanCountListener: (Int) -> Unit = {}
init { init {
setupDragListener(true) setupDragListener(true)
if (recyclerView.layoutManager is GridLayoutManager) {
setupZoomListener(this)
}
if (enableDrag) { if (enableDrag) {
touchHelper = ItemTouchHelper(ItemMoveCallback(this, viewType == VIEW_TYPE_GRID)) touchHelper = ItemTouchHelper(ItemMoveCallback(this, viewType == VIEW_TYPE_GRID))
touchHelper!!.attachToRecyclerView(recyclerView) touchHelper!!.attachToRecyclerView(recyclerView)
@ -340,4 +344,33 @@ class ContactsAdapter(
override fun onRowClear(myViewHolder: ViewHolder?) { override fun onRowClear(myViewHolder: ViewHolder?) {
onDragEndListener?.invoke() onDragEndListener?.invoke()
} }
override fun zoomIn() {
val layoutManager = recyclerView.layoutManager
if (layoutManager is GridLayoutManager) {
val currentSpanCount = layoutManager.spanCount
val newSpanCount = (currentSpanCount - 1).coerceIn(MIN_COLUMNS, MAX_COLUMNS)
layoutManager.spanCount = newSpanCount
recyclerView.requestLayout()
onSpanCountListener(newSpanCount)
}
}
override fun zoomOut() {
val layoutManager = recyclerView.layoutManager
if (layoutManager is GridLayoutManager) {
val currentSpanCount = layoutManager.spanCount
val newSpanCount = (currentSpanCount + 1).coerceIn(MIN_COLUMNS, MAX_COLUMNS)
layoutManager.spanCount = newSpanCount
recyclerView.requestLayout()
onSpanCountListener(newSpanCount)
}
}
companion object {
private const val MIN_COLUMNS = 2
private const val MAX_COLUMNS = 6
}
} }

View File

@ -91,42 +91,56 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
val viewType = context.config.viewType val viewType = context.config.viewType
setViewType(viewType) setViewType(viewType)
ContactsAdapter(
activity = activity as SimpleActivity, val currAdapter = fragment_list.adapter as ContactsAdapter?
contacts = allContacts, if (currAdapter == null) {
recyclerView = fragment_list, ContactsAdapter(
refreshItemsListener = this, activity = activity as SimpleActivity,
viewType = viewType, contacts = allContacts,
showDeleteButton = false, recyclerView = fragment_list,
enableDrag = true, refreshItemsListener = this,
) { viewType = viewType,
if (context.config.showCallConfirmation) { showDeleteButton = false,
CallConfirmationDialog(activity as SimpleActivity, (it as Contact).getNameToDisplay()) { enableDrag = true,
) {
if (context.config.showCallConfirmation) {
CallConfirmationDialog(activity as SimpleActivity, (it as Contact).getNameToDisplay()) {
activity?.apply {
initiateCall(it) { launchCallIntent(it) }
}
}
} else {
activity?.apply { activity?.apply {
initiateCall(it) { launchCallIntent(it) } initiateCall(it as Contact) { launchCallIntent(it) }
} }
} }
} else { }.apply {
activity?.apply { fragment_list.adapter = this
initiateCall(it as Contact) { launchCallIntent(it) }
onDragEndListener = {
val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) {
val items = adapter.contacts
saveCustomOrderToPrefs(items)
setupLetterFastScroller(items)
}
}
onSpanCountListener = { newSpanCount ->
context.config.gridLayoutSpanCount = newSpanCount
} }
} }
}.apply {
fragment_list.adapter = this
onDragEndListener = {
val adapter = fragment_list?.adapter if (context.areSystemAnimationsEnabled) {
if (adapter is ContactsAdapter) { fragment_list.scheduleLayoutAnimation()
val items = adapter.contacts
saveCustomOrderToPrefs(items)
setupLetterFastScroller(items)
}
} }
} else {
currAdapter.updateItems(allContacts)
currAdapter.viewType = viewType
currAdapter.recyclerView.requestLayout()
} }
if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation()
}
} }
private fun sortByCustomOrder(favorites: List<Contact>): ArrayList<Contact> { private fun sortByCustomOrder(favorites: List<Contact>): ArrayList<Contact> {
@ -182,9 +196,11 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
} }
private fun setViewType(viewType: Int) { private fun setViewType(viewType: Int) {
val spanCount = context.config.gridLayoutSpanCount
val layoutManager = if (viewType == VIEW_TYPE_GRID) { val layoutManager = if (viewType == VIEW_TYPE_GRID) {
letter_fastscroller.beGone() letter_fastscroller.beGone()
MyGridLayoutManager(context, 3) MyGridLayoutManager(context, spanCount)
} else { } else {
letter_fastscroller.beVisible() letter_fastscroller.beVisible()
MyLinearLayoutManager(context) MyLinearLayoutManager(context)