Skip to content

Commit

Permalink
Adding selected item indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakariaJawas committed Apr 12, 2020
1 parent 4f94a17 commit e1f0287
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 18 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/com/zak/listbottomsheetproject/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.zak.listbottomsheet.ListBottomSheet
import com.zak.listbottomsheet.NameField
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

Expand All @@ -16,21 +17,27 @@ class MainActivity : AppCompatActivity() {
val listSheet = ListBottomSheet.Builder<Category>(this)
.list(mList)
.title("Choose one")
// .itemLayout(R.layout.custom_list_item) //to set a custom layout for list items instead of the default one
// .itemLayout(R.layout.custom_list_item) //to set a custom layout for list items instead of the default one
.onChooseItemCallback { sheet: ListBottomSheet<Category>, category: Category, position: Int ->
sheet.dismiss() //hide the dialog
Log.d("##chosen_cat", category.name)
Log.d("##chosen_cat", "chosen category is ${category.name} at position $position")
}
.cancelable(false) //prevent the sheet from being canceled by clicking outside the sheet
.cancelButtonVisible(true) //show the cancel button
.searchable(true)
// .selectedItemColor(ContextCompat.getColor(this, R.color.colorAccent)) //set the selected item color
// .selectedItemIndex(0) //initial the list with selected item
.build()

// listSheet.titleAlignment = Gravity.RIGHT //to change title text alignment
// listSheet.titleSize = 18F //to change title text size
// listSheet.titleColor = ContextCompat.getColor(this, R.color.colorPrimary) //to change title text color
// listSheet.selectedItemIndex = 1 //to change the selected item after you build the sheet

listSheet.show()
btnOpenSheet.setOnClickListener {

listSheet.show()
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />


<Button
android:id="@+id/btnOpenSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Sheet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:3.6.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.zak.listbottomsheet

import android.content.Context
import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
Expand All @@ -14,7 +14,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.zak.listbottomsheet.adapter.ListAdapter
import kotlinx.android.synthetic.main.bottom_sheet_list_layout.view.*

/**
* ListBottomSheet class
Expand All @@ -25,6 +24,7 @@ import kotlinx.android.synthetic.main.bottom_sheet_list_layout.view.*
* @property titleColor the color of the title
* @property titleSize the size of the title
* @property titleAlignment the alignment of the sheet title
* @property selectedItemIndex the current selected item of the list
*/
class ListBottomSheet<T : Any> private constructor(
private val mContext: Context,
Expand All @@ -34,7 +34,9 @@ class ListBottomSheet<T : Any> private constructor(
private val cancelable: Boolean = true,
private var cancelButtonVisibility: Boolean = false,
private val searchable: Boolean = false,
private val onChooseItem: ((ListBottomSheet<T>, T, Int) -> Unit)?
private var _selectedItemIndex: Int,
private val onChooseItem: ((ListBottomSheet<T>, T, Int) -> Unit)?,
private val selectedItemColor: Int
) : BottomSheetDialog(mContext) {

class Builder<T : Any>(private val mContext: Context) {
Expand All @@ -47,6 +49,8 @@ class ListBottomSheet<T : Any> private constructor(
private var cancelable: Boolean = true
private var cancelButtonVisibility: Boolean = false
private var searchable: Boolean = false
private var selectedItemIndex: Int = -1 //no selected item as deafult
private var selectedItemColor: Int = Color.BLACK //black color

fun title(title: String) = apply { this.title = title }
fun list(mList: List<T>) = apply { this.mList = mList }
Expand All @@ -56,8 +60,24 @@ class ListBottomSheet<T : Any> private constructor(
fun cancelable(cancelable: Boolean) = apply { this.cancelable = cancelable }
fun cancelButtonVisible(cancelButtonVisibility: Boolean) = apply { this.cancelButtonVisibility = cancelButtonVisibility }
fun searchable(searchable: Boolean) = apply { this.searchable = searchable }

fun build() = ListBottomSheet(mContext, title, mList, layoutResource, cancelable, cancelButtonVisibility,searchable, onChooseItem)
fun selectedItemIndex(selectedItemIndex: Int) =
apply { this.selectedItemIndex = selectedItemIndex }

fun selectedItemColor(selectedItemColor: Int) =
apply { this.selectedItemColor = selectedItemColor }

fun build() = ListBottomSheet(
mContext,
title,
mList,
layoutResource,
cancelable,
cancelButtonVisibility,
searchable,
selectedItemIndex,
onChooseItem,
selectedItemColor
)
}

private var mAdapter: ListAdapter? = null
Expand Down Expand Up @@ -89,6 +109,13 @@ class ListBottomSheet<T : Any> private constructor(
field = value
}

var selectedItemIndex: Int = _selectedItemIndex
set(value) {
mAdapter?.selectedItem = value
field = value
}


init {

bottomSheetView.findViewById<TextView>(R.id.lblTitle).text = _title
Expand Down Expand Up @@ -127,10 +154,21 @@ class ListBottomSheet<T : Any> private constructor(
}//end if


val defaultItemColor = LayoutInflater.from(context).inflate(layoutResource, null)
.findViewById<TextView>(R.id.textView).currentTextColor

//fill the adapter
mAdapter = ListAdapter(mContext, valuesList.toMutableList(), layoutResource) { item ->
mAdapter = ListAdapter(
mContext,
valuesList.toMutableList(),
layoutResource,
defaultItemColor,
selectedItemColor
) { item ->

onChooseItem?.also {
selectedItemIndex = item.position
setSelectedItem()
it(this, mList[item.position], item.position)
}
}
Expand All @@ -139,6 +177,12 @@ class ListBottomSheet<T : Any> private constructor(
recyclerView?.layoutManager = LinearLayoutManager(mContext)
recyclerView?.setHasFixedSize(true)
recyclerView?.adapter = mAdapter

setSelectedItem()
}

private fun setSelectedItem() {
mAdapter?.selectedItem = this.selectedItemIndex
}

private fun setSearchable(searchable: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.zak.listbottomsheet

data class ListItem(val position: Int, val title: String)
data class ListItem(val position: Int, val title: String, var isSelected: Boolean = false)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zak.listbottomsheet.adapter

import android.content.Context
import android.graphics.Typeface
import android.util.Log
import android.view.LayoutInflater
import android.view.View
Expand All @@ -16,10 +17,31 @@ class ListAdapter(
private val mContext: Context,
var mList: MutableList<ListItem>,
private val layoutResource: Int,
private val defaultItemColor: Int,
private val selectedItemColor: Int,
private val onChooseItem: (ListItem) -> Unit
) :
RecyclerView.Adapter<ListAdapter.ViewHolder>(), Filterable{


var oldSelectedItem = -1
var selectedItem: Int = -1
set(value) {

if (value == -1 || value >= mList.size) {
return
}

if (oldSelectedItem != -1) {
mList[oldSelectedItem].isSelected = false
notifyItemChanged(oldSelectedItem)
} //end if

mList[value].isSelected = true
oldSelectedItem = value
notifyItemChanged(value)
}

var listFiltered = mList.toMutableList()


Expand All @@ -43,6 +65,16 @@ class ListAdapter(
val item = listFiltered[position]
holder.setOnClickListener(item, onChooseItem)
holder.lblName.text = item.title

if (item.isSelected) {

holder.lblName.setTextColor(selectedItemColor)
holder.lblName.setTypeface(holder.lblName.typeface, Typeface.BOLD)
} else {

holder.lblName.setTextColor(defaultItemColor)
holder.lblName.typeface = Typeface.DEFAULT
}
}


Expand Down
10 changes: 10 additions & 0 deletions listbottomsheet/src/main/res/drawable/rounded_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">


<stroke
android:width="1dp"
android:color="#666" />
<corners android:radius="4dp" />
</shape>
15 changes: 9 additions & 6 deletions listbottomsheet/src/main/res/layout/bottom_sheet_list_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools"
android:focusableInTouchMode="true">


<androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -36,8 +37,8 @@
android:src="@drawable/ic_action_cancel"
android:clickable="true"
android:focusable="true"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:visibility="visible"
android:background="?attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -51,10 +52,12 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/searchContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_height="40dp"
app:layout_constraintTop_toBottomOf="@+id/titleContainer"
android:visibility="gone"
android:padding="8dp">
android:layout_margin="4dp"
android:background="@drawable/rounded_background"
android:padding="4dp"
android:visibility="visible">

<ImageView
android:id="@+id/imageView"
Expand Down

0 comments on commit e1f0287

Please sign in to comment.