<span id="Label3"></p><p><p><em>"a flexible view for providing a limited window to a large data set."</em></p></p><p><p>Can be said to be an upgrade of the listview, Listvie we need to write their own viewholder, of course, you can not write, is in recylerview, is to let write Yo ~recyclerview for the inability to display the same format data in a screen range, It needs to be displayed in multiple rows or columns. such as showing contacts, pictures, videos and so On. Users need to swipe the screen to see the data, and then the Recyclerview feature is There. For example, when the user slides the current visible item off the screen, the Item's view will be reclaimed and reused after a new item enters the viewable Range. Recyclable view is a useful feature that not only reduces the overhead of the CPU constantly inflate view, but also saves the memory overhead of the cached VIEW.</p></p><p><p>Recylerview also has a big feature, is animation!</p></p><strong><strong>Recyclerview no longer responsible for displaying work</strong></strong><p><p>Unlike the listview, Recyclerview is no longer responsible for display functions such as item Placement. All and layout, drawing and other aspects of the work are split into different classes to Manage. So developers can customize a variety of functional classes that meet custom Requirements.</p></p> <table style="width: 780px;" border="3" cellspacing="0" cellpadding="2"> <tbody> <tr> <td valign="top" width="326"><p>Recyclerview.adapter</p></td> <td valign="top" width="454">Managed data collection, Creating a view for each item</td> </tr> <tr> <td valign="top" width="326">Recyclerview.viewholder</td> <td valign="top" width="454">A child view that hosts the item view</td> </tr> <tr> <td valign="top" width="326">Recyclerview.layoutmanager</td> <td valign="top" width="454">Responsible for the layout of the item view</td> </tr> <tr> <td valign="top" width="326">Recyclerview.itemdecoration</td> <td valign="top" width="454">Adds a sub-view for each item view that is used to draw divider in the demo</td> </tr> <tr> <td valign="top" width="326">Recyclerview.itemanimator</td> <td valign="top" width="454">Animations that are responsible for adding and deleting data</td> </tr> </tbody> </table>Viewholder<p><p>About Viewholder,google has long been recommended for developers, but it is only recommended. But now, Recyclerview.adapter ultimately requires developers to use Viewholder.</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #0000ff;">extends</span> <span style="color: #000000;">viewholder{ </span>public<span style="color: #0000ff;"></span> <span style="color: #000000;"> ImageView iv; </span> <span style="color: #0000ff;"></span> public <span style="color: #000000;">TextView tv; </span> <span style="color: #0000ff;"></span> public <span style="color: #000000;">myviewholder (View rootview) { </span><span style="color: #0000ff;">Super</span><span style="color: #000000;">(rootview); </span> =<span style="color: #000000;"> (ImageView) Rootview.findviewbyid (r.id.item_iv); </span> =<span style="color: #000000;"> (TextView) Rootview.findviewbyid (r.id.item_tv); } }</span></pre></pre>Recyclerview.adapter<p><p>Adapter is responsible for playing two roles: not only supporting the bottom data, but also creating the right view for the Data. Adapter applies to many controls on android, such as listview, autocompletetextview, and so On.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Myadapter<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Adapter<myviewholder><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>List<item><span style="color: #000000;"><span style="color: #000000;">mdata; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Myadapter (list<item><span style="color: #000000;"><span style="color: #000000;">Data) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Mdata =<span style="color: #000000;"><span style="color: #000000;">data; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">getitemcount () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">mdata.size (); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onbindviewholder (myviewholder holder,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Position) {Item Bean</span></span>=<span style="color: #000000;"><span style="color: #000000;">Mdata.get (position); Holder.tv.setText (bean.tv); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Myviewholder Oncreateviewholder (viewgroup parent,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Viewtype) {View Itemview</span></span>= Layoutinflater.from (parent.getcontext ()). Inflate (r.layout.item, parent,<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">); Myviewholder VH</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Myviewholder (itemview); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">vh; }</span></span></pre><pre><pre><span style="color: #000000;">}</span></pre></pre><p><p><span style="color: #ff0000;">Oncreateviewholder is responsible for creating a view for item, Onbindviewholder is responsible for binding the data to the view of Item.</span></p></p><strong><strong>Recyclerview.layoutmanager</strong></strong><p><p>LayoutManager is the most interesting class in RECYCLERVIEW. This class is responsible for the layout of each item view in RECYLERVIEW. Google now offers a subclass of Layoutmanager: Linearlayoutmanager. The Linearlayoutmanager provides both horizontal and vertical layouts.</p></p><pre><pre><span style="color: #0000ff;">New</span> Mylayoutmanager (<span style="color: #0000ff;"></span>this<span style="color: #000000;">); Manager.setorientation (linearlayout.horizontal);</span> <span style="color: #008000;">//</span> <span style="color: #008000;">default is linearlayout.vertical </span> Mrecyclerview.setlayoutmanager (manager);</pre></pre><p><p>Linearlayoutmanager provides several ways to help developers get the top item on the screen and the bottom item:</p></p> <ul> <li><li>Findfirstvisibleitemposition ()</li></li> <li><li>Findfirstcompletelyvisibleitemposition ()</li></li> <li><li>Findlastvisibleitemposition ()</li></li> <li><p><p>Findlastcompletelyvisibleitemposition ()</p></p><strong><strong>recyclerview.itemdecoration</strong></strong><p><p>Through the itemdecoration can make each item visually separate from each other, <span style="color: #ff0000;">actually and the divider of the ListView is very similar</span> . Itemdecoration is not recyclerview must be set, developers can not set or set multiple Decoration. Recyclerview will traverse all the itemdecoration and invoke the respective drawing Method.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mydecoration<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">itemdecoration {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>[] attrs =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">[]{android. r.attr.listdivider}; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">drawable mdivider; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">mydecoration (Context ctx) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>TypedArray A =<span style="color: #000000;"><span style="color: #000000;">ctx.obtainstyledattributes (attrs); Mdivider</span></span>= A.getdrawable (0<span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onDraw (Canvas c, recyclerview parent, State State) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>top =<span style="color: #000000;"><span style="color: #000000;">Parent.getpaddingtop (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Bottom = Parent.getheight ()-<span style="color: #000000;"><span style="color: #000000;">Parent.getpaddingbottom (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ChildCount =<span style="color: #000000;"><span style="color: #000000;">Parent.getchildcount (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=0;i < childcount;i++<span style="color: #000000;"><span style="color: #000000;">) {View</span> child</span>=<span style="color: #000000;"><span style="color: #000000;">Parent.getchildat (i); Recyclerview.layoutparams Layoutparams</span></span>=<span style="color: #000000;"><span style="color: #000000;">(recyclerview.layoutparams) Child.getlayoutparams (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>left = Child.getright () +<span style="color: #000000;"><span style="color: #000000;">layoutparams.rightmargin; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>right = left +<span style="color: #000000;"><span style="color: #000000;">Mdivider.getintrinsicwidth (); Mdivider.setbounds (left, top, right, bottom); Mdivider.draw (c); }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">getitemoffsets (Rect outrect, View view, recyclerview parent, State State) {outrect.set (</span></span>0, 0, mdivider.getintrinsicwidth (), 0<span style="color: #000000;"><span style="color: #000000;">); } }</span></span></pre>Recyclerview.itemanimatior <ul> <ul> <li>Delete an item</li> <li>Add a new item</li> <li>Move an item</li> </ul> </ul><p><p>Google provides a default itemanimator named Defaultitemanimator for developers to Use. If the developer does not set Itemanimator,recyclerview for recyclerview, The default defaultitemanimator will also be used.<br>obviously, in order for the animation to work, the developer must inform the adapter that the data has Changed. Before we used adapter to call Notifydatasetchanged () to notify adapter of data changes and update the view, Recyclerview,adapter now provides a number of notifyxyz () METHODS.</p></p>Process <ul> <ul> <li>Instantiate Recyclerview</li> <li>Set LayoutManager for Recyclerview</li> <li>Set Adapater for Recyclerview</li> <li>If there is a requirement, you can set one or more itemdecorations, and of course, you can not set</li> <li>If there is a need, you can set Itemanimator</li> </ul> </ul>Monitoring Events<p><p>Recyclerview is no longer responsible for the layout and display of the item view, so Recyclerview does not have a click event for item open onitemclick, which needs to be implemented by the Developer.</p></p><p><p>Because Viewholder we can get the root layout of each item, if we set a separate OnClick listener for the root layout and open it to adapter, it's not possible to set the Itemclicklistener when the Recyclerview is Assembled. , but this listener is not set to Recyclerview but is set to Adapter.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Myviewholder<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Viewholder<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">onclicklistener,onlongclicklistener{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">ImageView iv; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">TextView tv; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Myitemclicklistener mlistener; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Myitemlongclicklistener mlongclicklistener; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">myviewholder (View rootview,myitemclicklistener listener,myitemlongclicklistener longclicklistener) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(rootview); IV</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Rootview.findviewbyid (r.id.item_iv); TV</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Rootview.findviewbyid (r.id.item_tv); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Mlistener =<span style="color: #000000;"><span style="color: #000000;">listener; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Mlongclicklistener =<span style="color: #000000;"><span style="color: #000000;">longclicklistener; Rootview.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Rootview.setonlongclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Tap to listen</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mlistener! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {mlistener.onitemclick (v,getposition ()); } } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Long Press to monitor</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Onlongclick (View arg0) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mlongclicklistener! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {mlongclicklistener.onitemlongclick (arg0, getPosition ()); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } }</span></span></pre>Item Length Width<pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mylayoutmanager<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Linearlayoutmanager {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Mylayoutmanager (context Context) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onmeasure (recycler recycler, State state,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>widthspec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">HEIGHTSPEC) {view View</span></span>= Recycler.getviewforposition (0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(view! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {measurechild (view, widthspec, heightspec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Measuredwidth =<span style="color: #000000;"><span style="color: #000000;">measurespec.getsize (widthspec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Measuredheight =<span style="color: #000000;"><span style="color: #000000;">View.getmeasuredheight (); Setmeasureddimension (measuredwidth, measuredheight); } } }</span></span></pre>I'm The dividing line of the king of the land Tiger.<p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Reference: http://www.grokkingandroid.com/first-glance-androids-recyclerview/</p></p></li> </ul><p><p>Android--recyclerview</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service