【问题标题】:Validate RecyclerView content验证 RecyclerView 内容
【发布时间】:2019-05-09 07:28:19
【问题描述】:

我认为没有必要重复我的 RecyclerView 设置的确切描述。此处对此进行了描述:Android - RecyclerView with various dynamic content, different for each item

简而言之,我的 Recycler View 可能包含各种控件,如编辑文本、微调器等。然后,recyclerview 放置在 Activity 布局中,始终以嵌套滚动视图作为其父级展开。当用户在我的活动中点击“确认”按钮时,应验证回收站视图的内容 - 例如,如果任何特定项目包含需要为非空的文本视图,则活动不应完成,并且该特定编辑文本内的标准错误标记应显示.

在我的回收站视图中,我创建了validate 方法,它应该返回truefalse,所以我会知道验证结果。但是,我不知道如何从适配器访问特定 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;
    }

itemsList<Object>,它包含所有具有各种实际类型的recyclerview 项目,每种类型决定了呈现的特定recyclerview 内容。当用户更改特定项目的控件时,将分别更新来自items 的相应项目,因此当调用validate 时,所有项目都包含新数据(如果是EmailParameter,它将是value 字段已设置为文本类型在编辑文本控件中)。

由于我可以轻松验证最终值,我不知道如何更新相应的回收站视图项控件以显示错误。

你有什么想法吗?

【问题讨论】:

  • 发布您的 Parameter 类,如果它是您创建的自定义类,我将假设您有一个与存储在类中的参数相关联的唯一 ID。我建议在类中创建一个新的公共字段来设置错误并通过调用 notifyItemChanged / notifyDataSetChanged() 将其显示在 recyclerview 中的各个项目上。
  • 是的,实际上我就是这样解决的。

标签: android validation android-recyclerview android-viewholder


【解决方案1】:

工作解决方案 - 使用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;
    }

【讨论】:

    【解决方案2】:

    也许你需要那个库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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-17
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 2018-12-04
      • 2018-05-16
      相关资源
      最近更新 更多