Skip to content

Commit

Permalink
更新多选弹框
Browse files Browse the repository at this point in the history
  • Loading branch information
shaoshuai904 committed Nov 20, 2020
1 parent 7068f8a commit 1060cba
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 20 deletions.
66 changes: 63 additions & 3 deletions app/src/main/java/com/maple/iosdialog/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ class MainActivity : Activity() {
ar1?.show()
}

var ar2: ActionSheetRecyclerMultipleSelectedDialog? = null
var ar2: ActionSheetRecyclerSingleSelectedDialog? = null
fun asrBigDataList(view: View?) {
if (ar2 == null) {
val items = getSheetItemTestData(20)
ar2 = ActionSheetRecyclerMultipleSelectedDialog(this, ActionSheetRecyclerDialog.Config(this).apply {
ar2 = ActionSheetRecyclerSingleSelectedDialog(this, ActionSheetRecyclerDialog.Config(this).apply {
titleTextSizeSp = 18f
closeDraw = ContextCompat.getDrawable(context, android.R.drawable.ic_delete)
isShowMark = true
Expand All @@ -181,6 +181,66 @@ class MainActivity : Activity() {
startActivity(Intent(this, CustomActionSheetRecyclerDialogActivity::class.java))
}


// -------------------------------- Action Sheet Recycler List Dialog ----------------------------------------

var arm1: ActionSheetRecyclerMultipleSelectedDialog? = null
fun multipleList1(view: View?) {
if (arm1 == null) {
val items = getSheetItemTestData(3)
arm1 = ActionSheetRecyclerMultipleSelectedDialog(this, ActionSheetRecyclerDialog.Config(this).apply {
titleTextSizeSp = 18f
isShowMark = true
itemTextSelectedColor = Color.RED
}).apply {
setTitle("多选条目")
addSheetItems(items)
setCancelable(false)
setCanceledOnTouchOutside(false)
setBottomPadding(0f)
setMinScaleHeight(0.45) // 设置最小高度
addSheetItemClickListener { item, position ->
// showToast("$position ${item.getShowName()}")
}
addSheetItemSelectedConfirmListener { list ->
showToast("选中了 ${list.size}")
}
}
}
arm1?.show()
}

var arm2: ActionSheetRecyclerMultipleSelectedDialog? = null
fun multipleList2(view: View?) {
if (arm2 == null) {
val items = getSheetItemTestData(20)
arm2 = ActionSheetRecyclerMultipleSelectedDialog(this, ActionSheetRecyclerDialog.Config(this).apply {
titleTextSizeSp = 18f
isShowMark = true
itemTextSelectedColor = Color.RED
}).apply {
setTitle("多选条目")
addSheetItems(items)
setCancelable(false)
setCanceledOnTouchOutside(false)
setBottomPadding(0f)
setMinScaleHeight(0.45) // 设置最小高度
setMaxScaleHeight(1.0) // 设置最大高度
addSheetItemClickListener { item, position ->
// showToast("$position ${item.getShowName()}")
}
addSheetItemSelectedConfirmListener { list ->
showToast("选中了 ${list.size}")
}
}
}
arm2?.show()
}


fun customMultipleACR(view: View?){

}
// -------------------------------- Action Sheet List Dialog ----------------------------------------
var asl1: ActionSheetListDialog? = null
fun aslList(view: View?) {
Expand Down Expand Up @@ -294,7 +354,7 @@ class MainActivity : Activity() {
val testData = arrayListOf<SheetItem>()
for (index in 1..count) {
val mColor = -0x1000000 or Random().nextInt(0xffffff)
testData.add(SheetItem("single select item $index", mColor))
testData.add(SheetItem("custom sheet view $index", mColor))
}
return testData
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class CustomActionSheetRecyclerDialogActivity : Activity() {
val testData = arrayListOf<SheetItem>()
for (index in 1..count) {
val mColor = -0x1000000 or Random().nextInt(0xffffff)
testData.add(SheetItem("single select item $index", mColor))
testData.add(SheetItem("Custom Sheet View $index", mColor))
}
return testData
}
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
android:text="Action Sheet Recycler List Dialog"
android:textSize="18sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single Selected : " />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -121,6 +126,42 @@
android:textAllCaps="false" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiple Selected : " />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="multipleList1"
android:text="少数据量\n限定最小高度"
android:textAllCaps="false" />

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="multipleList2"
android:text="大数据量\n自适应高度"
android:textAllCaps="false" />

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="customMultipleACR"
android:text="深度\n自定义"
android:textAllCaps="false" />
</LinearLayout>


<TextView
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ abstract class ActionSheetRecyclerDialog(
fun getTitleView() = binding.tvTitle
fun getCloseView() = binding.ivClose
fun getDataView() = binding.rvData
fun getFooterRoot() = binding.llFooter

fun setDialogTitle(title: CharSequence?): ActionSheetRecyclerDialog {
return setTitle(title, isBold = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.maple.msdialog

import android.content.Context
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.maple.msdialog.adapter.SheetMultipleSelectedAdapter
import com.maple.msdialog.databinding.MsLayoutSelectConfirmBinding

/**
* 多选页签List Dialog [ 标题 + 页签条目 + 取消按钮 ]
Expand All @@ -14,24 +18,49 @@ class ActionSheetRecyclerMultipleSelectedDialog(
private val mContext: Context,
private val config: Config = Config(mContext)
) : ActionSheetRecyclerDialog(mContext, config) {
private var onSingleSelectedItemClickListener: OnSheetItemClickListener? = null
private lateinit var confirmView: MsLayoutSelectConfirmBinding
private var onSheetItemClickListener: OnSheetItemClickListener? = null
private var onSheetItemSelectConfirmListener: OnSheetItemSelectConfirmListener? = null
private val adapter by lazy {
SheetMultipleSelectedAdapter(mContext, config).apply {
setOnItemClickListener { item, position ->
// updateItemStatus(position, !item.isSelected)
val newItem = item.apply { isSelected = !isSelected } // 选中状态取反
updateItemStatus(newItem)
onSingleSelectedItemClickListener?.onItemClick(newItem, position)
// dismiss()
val newItem = item.apply { isSelected = !isSelected }
updateItemStatus(newItem)// 选中状态取反
confirmView.cbSelectAll.isChecked = !isSelectAll()// 判断是否已经全选
onSheetItemClickListener?.onItemClick(newItem, position)
}
}
}

init {
confirmView = getSelectConfirmBinding()
getFooterRoot().let {
if (it.childCount > 0) it.removeAllViews()
it.addView(confirmView.root)
}

getDataView().let {
// 添加item分割线
it.addItemDecoration(DividerItemDecoration(
config.dividerPaddingLeft, config.dividerHeight, config.dividerColor,
LinearLayoutManager.VERTICAL, config.skipLastItems))
// 添加滑动监听
it.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (!it.canScrollVertically(1)) {
// Toast.makeText(mContext, "滑动到底部了", Toast.LENGTH_SHORT).show()
}
if (!it.canScrollVertically(-1)) {
// Toast.makeText(mContext, "到顶了", Toast.LENGTH_SHORT).show()
}
}
})
}
}

// 添加动作页签集合
fun addSheetItems(items: List<SheetItem>): ActionSheetRecyclerMultipleSelectedDialog {
getDataView().addItemDecoration(DividerItemDecoration(
config.dividerPaddingLeft, config.dividerHeight, config.dividerColor,
LinearLayoutManager.VERTICAL, config.skipLastItems))
getDataView().adapter = adapter
adapter.refreshData(items)
return this
Expand All @@ -42,15 +71,15 @@ class ActionSheetRecyclerMultipleSelectedDialog(

// 添加条目点击监听
fun addSheetItemClickListener(itemClickListener: OnSheetItemClickListener?): ActionSheetRecyclerMultipleSelectedDialog {
onSingleSelectedItemClickListener = itemClickListener
onSheetItemClickListener = itemClickListener
return this
}

// // 设置当前选中条目
// fun setSelectedIndex(index: Int): ActionSheetRecyclerMultipleSelectedDialog {
// adapter.updateSelectItem(index)
// return this
// }
// 添加条目选择确认监听
fun addSheetItemSelectedConfirmListener(selectConfirmListener: OnSheetItemSelectConfirmListener?): ActionSheetRecyclerMultipleSelectedDialog {
onSheetItemSelectConfirmListener = selectConfirmListener
return this
}

// 是否显示item选中标记
fun isShowItemMark(isShow: Boolean): ActionSheetRecyclerMultipleSelectedDialog {
Expand All @@ -59,5 +88,21 @@ class ActionSheetRecyclerMultipleSelectedDialog(
return this
}

// 底部确认选择View
private fun getSelectConfirmBinding(): MsLayoutSelectConfirmBinding {
val binding: MsLayoutSelectConfirmBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext),
R.layout.ms_layout_select_confirm, null, false)
return binding.apply {
cbSelectAll.setOnClickListener {
// cbSelectAll.text = if (cbSelectAll.isChecked) "取消全选" else "全选"
adapter.isSelectAll(cbSelectAll.isChecked)
}
tvConfirm.setOnClickListener {
val currentSelectList = adapter.getCurrentSelectedItemList()
onSheetItemSelectConfirmListener?.onSelectConfirm(currentSelectList)
dismiss()
}
}
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.maple.msdialog;

/**
* Sheet item 点击监听
* @author : shaoshuai27
* @date :2020/11/20
*/
public interface OnSheetItemClickListener {

void onItemClick(SheetItem item, int position);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.maple.msdialog;

import java.util.List;

/**
* 选择确认监听
*
* @author : shaoshuai
* @date :2020/11/20
*/
public interface OnSheetItemSelectConfirmListener {

void onSelectConfirm(List<SheetItem> selectedList);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class SheetMultipleSelectedAdapter(
val config: ActionSheetRecyclerDialog.Config
) : BaseQuickAdapter<SheetItem, RecyclerView.ViewHolder>() {

// 是否选择全部
fun isSelectAll(isSelected: Boolean) {
data.forEach { it.isSelected = isSelected }
notifyDataSetChanged()
}

// 是否已经选择所有
fun isSelectAll(): Boolean = data.any { !it.isSelected }

// 更新item选中状态
fun updateItemStatus(item: SheetItem) {
data.filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:paddingBottom="20dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="4"
tools:listitem="@layout/ms_item_sheet" />

<LinearLayout
android:id="@+id/ll_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />

</LinearLayout>
</layout>
Loading

0 comments on commit 1060cba

Please sign in to comment.