feat(自定义控件): 增加自定义控件功能

增加CustomTitleBar的右侧图标点击功能
This commit is contained in:
糕小菜 2024-10-16 14:01:39 +08:00
parent 0885a41003
commit 85e9a4c487
5 changed files with 208 additions and 2 deletions

View File

@ -0,0 +1,20 @@
package com.kaixed.kchat.model.setting
/**
* @Author: kaixed
* @Date: 2024/10/8 13:53
* @Description: TODO
*/
sealed class SettingItem {
data class NormalItem(
val name: String,
val group: String,
) : SettingItem()
data class FunctionItem(
val itemName: String,
val remark: String,
) : SettingItem()
data class TitleItem(val title: String) : SettingItem()
}

View File

@ -0,0 +1,54 @@
package com.kaixed.kchat.view.activity
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.kaixed.kchat.databinding.ActivitySettingBinding
import com.kaixed.kchat.model.setting.SettingItem
import com.kaixed.kchat.view.adapter.SettingListAdapter
class SettingActivity : AppCompatActivity() {
private lateinit var binding: ActivitySettingBinding
private lateinit var settingItems: MutableList<SettingItem>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySettingBinding.inflate(layoutInflater)
enableEdgeToEdge()
setContentView(binding.root)
settingItems = getSettingItems()
binding.rvSetting.layoutManager =
LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
binding.rvSetting.adapter = SettingListAdapter(settingItems)
}
private fun getSettingItems(): MutableList<SettingItem> {
return mutableListOf(
SettingItem.NormalItem("账号与安全", "安全"),
SettingItem.TitleItem(""),
SettingItem.NormalItem("青少年模式", "模式"),
SettingItem.NormalItem("关怀模式", "模式"),
SettingItem.TitleItem(""),
SettingItem.NormalItem("新消息通知", ""),
SettingItem.NormalItem("聊天", ""),
SettingItem.NormalItem("设备", ""),
SettingItem.NormalItem("通用", ""),
SettingItem.TitleItem("隐私"),
SettingItem.NormalItem("朋友权限", "隐私"),
SettingItem.NormalItem("个人信息与权限", "隐私"),
SettingItem.NormalItem("个人信息收集清单", "隐私"),
SettingItem.NormalItem("第三方信息共享清单", "隐私"),
SettingItem.TitleItem(""),
SettingItem.NormalItem("插件", "插件"),
SettingItem.TitleItem(""),
SettingItem.NormalItem("关于", "帮助"),
SettingItem.NormalItem("帮助与反馈", "帮助"),
SettingItem.TitleItem(""),
SettingItem.NormalItem("切换账户", "账户"),
SettingItem.NormalItem("退出", "账户")
)
}
}

View File

@ -0,0 +1,100 @@
package com.kaixed.kchat.view.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.View.VISIBLE
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.kaixed.kchat.databinding.ItemSettingFunctionBinding
import com.kaixed.kchat.databinding.ItemSettingNormalBinding
import com.kaixed.kchat.databinding.ItemSettingTitleBinding
import com.kaixed.kchat.model.setting.SettingItem
import com.kaixed.kchat.utils.Constants.MESSAGE_TYPE_FUNCTION
import com.kaixed.kchat.utils.Constants.MESSAGE_TYPE_NORMAL
import com.kaixed.kchat.utils.Constants.MESSAGE_TYPE_TITLE
/**
* @Author: kaixed
* @Date: 2024/10/8 13:53
* @Description: TODO
*/
class SettingListAdapter(private val items: MutableList<SettingItem>) :
RecyclerView.Adapter<ViewHolder>() {
class NormalViewHolder(val binding: ItemSettingNormalBinding) : ViewHolder(binding.root)
class TitleViewHolder(val binding: ItemSettingTitleBinding) : ViewHolder(binding.root)
class FunctionViewHolder(val binding: ItemSettingFunctionBinding) : ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return when (viewType) {
MESSAGE_TYPE_TITLE -> {
val binding = ItemSettingTitleBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
TitleViewHolder(binding)
}
MESSAGE_TYPE_NORMAL -> {
val binding = ItemSettingNormalBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
NormalViewHolder(binding)
}
MESSAGE_TYPE_FUNCTION -> {
val binding = ItemSettingFunctionBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
FunctionViewHolder(binding)
}
else -> throw IllegalArgumentException("Invalid view type")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is TitleViewHolder -> bindTitleViewHolder(holder, position)
is NormalViewHolder -> bindNormalViewHolder(holder, position)
is FunctionViewHolder -> bindFunctionViewHolder(holder, position)
}
}
private fun bindTitleViewHolder(holder: TitleViewHolder, position: Int) {
val item = items[position] as SettingItem.TitleItem
holder.binding.tvItemName.text = item.title
holder.binding.tvItemName.visibility = if (item.title.isEmpty()) View.GONE else VISIBLE
holder.binding.view.visibility = if (item.title.isEmpty()) VISIBLE else View.GONE
}
private fun bindNormalViewHolder(holder: NormalViewHolder, position: Int) {
val item = items[position] as SettingItem.NormalItem
holder.binding.tvItemName.text = item.name
holder.binding.viewItemDecoration.visibility =
if (position != items.size - 1 && items[position + 1] is SettingItem.TitleItem) View.GONE else VISIBLE
}
private fun bindFunctionViewHolder(holder: FunctionViewHolder, position: Int) {
}
override fun getItemViewType(position: Int): Int {
return when (items[position]) {
is SettingItem.TitleItem -> MESSAGE_TYPE_TITLE
is SettingItem.NormalItem -> MESSAGE_TYPE_NORMAL
is SettingItem.FunctionItem -> MESSAGE_TYPE_FUNCTION
}
}
override fun getItemCount(): Int = items.size
}

View File

@ -0,0 +1,31 @@
package com.kaixed.kchat.view.fragment
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceFragmentCompat
import com.kaixed.kchat.R
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
val view = super.onCreateView(inflater, container, savedInstanceState)
view.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.white))
return view
}
}

View File

@ -51,9 +51,10 @@ class CustomTitleBar @JvmOverloads constructor(
binding.ivBack.setOnClickListener {
(context as? Activity)?.finish()
}
}
fun setOnSettingClickListener(listener: OnClickListener?) {
binding.ivSetting.setOnClickListener(listener)
}
}