修改自定义RecyclerView的项目

时间:2018-06-19 09:57:21

标签: java android android-recyclerview android-custom-view

我正在尝试创建自定义<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <%@ Page Language="C#" %> <%@ Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"> <head runat="server"> <meta name="WebPartPageExpansion" content="full" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery validate</title> <meta http-equiv="X-UA-Compatible" content="IE=10" /> <SharePoint:CssRegistration Name="default" runat="server"/> <!--[if gte mso 9]> <SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,WikiField,_dlc_DocId,_dlc_DocIdUrl,_dlc_DocIdPersistId"><xml> <mso:CustomDocumentProperties> <mso:_dlc_DocId msdt:dt="string">5JSTYM4NXMKN-1550736763-50</mso:_dlc_DocId> <mso:_dlc_DocIdItemGuid msdt:dt="string">56166d47-3111-4d9e-86c4-34fc0308c55f</mso:_dlc_DocIdItemGuid> <mso:_dlc_DocIdUrl msdt:dt="string">http://sp2013/sites/team/_layouts/15/DocIdRedir.aspx?ID=5JSTYM4NXMKN-1550736763-50, 5JSTYM4NXMKN-1550736763-50</mso:_dlc_DocIdUrl> </mso:CustomDocumentProperties> </xml></SharePoint:CTFieldRefs><![endif]--> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/> <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script> <script type="text/javascript"> $(function(){ $.validator.setDefaults({ submitHandler: function () { alert( "Submitted!" ); } }); $("#signupForm1").validate( { rules: { ACI_client_name_input: "required" }, messages: { ACI_client_name_input: "Please enter the Client name", }, /* THIS PART WAS PUT IN FROM GUIDE ONLINE FOR SHAREPOINT SITES */ //if ($("input[title='ACI_client-name-input']").attr("name") == undefined) { // $("input[title='ACI_client-name-input']").attr("name", $("input[title='ACI_client-name-input']").attr("id")); //} errorElement: "em", errorPlacement: function ( error, element ) { // Add the `help-block` class to the error element error.addClass( "help-block" ); // Add `has-feedback` class to the parent div.form-group // in order to add icons to inputs element.parents( ".col-sm-5" ).addClass( "has-feedback" ); if ( element.prop( "type" ) === "checkbox" ) { error.insertAfter( element.parent( "label" ) ); } else { error.insertAfter( element ); } // Add the span element, if doesn't exists, and apply the icon classes to it. if ( !element.next( "span" )[ 0 ] ) { $( "<span class='glyphicon glyphicon-remove form-control-feedback'></span>" ).insertAfter( element ); } }, success: function ( label, element ) { // Add the span element, if doesn't exists, and apply the icon classes to it. if ( !$( element ).next( "span" )[ 0 ] ) { $( "<span class='glyphicon glyphicon-ok form-control-feedback'></span>" ).insertAfter( $( element ) ); } }, highlight: function ( element, errorClass, validClass ) { $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" ); $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" ); }, unhighlight: function ( element, errorClass, validClass ) { $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" ); $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" ); } }); }); </script> </head> <body> <form id="signupForm1" method="post" class="form-horizontal" action novalidate="novalidate"> <div class="form-group row" style="margin-bottom: 15px;"> <label class="col-lg-10 control-label" for="ACI_client-name-input">Client Name</label> <div class="col-lg-8 required-after"> <input type="text" class="form-control" id="ACI_client_name_input" name="ACI_client_name_input" placeholder="Ex: FleishmanHillard" required/> <span class="">*</span> </div> </div> <div class="form-group row col-lg-8"> <button type="submit" name="singlebutton" class="btn btn-success" id="submit">Submit</button> <button type="reset" name="cancelbutton" class="btn btn-warning" id="cancel" onclick="window.location.href='//fh126cloud.sharepoint.com/emplsrv/missupport/pages/MailChimpIntake.aspx'">Cancel</button> </div> </form> </body> </html>

首先,我只想旋转附加到RecyclerView的列表的第一个元素。

以下是我尝试通过覆盖recyclerView方法做的事情:

setAdapter

但每次我尝试更改列表中的元素时都会收到此错误:

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.view.View.setRotation(float)'

我希望逐个获取这个RecyclerView项目来修改它们。是否可以到达我的RecyclerView的每个项目以添加它们的修改?知道我实际上正在创建自定义视图扩展RecyclerView 而不是 RecyclerView适配器

1 个答案:

答案 0 :(得分:1)

应用崩溃是因为您尝试访问不存在的视图。当您调用setAdapter时,它只会创建适配器对象并将其设置为RecyclerView。那时孩子还没有被创建,所以getChildAt(0)返回null并且你得到了崩溃。

相反,您应该使用适配器进行项目视图操作。在您的适配器中覆盖onBindViewHolder方法并在那里执行。每次将项目视图绑定到RecyclerView时,都会调用该方法。它有一个位置参数,可用于仅旋转列表中的第一个项目。

此处有更多信息:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onBindViewHolder(VH,%20int,%20java.util.List%3Cjava.lang.Object%3E)