Skip to content

Commit

Permalink
Add customs
Browse files Browse the repository at this point in the history
* add custom adapter
* add custom layout manager
  • Loading branch information
whalemare committed Jun 7, 2017
1 parent d7705c0 commit 5f947a5
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 34 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/ru/whalemare/bottomsheet/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.button_show_menu).setOnClickListener(new View.OnClickListener() {
findViewById(R.id.button_linear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setup();
Expand All @@ -41,7 +41,7 @@ private void setup2() {
R.string.title,
null,
R.menu.menu,
null,
null, null,
new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Expand Down
25 changes: 18 additions & 7 deletions app/src/main/java/ru/whalemare/bottomsheet/MainActivityKotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,43 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.view.MenuItem
import android.widget.CheckBox
import ru.whalemare.sheetmenu.SheetMenu

open class MainActivityKotlin : AppCompatActivity() {

var needTitle = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

findViewById(R.id.button_show_menu).setOnClickListener({
setup()
(findViewById(R.id.checkbox_title) as CheckBox)
.setOnCheckedChangeListener { _, isChecked -> needTitle = isChecked }

findViewById(R.id.button_linear).setOnClickListener({
setupLinear()
})

findViewById(R.id.button_grid).setOnClickListener({
setupGrid()
})

}

fun setup() {
fun setupLinear() {
SheetMenu().apply {
titleId = R.string.title
layoutManager = GridLayoutManager(this@MainActivityKotlin, 2)
titleId = if (needTitle) R.string.title else -1
click = MenuItem.OnMenuItemClickListener { true }
menu = R.menu.menu
}.show(this)
}

fun setup2() {
fun setupGrid() {
SheetMenu(
titleId = if (needTitle) R.string.title else -1,
menu = R.menu.menu,
titleId = R.string.title,
layoutManager = GridLayoutManager(this, 3),
click = MenuItem.OnMenuItemClickListener { true }
).show(this)
}
Expand Down
43 changes: 37 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,52 @@
android:background="#4f4f4f"
tools:context="ru.whalemare.bottomsheet.MainActivity">

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/checkbox_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="128dp"
android:text="@string/text_with_title"
android:textColor="#fff"
app:buttonTint="#fff"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
/>

<Button
android:id="@+id/button_show_menu"
android:id="@+id/button_linear"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="#fff"
android:text="@string/text_show_menu"
app:layout_constraintBottom_toBottomOf="parent"
android:text="@string/text_linear"
app:layout_constraintHorizontal_bias="0.229"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintBaseline_toBaselineOf="@+id/button_grid"/>

<Button
android:id="@+id/button_grid"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:backgroundTint="#fff"

android:text="@string/text_grid"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.203"
app:layout_constraintLeft_toRightOf="@+id/button_linear"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/checkbox_title"
app:layout_constraintVertical_bias="0.32"/>

</android.support.constraint.ConstraintLayout>
16 changes: 12 additions & 4 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_first"
android:id="@+id/action_one"
android:icon="@drawable/ic_atom"
android:title="First"/>
android:title="One"/>
<item
android:id="@+id/action_second"
android:id="@+id/action_two"
android:icon="@drawable/ic_disco"
android:title="Second"/>
android:title="Two"/>
<item
android:id="@+id/action_three"
android:icon="@drawable/ic_atom"
android:title="Three"/>
<item
android:id="@+id/action_four"
android:icon="@drawable/ic_disco"
android:title="Four"/>
</menu>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<resources>
<string name="app_name">BottomSheet</string>
<string name="title">Title</string>
<string name="text_show_menu">Show menu</string>
<string name="text_linear">Linear</string>
<string name="text_grid">Grid</string>
<string name="text_with_title">With title</string>
</resources>
50 changes: 44 additions & 6 deletions sheetmenu/src/main/java/ru/whalemare/sheetmenu/SheetMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package ru.whalemare.sheetmenu

import android.content.Context
import android.support.design.widget.BottomSheetDialog
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import ru.whalemare.sheetmenu.adapter.MenuAdapter
import ru.whalemare.sheetmenu.extension.inflate
import ru.whalemare.sheetmenu.extension.marginTop
import ru.whalemare.sheetmenu.extension.toList

/**
* Developed with ❤
Expand All @@ -17,14 +21,15 @@ import ru.whalemare.sheetmenu.extension.inflate
* @param titleId id from resources with text for title. it is more important than common <b>title</b> param
* @param title string with text for title
* @param menu id from resources for auto-inflate menu
* @param layoutManager for RecyclerView. By default: vertical linear layout manager.
* @param layoutManager for RecyclerView. By default: vertical linear layout manager. If you set this, @param <b>mode</b> is not be used.
* @param click listener for menu items
*/
open class SheetMenu(
var titleId: Int = 0,
var title: String? = "",
var menu: Int = 0,
var layoutManager: RecyclerView.LayoutManager? = null,
var adapter: MenuAdapter? = null,
var click: MenuItem.OnMenuItemClickListener = MenuItem.OnMenuItemClickListener { false }) {

fun show(context: Context) {
Expand All @@ -38,9 +43,18 @@ open class SheetMenu(

BottomSheetDialog(context).apply {
setContentView(root)
processGrid(root)
}.show()
}

private fun processGrid(root: View) {
if (root.findViewById(R.id.text_title).visibility != View.VISIBLE) {
if (layoutManager is GridLayoutManager) {
root.marginTop(24)
}
}
}

open protected fun processTitle(textTitle: TextView) {
if (titleId > 0) {
textTitle.setText(titleId)
Expand All @@ -55,11 +69,25 @@ open class SheetMenu(

open protected fun processRecycler(recycler: RecyclerView) {
if (menu > 0) {
recycler.adapter = ListAdapter.with(recycler.context.inflate(menu)).apply {
callback = click
if (layoutManager == null) {
layoutManager = LinearLayoutManager(recycler.context, LinearLayoutManager.VERTICAL, false)
}

var itemLayoutId = R.layout.item_linear
if (layoutManager is GridLayoutManager) {
itemLayoutId = R.layout.item_grid
}

if (adapter == null) {
adapter = MenuAdapter(
menuItems = recycler.context.inflate(menu).toList(),
callback = click,
itemLayoutId = itemLayoutId
)
}
recycler.layoutManager = layoutManager ?:
LinearLayoutManager(recycler.context, LinearLayoutManager.VERTICAL, false)

recycler.adapter = adapter
recycler.layoutManager = layoutManager
}
}

Expand All @@ -76,10 +104,11 @@ open class SheetMenu(
private var title = ""
private var menu: Int = 0
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: MenuAdapter? = null
private var click: MenuItem.OnMenuItemClickListener = MenuItem.OnMenuItemClickListener { false }

fun show() {
SheetMenu(0, title, menu, layoutManager, click).show(context)
SheetMenu(0, title, menu, layoutManager, null, click).show(context)
}

/**
Expand Down Expand Up @@ -121,6 +150,15 @@ open class SheetMenu(
this.layoutManager = layoutManager
return this
}

/**
* @param adapter for RecyclerView.
* @see MenuAdapter by default
*/
fun setAdapter(adapter: MenuAdapter): Builder {
this.adapter = adapter
return this
}
}
//endregion
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package ru.whalemare.sheetmenu
package ru.whalemare.sheetmenu.adapter

import android.support.annotation.LayoutRes
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import ru.whalemare.sheetmenu.R
import ru.whalemare.sheetmenu.extension.toList

/**
* Developed with ❤
* @since 2017
* @author Anton Vlasov - whalemare
*/
open class ListAdapter(var menuItems: List<MenuItem> = emptyList(),
var callback: MenuItem.OnMenuItemClickListener? = null)
: RecyclerView.Adapter<ListAdapter.ViewHolder>() {
open class MenuAdapter(var menuItems: List<MenuItem> = emptyList(),
var callback: MenuItem.OnMenuItemClickListener? = null,
var itemLayoutId: Int = 0)
: RecyclerView.Adapter<MenuAdapter.ViewHolder>() {

companion object {
fun with(menu: Menu): ListAdapter {
return ListAdapter(menu.toList())
fun with(itemLayoutId: Int, menu: Menu): MenuAdapter {
return MenuAdapter(menuItems = menu.toList(), itemLayoutId = itemLayoutId)
}
}

@LayoutRes
open fun getLayoutItem(): Int {
return R.layout.item_sheet_list
return itemLayoutId
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.support.v7.view.SupportMenuInflater
import android.support.v7.view.menu.MenuBuilder
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup


/**
* Developed by Magora-Systems.com
Expand All @@ -23,4 +26,17 @@ fun Context.inflate(@MenuRes menuRes: Int): Menu {
val menuInflater = SupportMenuInflater(this)
menuInflater.inflate(menuRes, menu)
return menu
}
}

fun View.marginBottom(dp: Int) {
val params = this.layoutParams as ViewGroup.MarginLayoutParams
params.bottomMargin = dp2px(this.context, dp)
}

fun View.marginTop(dp: Int) {
val params = this.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = dp2px(this.context, dp)
this.layoutParams = params
}

fun dp2px(context: Context, dp: Int): Int = (dp * context.resources.displayMetrics.density + 0.5).toInt()
31 changes: 31 additions & 0 deletions sheetmenu/src/main/res/layout/item_grid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:background="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="8dp">

<ImageView
android:id="@+id/image_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:padding="8dp"
tools:background="@color/gray_material"/>

<TextView
android:id="@+id/text_title"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:textSize="16sp"
tools:text="Item name name"/>

</LinearLayout>

0 comments on commit 5f947a5

Please sign in to comment.