Skip to content

Commit

Permalink
fix: adding clear selected items to multi select sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakariaJawas committed Mar 5, 2022
1 parent 1b8e73d commit 62604e6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class MainActivity : AppCompatActivity() {
multiListSheet.show()
}

btnClearMultiSheet.setOnClickListener {

multiListSheet.clearSelectedItems()
}

}
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnOpenSheet" />

<Button
android:id="@+id/btnClearMultiSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Multiselect"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnOpenMultiSheet" />

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions listbottomsheet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 31
versionCode 11
versionName "1.3.7"
versionCode 12
versionName "1.3.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,34 +242,30 @@ class MultiListBottomSheet<T : Any> private constructor(
txtSearch?.typeface = it
btnAction?.typeface = it
}

}

private fun setSearchable(searchable: Boolean) {

if (!searchable) {
return
}
} //end if

//display the search view
bottomSheetView.findViewById<View>(R.id.searchContainer).visibility = View.VISIBLE

//bind txtSearch watcher listener
bottomSheetView.findViewById<EditText>(R.id.txtSearch).addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(p0: Editable?) {

}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

}

override fun onTextChanged(charSequence: CharSequence?, p1: Int, p2: Int, p3: Int) {

mAdapter?.filter?.filter(charSequence)
}

})

}

override fun show() {
Expand All @@ -285,4 +281,11 @@ class MultiListBottomSheet<T : Any> private constructor(
}, 0)
super.show()
}

fun clearSelectedItems() {
(recyclerView?.adapter as? MultiListAdapter)?.also {
it.selectedItemsList.clear()
it.notifyDataSetChanged()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.zak.listbottomsheet.ListItem
import com.zak.listbottomsheet.R
import java.util.*


class MultiListAdapter(
private val mContext: Context,
var mList: MutableList<ListItem>,
Expand All @@ -30,19 +29,16 @@ class MultiListAdapter(
val selectedItemsList = mutableListOf<ListItem>()
var listFiltered = mList.toMutableList()


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

val view = LayoutInflater.from(mContext).inflate(
layoutResource,
parent,
false
)

return ViewHolder(view, selectedItemColor, defaultItemColor, selectedItemBackgroundColor, defaultItemBackgroundColor)
}



override fun getItemCount(): Int {
return listFiltered.size
}
Expand All @@ -59,7 +55,6 @@ class MultiListAdapter(
}
}


class ViewHolder(itemView: View,private val selectedColor: Int,
private val defaultItemColor: Int,
private val selectedItemBackgroundColor: Int,
Expand All @@ -83,14 +78,11 @@ class MultiListAdapter(
field = value
}


fun setOnClickListener(instance: MultiListAdapter, item: ListItem, listener: (ListItem) -> Unit) {
itemView.setOnClickListener {
if (!instance.selectedItemsList.remove(item)) { //if remove return false means it doesn't exists so add it to the list
Log.d("##item", " will be added")
instance.selectedItemsList.add(item)
} //end if
Log.d("##item", " size ${instance.selectedItemsList.size}")
instance.notifyItemChanged(adapterPosition)
listener(item)
}
Expand All @@ -107,7 +99,6 @@ class MultiListAdapter(
} else {

mList.filter {
Log.d("##charSeq", "search in ${it.title}")
it.title.lowercase(Locale.getDefault()).contains(charSequence.toString()
.lowercase(Locale.getDefault()))
}.toMutableList()
Expand All @@ -122,7 +113,6 @@ class MultiListAdapter(
listFiltered = filterResults?.values as MutableList<ListItem>
notifyDataSetChanged()
}

}
}
}

0 comments on commit 62604e6

Please sign in to comment.