commit
73c5f27a19
@ -0,0 +1,50 @@ |
||||
package net.pokeranalytics.android.ui.adapter |
||||
|
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import kotlinx.android.synthetic.main.row_bottom_sheet_grid_title.view.* |
||||
import net.pokeranalytics.android.R |
||||
|
||||
class TableSizeGridAdapter(private var tableSizes: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
||||
|
||||
companion object { |
||||
const val ROW_TABLE_SIZE: Int = 100 |
||||
} |
||||
|
||||
var onClickOnItem: ((position: Int) -> Unit)? = null |
||||
|
||||
inner class CellSessionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
||||
|
||||
fun bind(tableSize: String) { |
||||
itemView.title.text = tableSize |
||||
itemView.container.setOnClickListener { |
||||
onClickOnItem?.invoke(adapterPosition) |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { |
||||
when (viewType) { |
||||
ROW_TABLE_SIZE -> return CellSessionViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_bottom_sheet_grid_title, parent, false)) |
||||
else -> throw IllegalStateException("Need to implement type $viewType in HistoryAdapter") |
||||
} |
||||
} |
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
||||
when (getItemViewType(position)) { |
||||
ROW_TABLE_SIZE -> (holder as TableSizeGridAdapter.CellSessionViewHolder).bind(tableSizes[position]) |
||||
} |
||||
} |
||||
|
||||
override fun getItemCount(): Int { |
||||
return tableSizes.size |
||||
} |
||||
|
||||
override fun getItemViewType(position: Int): Int { |
||||
return ROW_TABLE_SIZE |
||||
} |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,82 @@ |
||||
package net.pokeranalytics.android.ui.fragment.components.bottomsheet |
||||
|
||||
import android.os.Bundle |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import androidx.recyclerview.widget.GridLayoutManager |
||||
import kotlinx.android.synthetic.main.bottom_sheet_grid.* |
||||
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.* |
||||
import net.pokeranalytics.android.ui.adapter.TableSizeGridAdapter |
||||
import net.pokeranalytics.android.ui.view.GridSpacingItemDecoration |
||||
import net.pokeranalytics.android.util.px |
||||
|
||||
|
||||
class BottomSheetTableSizeGridFragment : BottomSheetFragment() { |
||||
|
||||
private var dataList: ArrayList<String> = ArrayList() |
||||
private lateinit var dataAdapter: TableSizeGridAdapter |
||||
private var defaultSize: Int? = null |
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
||||
super.onViewCreated(view, savedInstanceState) |
||||
initData() |
||||
initUI() |
||||
} |
||||
|
||||
override fun onResume() { |
||||
super.onResume() |
||||
dataAdapter.notifyDataSetChanged() |
||||
} |
||||
|
||||
override fun getValue(): Any? { |
||||
return defaultSize |
||||
} |
||||
|
||||
/** |
||||
* Init data |
||||
*/ |
||||
private fun initData() { |
||||
|
||||
val bottomSheetData = getData() |
||||
if (bottomSheetData.isNotEmpty() && bottomSheetData.first().defaultValue != null) { |
||||
defaultSize = bottomSheetData.first().defaultValue as Int? |
||||
} |
||||
|
||||
dataList.add(getString(net.pokeranalytics.android.R.string.heads_up)) |
||||
for (i in 3..10) { |
||||
dataList.add("$i-max") |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Init UI |
||||
*/ |
||||
private fun initUI() { |
||||
|
||||
setAddButtonVisible(false) |
||||
|
||||
LayoutInflater.from(requireContext()) |
||||
.inflate(net.pokeranalytics.android.R.layout.bottom_sheet_grid, view?.bottomSheetContainer, true) |
||||
|
||||
val viewManager = GridLayoutManager(requireContext(), 3) |
||||
dataAdapter = TableSizeGridAdapter(dataList) |
||||
dataAdapter.onClickOnItem = { position -> |
||||
bottomSheetDelegate.setValue(position + 2, row) |
||||
dismiss() |
||||
} |
||||
|
||||
val spanCount = 3 |
||||
val spacing = 2.px |
||||
val includeEdge = false |
||||
|
||||
reyclerView.apply { |
||||
setHasFixedSize(true) |
||||
layoutManager = viewManager |
||||
adapter = dataAdapter |
||||
addItemDecoration(GridSpacingItemDecoration(spanCount, spacing, includeEdge)) |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,34 @@ |
||||
package net.pokeranalytics.android.ui.view |
||||
|
||||
import android.graphics.Rect |
||||
import android.view.View |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
|
||||
/** |
||||
* Manage spacing for recycler view & grid layout manager |
||||
*/ |
||||
class GridSpacingItemDecoration(val spanCount: Int, val spacing: Int, val includeEdge: Boolean) : |
||||
RecyclerView.ItemDecoration() { |
||||
|
||||
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { |
||||
val position = parent.getChildAdapterPosition(view) // item position |
||||
val column = position % spanCount // item column |
||||
|
||||
if (includeEdge) { |
||||
outRect.left = spacing - column * spacing / spanCount // spacing - column * ((1f / spanCount) * spacing) |
||||
outRect.right = (column + 1) * spacing / spanCount // (column + 1) * ((1f / spanCount) * spacing) |
||||
|
||||
if (position < spanCount) { // top edge |
||||
outRect.top = spacing |
||||
} |
||||
outRect.bottom = spacing // item bottom |
||||
} else { |
||||
outRect.left = column * spacing / spanCount // column * ((1f / spanCount) * spacing) |
||||
outRect.right = |
||||
spacing - (column + 1) * spacing / spanCount // spacing - (column + 1) * ((1f / spanCount) * spacing) |
||||
if (position >= spanCount) { |
||||
outRect.top = spacing // item top |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,11 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/reyclerView" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="200dp" /> |
||||
|
||||
</FrameLayout> |
||||
@ -0,0 +1,26 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:id="@+id/container" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="64dp" |
||||
android:background="@color/kaki_light" |
||||
android:foreground="?selectableItemBackground" |
||||
android:padding="8dp"> |
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView |
||||
android:id="@+id/title" |
||||
android:layout_width="0dp" |
||||
android:layout_height="0dp" |
||||
android:ellipsize="end" |
||||
android:gravity="center" |
||||
android:lines="1" |
||||
android:maxLines="1" |
||||
app:layout_constraintBottom_toBottomOf="parent" |
||||
app:layout_constraintEnd_toEndOf="parent" |
||||
app:layout_constraintStart_toStartOf="parent" |
||||
app:layout_constraintTop_toTopOf="parent" |
||||
tools:text="Data Type Title" /> |
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout> |
||||
Loading…
Reference in new issue