What is Recyclerview?
Recyclerview is a new view group with the goal of providing a similar rendering for any adapter-based view. It is used as a successor to the ListView and GridView controls and is supported in the latest version of SUPPORT-V7.
Extensibility is well thought out when developing recyclerview, so use it to create layouts of any kind you think of. But it is also slightly inconvenient to use. This is android--is not so easy to accomplish a thing.
Looking at the Recyclerview architecture as a whole, it provides a plug-and-play experience, a high degree of decoupling, exceptional flexibility, and an eye-popping effect by setting the different layoutmanager,itemdecoration it offers.
Recyclerview can implement the following functions:
Features of the ListView
Features of the GridView
Features of the landscape listview
The function of transverse scrollview
Waterfall Flow Effect
Easy to add item additions and removal animations
Basic use
Introduction of the Official V7 package
Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:context=". Mainactivity "Tools:showin= "@layout/activity_main" > <Android.support.v7.widget.RecyclerView Android:id= "@+id/recyclerview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"/></relativelayout>
Declarations in Java code are the same as normal controls;
Recyclerview = (Recyclerview) Findviewbyid (R.id.recyclerview); Recyclerview.setlayoutmanager (new Gridlayoutmanager (this, 2)); Recyclerview.setadapter (adapter);
Setlayoutmanager requires a LayoutManager class, which has three implementations, namely:
1.LinearLayoutManager current manager, supports landscape, portrait
2.GridLayoutManager Grid Layout Manager
3.StaggeredGridLayoutManager Waterfall-style layout manager
The above code uses Gridlayoutmanager to show a two-column grid layout effect;
In other words, Adapter,recyclerview contains a new adapter, which also requires the use of viewholder, which requires rewriting two main methods: a view and its holder onCreateViewHolder(ViewGroup parent, int viewType) , One used to bind data to a view onBindViewHolder(ViewHolder holder, int position) . The advantage of this is that onCreateViewHolder it is only called when we really need to create a new view, and we do not need to check whether it has been recycled.
Adapter Code:
Public classRecycleradapterextendsRecyclerview.adapter<recycleradapter.myviewholder> { Privatecontext Context; PrivateList<string>Mdatas; PublicRecycleradapter (context context, list<string>Mdatas) { Super(); This. Context =context; This. Mdatas =Mdatas;} @Override PublicMyviewholder Oncreateviewholder (viewgroup parent,intViewType) {Myviewholder Holder=NewMyviewholder (Layoutinflater.from (context). Inflate (R.layout.item_home, parent,false)); returnHolder; } @Override Public voidOnbindviewholder (FinalMyviewholder Holder,intposition) {Holder.tv.setText (mdatas.get (position));} @Override Public intGetItemCount () {returnmdatas.size (); } classMyviewholderextendsRecyclerview.viewholder {TextView TV; PublicMyviewholder (View itemview) {Super(Itemview); TV=(TextView) Itemview.findviewbyid (R.id.pos); } } }
Item Code:
<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/pos " android:layout_width=" 144dip " android:layout_ Height= "72dip" android:background= "@color/colorprimary" android:gravity= " Center " android:layout_margin=" 4dip " android:textcolor=" #ffffff "/>
The reason why a 4DP margin is set is because Recyclerview does not provide us with a set of Split line properties like the ListView, so you can set a margin in item, but you can actually pass Recyclerview's Additemdecoration method to customize a split line.
Run the above code to show the effect:
Using the Linearlayoutmanager display effect
Recyclerview.setlayoutmanager (new Linearlayoutmanager (this));
Using the Staggeredgridlayoutmanager display effect
Recyclerview.setlayoutmanager (new Staggeredgridlayoutmanager (3, staggeredgridlayoutmanager.vertical));
Using Staggeredgridlayoutmanager to show waterfall flow effects requires a random setting of item height in adapter
Adapter Code
Public classRecycleradapterextendsRecyclerview.adapter<recycleradapter.myviewholder> { Privatecontext Context; PrivateList<string>Mdatas; PrivateList<integer>mheights; PublicRecycleradapter (context context, list<string>Mdatas) { Super(); ... ...mheights=NewArraylist<integer>(); for(inti = 0; I < mdatas.size (); i++) {Mheights.add (int) (+ math.random () * 300)); }
}@Override Public voidOnbindviewholder (FinalMyviewholder Holder,intposition) {Viewgroup.layoutparams LP=Holder.tv.getLayoutParams (); Lp.height=Mheights.get (position); HOLDER.TV.SETLAYOUTPARAMS (LP); ... ... }
The other code is the same as the above adapter, where only the added part is posted, the effect is as follows:
The second parameter of the Staggeredgridlayoutmanager construct in the code above passes a orientation, if the incoming is a StaggeredGridLayoutManager.VERTICAL representation of how many columns, then the incoming if is the StaggeredGridLayoutManager.HORIZONTAL number of rows represented. For example, change into
Recyclerview.setlayoutmanager (new Staggeredgridlayoutmanager (3, Staggeredgridlayoutmanager.horizontal)) ;
The corresponding adapter are changed into
@Override Public void Onbindviewholder (finalint position) { = holder.tv.getLayoutParams (); = Mheights.get (position); HOLDER.TV.SETLAYOUTPARAMS (LP);}
Run the following effect
Here only the static diagram, in fact, can be horizontal sliding.
Again, the animated effect of item, Recyclerview supports the custom animation effect of item. There are also a lot of custom effects on GitHub, and here's a link.
Https://github.com/wasabeef/recyclerview-animators
Of course the system also provides us with a default effect
Recyclerview.setitemanimator (new defaultitemanimator ());
A code to show the effect as follows
Two additional methods in adapter
Public void addData (int position) { "New Item"); notifyiteminserted (position); } Public void removedata (int position) { mdatas.remove (position); notifyitemremoved (position); }
Call in activity
@Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); Switch(ID) { CaseR.id.action_add:adapter.adddata (1); Break; CaseR.id.action_delete:adapter.removedata (1); Break; } return Super. onoptionsitemselected (item); }
It is important to note that the Recyclerview update data is notifyItemInserted(position) used notifyItemRemoved(position) differently than here and the ListView notifyDataSetChanged , although the Recyclerview also provides notifyDataSetChanged methods, but notifyDataSetChanged the use is without any animated effect.
Since the Recyclerview is very strong, the system does not provide us with a Setonitemclicklistener method, which we need to define ourselves, we can define the item callback interface in adapter
Public Interface Onitemclicklitener { voidint position); void int position); } Private Onitemclicklitener Monitemclicklitener; Public void Setonitemclicklitener (Onitemclicklitener monitemclicklitener) { this. Monitemclicklitener = monitemclicklitener; }
Called in Onbindviewholder
@Override Public voidOnbindviewholder (FinalMyviewholder Holder,intposition) { ... ...if(Monitemclicklitener! =NULL) {Holder.itemView.setOnClickListener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {intpos =holder.getlayoutposition (); Monitemclicklitener.onitemclick (Holder.itemview, POS); } }); Holder.itemView.setOnLongClickListener (NewView.onlongclicklistener () {@Override Public BooleanOnlongclick (View v) {intposition=holder.getlayoutposition (); Monitemclicklitener.onitemlongclick (holder.itemview,position); return true; } }); } }
Add the following code to the activity
Adapter.setonitemclicklitener (NewRecycleradapter.onitemclicklitener () {@Override Public voidOnitemclick (View view,intposition) {Toast.maketext (mainactivity). This, Position + "click", Toast.length_short). Show (); } @Override Public voidOnitemlongclick (View view,intposition) {Toast.maketext (mainactivity). This, Position + "Longclick", Toast.length_short). Show (); } });
Operating effect:
To those who also recyclerview not too familiar with the brothers, need source code please leave a message.
Resources
Https://github.com/wasabeef/recyclerview-animators
http://blog.csdn.net/lmj623565791/article/details/45059587
http://blog.jobbole.com/74208/
Use of Android Recyclerview