Skip to content

Commit 5f947a5

Browse files
committed
Add customs
* add custom adapter * add custom layout manager
1 parent d7705c0 commit 5f947a5

File tree

10 files changed

+173
-34
lines changed

10 files changed

+173
-34
lines changed

app/src/main/java/ru/whalemare/bottomsheet/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected void onCreate(Bundle savedInstanceState) {
1414
super.onCreate(savedInstanceState);
1515
setContentView(R.layout.activity_main);
1616

17-
findViewById(R.id.button_show_menu).setOnClickListener(new View.OnClickListener() {
17+
findViewById(R.id.button_linear).setOnClickListener(new View.OnClickListener() {
1818
@Override
1919
public void onClick(View v) {
2020
setup();
@@ -41,7 +41,7 @@ private void setup2() {
4141
R.string.title,
4242
null,
4343
R.menu.menu,
44-
null,
44+
null, null,
4545
new MenuItem.OnMenuItemClickListener() {
4646
@Override
4747
public boolean onMenuItemClick(MenuItem item) {

app/src/main/java/ru/whalemare/bottomsheet/MainActivityKotlin.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,43 @@ import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.support.v7.widget.GridLayoutManager
66
import android.view.MenuItem
7+
import android.widget.CheckBox
78
import ru.whalemare.sheetmenu.SheetMenu
89

910
open class MainActivityKotlin : AppCompatActivity() {
1011

12+
var needTitle = false
13+
1114
override fun onCreate(savedInstanceState: Bundle?) {
1215
super.onCreate(savedInstanceState)
1316
setContentView(R.layout.activity_main)
1417

15-
findViewById(R.id.button_show_menu).setOnClickListener({
16-
setup()
18+
(findViewById(R.id.checkbox_title) as CheckBox)
19+
.setOnCheckedChangeListener { _, isChecked -> needTitle = isChecked }
20+
21+
findViewById(R.id.button_linear).setOnClickListener({
22+
setupLinear()
1723
})
24+
25+
findViewById(R.id.button_grid).setOnClickListener({
26+
setupGrid()
27+
})
28+
1829
}
1930

20-
fun setup() {
31+
fun setupLinear() {
2132
SheetMenu().apply {
22-
titleId = R.string.title
23-
layoutManager = GridLayoutManager(this@MainActivityKotlin, 2)
33+
titleId = if (needTitle) R.string.title else -1
2434
click = MenuItem.OnMenuItemClickListener { true }
2535
menu = R.menu.menu
2636
}.show(this)
2737
}
2838

29-
fun setup2() {
39+
fun setupGrid() {
3040
SheetMenu(
41+
titleId = if (needTitle) R.string.title else -1,
3142
menu = R.menu.menu,
32-
titleId = R.string.title,
43+
layoutManager = GridLayoutManager(this, 3),
3344
click = MenuItem.OnMenuItemClickListener { true }
3445
).show(this)
3546
}

app/src/main/res/layout/activity_main.xml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,52 @@
88
android:background="#4f4f4f"
99
tools:context="ru.whalemare.bottomsheet.MainActivity">
1010

11+
<android.support.v7.widget.AppCompatCheckBox
12+
android:id="@+id/checkbox_title"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:layout_marginTop="128dp"
16+
android:text="@string/text_with_title"
17+
android:textColor="#fff"
18+
app:buttonTint="#fff"
19+
app:layout_constraintTop_toTopOf="parent"
20+
android:layout_marginRight="8dp"
21+
app:layout_constraintRight_toRightOf="parent"
22+
android:layout_marginLeft="8dp"
23+
app:layout_constraintLeft_toLeftOf="parent"
24+
/>
25+
1126
<Button
12-
android:id="@+id/button_show_menu"
27+
android:id="@+id/button_linear"
1328
android:layout_width="120dp"
1429
android:layout_height="60dp"
15-
android:layout_marginBottom="8dp"
1630
android:layout_marginEnd="8dp"
1731
android:layout_marginLeft="8dp"
1832
android:layout_marginRight="8dp"
1933
android:layout_marginStart="8dp"
20-
android:layout_marginTop="8dp"
2134
android:backgroundTint="#fff"
22-
android:text="@string/text_show_menu"
23-
app:layout_constraintBottom_toBottomOf="parent"
35+
android:text="@string/text_linear"
36+
app:layout_constraintHorizontal_bias="0.229"
2437
app:layout_constraintLeft_toLeftOf="parent"
2538
app:layout_constraintRight_toRightOf="parent"
26-
app:layout_constraintTop_toTopOf="parent"/>
39+
app:layout_constraintBaseline_toBaselineOf="@+id/button_grid"/>
40+
41+
<Button
42+
android:id="@+id/button_grid"
43+
android:layout_width="120dp"
44+
android:layout_height="60dp"
45+
android:layout_marginBottom="8dp"
46+
android:layout_marginLeft="8dp"
47+
android:layout_marginRight="8dp"
48+
android:backgroundTint="#fff"
49+
50+
android:text="@string/text_grid"
51+
app:layout_constraintBottom_toBottomOf="parent"
52+
app:layout_constraintHorizontal_bias="0.203"
53+
app:layout_constraintLeft_toRightOf="@+id/button_linear"
54+
app:layout_constraintRight_toRightOf="parent"
55+
android:layout_marginTop="8dp"
56+
app:layout_constraintTop_toBottomOf="@+id/checkbox_title"
57+
app:layout_constraintVertical_bias="0.32"/>
2758

2859
</android.support.constraint.ConstraintLayout>

app/src/main/res/menu/menu.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<menu xmlns:android="http://schemas.android.com/apk/res/android">
22
<item
3-
android:id="@+id/action_first"
3+
android:id="@+id/action_one"
44
android:icon="@drawable/ic_atom"
5-
android:title="First"/>
5+
android:title="One"/>
66
<item
7-
android:id="@+id/action_second"
7+
android:id="@+id/action_two"
88
android:icon="@drawable/ic_disco"
9-
android:title="Second"/>
9+
android:title="Two"/>
10+
<item
11+
android:id="@+id/action_three"
12+
android:icon="@drawable/ic_atom"
13+
android:title="Three"/>
14+
<item
15+
android:id="@+id/action_four"
16+
android:icon="@drawable/ic_disco"
17+
android:title="Four"/>
1018
</menu>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<resources>
22
<string name="app_name">BottomSheet</string>
33
<string name="title">Title</string>
4-
<string name="text_show_menu">Show menu</string>
4+
<string name="text_linear">Linear</string>
5+
<string name="text_grid">Grid</string>
6+
<string name="text_with_title">With title</string>
57
</resources>

sheetmenu/src/main/java/ru/whalemare/sheetmenu/SheetMenu.kt

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package ru.whalemare.sheetmenu
22

33
import android.content.Context
44
import android.support.design.widget.BottomSheetDialog
5+
import android.support.v7.widget.GridLayoutManager
56
import android.support.v7.widget.LinearLayoutManager
67
import android.support.v7.widget.RecyclerView
78
import android.view.LayoutInflater
89
import android.view.MenuItem
910
import android.view.View
1011
import android.widget.TextView
12+
import ru.whalemare.sheetmenu.adapter.MenuAdapter
1113
import ru.whalemare.sheetmenu.extension.inflate
14+
import ru.whalemare.sheetmenu.extension.marginTop
15+
import ru.whalemare.sheetmenu.extension.toList
1216

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

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

3944
BottomSheetDialog(context).apply {
4045
setContentView(root)
46+
processGrid(root)
4147
}.show()
4248
}
4349

50+
private fun processGrid(root: View) {
51+
if (root.findViewById(R.id.text_title).visibility != View.VISIBLE) {
52+
if (layoutManager is GridLayoutManager) {
53+
root.marginTop(24)
54+
}
55+
}
56+
}
57+
4458
open protected fun processTitle(textTitle: TextView) {
4559
if (titleId > 0) {
4660
textTitle.setText(titleId)
@@ -55,11 +69,25 @@ open class SheetMenu(
5569

5670
open protected fun processRecycler(recycler: RecyclerView) {
5771
if (menu > 0) {
58-
recycler.adapter = ListAdapter.with(recycler.context.inflate(menu)).apply {
59-
callback = click
72+
if (layoutManager == null) {
73+
layoutManager = LinearLayoutManager(recycler.context, LinearLayoutManager.VERTICAL, false)
74+
}
75+
76+
var itemLayoutId = R.layout.item_linear
77+
if (layoutManager is GridLayoutManager) {
78+
itemLayoutId = R.layout.item_grid
79+
}
80+
81+
if (adapter == null) {
82+
adapter = MenuAdapter(
83+
menuItems = recycler.context.inflate(menu).toList(),
84+
callback = click,
85+
itemLayoutId = itemLayoutId
86+
)
6087
}
61-
recycler.layoutManager = layoutManager ?:
62-
LinearLayoutManager(recycler.context, LinearLayoutManager.VERTICAL, false)
88+
89+
recycler.adapter = adapter
90+
recycler.layoutManager = layoutManager
6391
}
6492
}
6593

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

81110
fun show() {
82-
SheetMenu(0, title, menu, layoutManager, click).show(context)
111+
SheetMenu(0, title, menu, layoutManager, null, click).show(context)
83112
}
84113

85114
/**
@@ -121,6 +150,15 @@ open class SheetMenu(
121150
this.layoutManager = layoutManager
122151
return this
123152
}
153+
154+
/**
155+
* @param adapter for RecyclerView.
156+
* @see MenuAdapter by default
157+
*/
158+
fun setAdapter(adapter: MenuAdapter): Builder {
159+
this.adapter = adapter
160+
return this
161+
}
124162
}
125163
//endregion
126164
}

sheetmenu/src/main/java/ru/whalemare/sheetmenu/ListAdapter.kt renamed to sheetmenu/src/main/java/ru/whalemare/sheetmenu/adapter/MenuAdapter.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
package ru.whalemare.sheetmenu
1+
package ru.whalemare.sheetmenu.adapter
22

33
import android.support.annotation.LayoutRes
44
import android.support.v7.widget.RecyclerView
55
import android.view.*
66
import android.widget.ImageView
77
import android.widget.TextView
8+
import ru.whalemare.sheetmenu.R
89
import ru.whalemare.sheetmenu.extension.toList
910

1011
/**
1112
* Developed with ❤
1213
* @since 2017
1314
* @author Anton Vlasov - whalemare
1415
*/
15-
open class ListAdapter(var menuItems: List<MenuItem> = emptyList(),
16-
var callback: MenuItem.OnMenuItemClickListener? = null)
17-
: RecyclerView.Adapter<ListAdapter.ViewHolder>() {
16+
open class MenuAdapter(var menuItems: List<MenuItem> = emptyList(),
17+
var callback: MenuItem.OnMenuItemClickListener? = null,
18+
var itemLayoutId: Int = 0)
19+
: RecyclerView.Adapter<MenuAdapter.ViewHolder>() {
1820

1921
companion object {
20-
fun with(menu: Menu): ListAdapter {
21-
return ListAdapter(menu.toList())
22+
fun with(itemLayoutId: Int, menu: Menu): MenuAdapter {
23+
return MenuAdapter(menuItems = menu.toList(), itemLayoutId = itemLayoutId)
2224
}
2325
}
2426

2527
@LayoutRes
2628
open fun getLayoutItem(): Int {
27-
return R.layout.item_sheet_list
29+
return itemLayoutId
2830
}
2931

3032
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

sheetmenu/src/main/java/ru/whalemare/sheetmenu/extension/Extensions.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import android.support.v7.view.SupportMenuInflater
66
import android.support.v7.view.menu.MenuBuilder
77
import android.view.Menu
88
import android.view.MenuItem
9+
import android.view.View
10+
import android.view.ViewGroup
11+
912

1013
/**
1114
* Developed by Magora-Systems.com
@@ -23,4 +26,17 @@ fun Context.inflate(@MenuRes menuRes: Int): Menu {
2326
val menuInflater = SupportMenuInflater(this)
2427
menuInflater.inflate(menuRes, menu)
2528
return menu
26-
}
29+
}
30+
31+
fun View.marginBottom(dp: Int) {
32+
val params = this.layoutParams as ViewGroup.MarginLayoutParams
33+
params.bottomMargin = dp2px(this.context, dp)
34+
}
35+
36+
fun View.marginTop(dp: Int) {
37+
val params = this.layoutParams as ViewGroup.MarginLayoutParams
38+
params.topMargin = dp2px(this.context, dp)
39+
this.layoutParams = params
40+
}
41+
42+
fun dp2px(context: Context, dp: Int): Int = (dp * context.resources.displayMetrics.density + 0.5).toInt()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="wrap_content"
5+
android:layout_height="wrap_content"
6+
android:layout_marginBottom="24dp"
7+
android:layout_marginEnd="24dp"
8+
android:layout_marginStart="24dp"
9+
android:background="?attr/selectableItemBackground"
10+
android:orientation="vertical"
11+
android:padding="8dp">
12+
13+
<ImageView
14+
android:id="@+id/image_icon"
15+
android:layout_width="48dp"
16+
android:layout_height="48dp"
17+
android:layout_gravity="center"
18+
android:padding="8dp"
19+
tools:background="@color/gray_material"/>
20+
21+
<TextView
22+
android:id="@+id/text_title"
23+
style="@style/TextAppearance.AppCompat.Body1"
24+
android:layout_width="wrap_content"
25+
android:layout_height="wrap_content"
26+
android:layout_gravity="center"
27+
android:layout_marginTop="8dp"
28+
android:textSize="16sp"
29+
tools:text="Item name name"/>
30+
31+
</LinearLayout>
File renamed without changes.

0 commit comments

Comments
 (0)