【发布时间】:2019-05-22 22:43:26
【问题描述】:
我正在构建一个应用程序,用户需要填写一些数据才能发布内容,因此片段由EditText、单选按钮和Spinner 以及动态呈现多个子布局的RecyclerView 组成包含TextView 和EditText。
因此,当用户从Spinner 中选择类别时,与该类别相关的一些属性会显示在RecyclerView 中,用户可以选择填充其中的一些。
我尝试使用回调和TextWatcher 来实现此功能,但我没有得到我想要的值。
回调
interface PropertiesCallback {
fun addProp(position: Int, title: String, value: String)
}
适配器
class PropertiesAdapter(private val propertiesCallback: PropertiesCallback)
: RecyclerView.Adapter<PropertiesAdapter.ViewHolder>() {
private var list = listOf<CategoriesAndSubcategoriesQuery.Property>()
fun setData(listOfProps: List<CategoriesAndSubcategoriesQuery.Property>) {
this.list = listOfProps
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.z_property_input, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int = list.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(list[position], position)
}
inner class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
private val label: TextView = view.findViewById(R.id.label)
private val input: EditText = view.findViewById(R.id.input)
fun bind(prop: CategoriesAndSubcategoriesQuery.Property, position: Int) {
label.text = prop.title()
prop.hint()?.let { input.hint = prop.hint() }
input.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
propertiesCallback.addProp(position, prop.title(), input.text.toString())
}
})
}
}
}
在片段中
private var propertiesList = mutableListOf<CategoriesAndSubcategoriesQuery.Property>()
private var propertiesInputList = mutableListOf<ProductPropertiesInput>()
private fun setUpSubcategorySpinner() {
subcategoriesAdapter = ArrayAdapter(
this@AddProductFragment.context!!,
android.R.layout.simple_spinner_item,
subcategoriesList
)
//Subcategories
subcategoriesAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line)
subcategory_spinner.adapter = subcategoriesAdapter
subcategory_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
subcategoryId = subcategoriesList[position].id()
//Adding properties
subcategoriesList[position].properties()?.let {
//Clear previous properties data of another subcategory.
propertiesInputList.clear()
propertiesList.clear()
propertiesList.addAll(it)
propertiesAdapter.setData(propertiesList)
propertiesAdapter.notifyDataSetChanged()
}
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
}
覆盖
override fun addProp(position: Int, title: String, value: String) {
val prop = ProductPropertiesInput
.builder()
.title(title)
.value(value)
.build()
propertiesInputList.add(prop)
//Log.d(TAG, "prop: ${prop.title()} : ${prop.value()}")
}
提交乐趣
private fun submitProduct() {
//Initializing properties.
val properties: Any
//The keys needed in final list.
val propertyKeys = propertiesList.map { it.title() }
//Removing objects which keys are not needed.
propertiesInputList.removeAll { it.title() !in propertyKeys }
Log.d(TAG, "propertiesInputList: $propertiesInputList")
//Removing duplicate and assign result in properties var.
properties = propertiesInputList
.distinctBy { it.title() }
Log.d(TAG, "properties: $properties")
for (prop in properties) {
Log.d(TAG, "properties , title: ${prop.title()}, value: ${prop.value()} ")
}
}
以上代码旨在用作。当用户在RecyclerView 中的EditText 之一中键入一个值时,该值将被分片并添加到具有标题和值的对象中,然后添加到propertiesInputList。
问题 1: propertiesInputList 会有很多具有相同标题的重复对象,我认为最好的解决方案是使用 distinctBy。
问题 2: 当用户填写一些与假设category1 相关的EditText 并改变主意并从Spinner 中选择另一个类别时。 不属于新选择的类别的先前值仍保留在propertiesInputList 列表中。所以我认为最好的解决方案是清除propertiesInputList 并使用removeAll 与类别相关的标题来过滤不需要的对象。
但现在我只得到用户类型的第一个字母。如果用户输入 shoes,我会得到 s。所以似乎distinctBy 返回了第一个对象,但我想得到用户输入的最后一个字,如果用户输入并删除了我想要空白的所有内容。
有没有更好的解决方案来处理这个问题?就像循环recyclerView 仅当用户按下提交而不是TextWatcher 时?或者我应该修复哪个部分来完成这项工作?
【问题讨论】:
标签: android kotlin android-recyclerview