我认为没有必要重复描述我的RecyclerView是如何设置的。这在这里描述:Android - RecyclerView with various dynamic content, different for each item
简而言之,我的回收器视图可以包含各种控件,如编辑文本、微调器等。然后,回收器视图被放置在Activity布局中,始终以嵌套滚动视图作为其父视图展开。当用户点击“我的活动”中的“确认”按钮时,回收者视图的内容应该被验证-例如,如果任何特定的项目包含需要为非空的文本视图,则活动不应该结束,并且应该显示该特定编辑文本内的标准错误标记。
在我的回收器视图中,我创建了validate方法,它应该返回true或false,这样我就可以知道验证结果。但是,我不知道如何从适配器访问特定recyclerview项目内容,这是我的方法(在本例中,编辑类型设置为电子邮件的文本控件):
public boolean validate()
{
for(Object item : items)
{
Parameter p = (Parameter)item;
switch (p.type)
{
case Parameter.EMAIL_PARAM:
{
String email = ((EmailParameter)p).value;
/*validate email here, if invalid, return
false and set error indicatior for corresponding
recyclerview item*/
}
}
}
return true;
}items是List<Object>,它包含所有具有各种实际类型的回收器视图项目,每种类型都决定了呈现的特定回收器内容。当用户更改特定项的控件时,items中的相应项将分别更新,因此当调用validate时,所有项都包含新数据(对于EmailParameter,它将是已经设置为在编辑文本控件中键入的文本的value字段)。
因为我可以很容易地验证最终值,我不知道如何更新相应的回收器视图项目控件以显示错误。
你有什么想法吗?
发布于 2019-05-09 19:09:13
工作解决方案-使用notifyDatasetChanged
public boolean validate()
{
boolean allValid = true;
for(Object item : items)
{
Parameter p = (Parameter)item;
switch (p.type)
{
case Parameter.EMAIL_PARAM:
{
String email = ((EmailParameter)p).value;
/*validate email here, if invalid, return
false and set error indicatior for corresponding
recyclerview item*/
p.errorMessage = valid ? null:"Email is incorrect";
allValid &= valid;
break;
}
/*All remaining parameter types validaion*/
}
}
if(!allValid) this.notifyDataSetChanged();
return allValid;
}发布于 2019-05-09 16:23:12
也许这个库就是你所需要的https://github.com/Link184/KidAdapter
下面是一个如何验证数据的简短示例:
val adapter : TypedKidAdapter = recyclerView.setUp {
withViewType {
withItems(mutableListOf("one", "two"))
withLayoutResId(android.R.layout.list_content)
withContentComparator<String> { oldObject, newObject ->
insertNewViewType(newItems)
oldObject.compareTo(newObject)
}
withItemsComparator<String> { oldObject, newObject ->
oldObject.equals(newObject).also {
updateItemsFromViewType(someNewItems)
}
}
bind<String> {
//ui logic
}
}
withViewType {
//another viewtype config
}
}
fun insertNewViewType(newItems: MutableList<Any>) {
adapter restructure {
insertTop {
withItems(newItems)
withLayoutResId(android.R.layout.list_content)
withItemsComparator<Any> {
...
}
bind<Any> {
//UI logic
}
}
}
}
fun updateItemsFromViewType(newItems: String) {
adapter update {
insertBottom(newItems)
}
}https://stackoverflow.com/questions/56054259
复制相似问题