First on:
A normal listview on the left, nothing to say. As for the right side, when I saw the prototype first reaction was Listview+gridview (not used before Recyclerview), and the supervisor after the discussion suggested I use the recyclerliew to achieve, more flexible, more customized.
OK, open the dry!
Thanks to the theory and technical support provided by this blog from the Great God of Hong Yang
Android Recyclerview Art-like controls with full parsing experience
Then get to the point.
Analysis of the requirements, in fact, is a two-level classification and three-level classification, according to different types to differentiate. When the data is displayed, according to Listview+gridview, the above line TextView shows a level two classification, the following GridView shows a level three classification, and then writes two adapters to manage the data. Recycleview flexible and convenient place is reflected here, can be combined to design the required layout, management and use is more simple.
1. layout file:
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"/>
At a glance, nothing.
2. Data Objects:
Public class Category { Public Static Final intSecond_type =0; Public Static Final intThird_type =1;PrivateString CategoryName;Private intType Public Category(String name,intType) { This. CategoryName = name; This. type = type; } PublicStringGetcategoryname() {returnCategoryName; } Public int GetType() {returnType }}
Type is used to differentiate between level two classification and level three classification
3.ViewHolder's declaration:
Viewholder is used to cache the layout information and data of elements to improve performance. The ListView practice is to implement the Viewholder itself to cache the results. The strength of Recyclerview is that it provides the viewholder itself, as long as we inherit from the Viewholder, as for the Viewholder how to store, the system will automatically help us to fix.
Public class secondviewholder extends recyclerview. Viewholder { PrivateTextView secondcategory; Public Secondviewholder(View Itemview) {Super(Itemview); Secondcategory = (TextView) Itemview.findviewbyid (r.id.second_cat_name); } } Public class thirdviewholder extends recyclerview. Viewholder { PrivateTextView thirdcategory; Public Thirdviewholder(View Itemview) {Super(Itemview); Thirdcategory = (TextView) Itemview.findviewbyid (R.id.third_cat_name_first); } }
Use of 4.CategoryAdapter:
/** * Created by Tangyangkai on 16/5/6. * * Public classCategoryadapter extends Recyclerview.adapter<recyclerview.viewholder> {PrivateContext context;Privatelist<category> lists =NewArraylist<> ();PrivateLayoutinflater Layoutinflater; Public Categoryadapter(Context context, list<category> lists) { This. Context = Context; This. Lists = lists; This. Layoutinflater = Layoutinflater. from(context); } @Override Public int Getitemviewtype(intPosition) {returnLists.Get(position). GetType (); } @Override PublicRecyclerview.viewholderOncreateviewholder(ViewGroup parent,intViewType) {if(ViewType = = Category.second_type) {return NewSecondviewholder (Layoutinflater.inflate (R.layout.item_list_category_second,NULL,false)); }Else{return NewThirdviewholder (Layoutinflater.inflate (R.layout.item_list_category_third,NULL,false)); }} @Override Public void Onbindviewholder(Recyclerview.viewholder holder,intPosition) {Switch(Getitemviewtype (position)) { CaseCategory.SECOND_TYPE:SecondViewHolder Secondviewholder = (secondviewholder) holder; SecondViewHolder.secondCategory.setText (lists.Get(position). Getcategoryname ()); Break; CaseCategory.THIRD_TYPE:ThirdViewHolder Thirdviewholder = (thirdviewholder) holder; ThirdViewHolder.thirdCategory.setText (lists.Get(position). Getcategoryname ()); Break; }} @Override Public int GetItemCount() {if(Lists! =NULL) {returnLists.size (); }Else{return 0; } }
The custom Categoryadapter inherits Recyclerview.adapter and overrides several methods of the parent class. Here are some of the ways to focus, highlighting
(1)
publicintgetItemViewType(int position) { return lists.get(position).getType(); }
used to determine what type of layout the element is using, passing in the subscript position of the data source list, returning an int type flag, passed to the second argument of Oncreateviewholder.
(2)
publiconCreateViewHolderint viewType) {}
Based on the Getitemviewtype incoming ViewType to render the construction of the concrete viewholder, return the different types of Viewholder
(3)
publicvoidonBindViewHolderint position) {}
According to the subscript position of the incoming data source list, we get different viewtype and bind the data to different viewholder.
(4)
publicintgetItemCount() {}
Returns the size of the lists
Use in 5.Activity:
Public class recyclerviewactivity extends appcompatactivity { PrivateRecyclerview Recyclerview;PrivateCategoryadapter adapter; list<category> lists =NewArraylist<> ();@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_recycler_view); Initviews (); Initdatas (); }Private void Initdatas() {Lists.add (NewCategory ("Biscuits",0)); Lists.add (NewCategory ("Cream Crackers",1)); Lists.add (NewCategory ("Wafer",1)); Lists.add (NewCategory ("Cookie",1)); Lists.add (NewCategory ("Traditional pastries",0)); Lists.add (NewCategory ("Pineapple Cake",1)); Lists.add (NewCategory ("Almond Pie",1)); Lists.add (NewCategory ("Sesame Cake",1)); Lists.add (NewCategory ("Peanut Cake",1)); Lists.add (NewCategory ("Western Pastry",0)); Lists.add (NewCategory ("Chocolate pie.",1)); Lists.add (NewCategory ("crisp Heart Roll",1)); Lists.add (NewCategory ("Bread",1)); Lists.add (NewCategory ("Puff Puff",1)); Lists.add (NewCategory ("Egg tart",1)); }Private void initviews() {Recyclerview = (Recyclerview) Findviewbyid (R.id.recyclerview); adapter =NewCategoryadapter ( This, lists); Gridlayoutmanager Mlayoutmanager =NewGridlayoutmanager ( This,3); Mlayoutmanager.setspansizelookup (NewGridlayoutmanager.spansizelookup () {@Override Public int getspansize(intPosition) {Switch(Adapter.getitemviewtype (position)) { CaseCategory.second_type:return 3; CaseCategory.third_type:return 1;default:return 1; } } }); Recyclerview.setlayoutmanager (Mlayoutmanager); Recyclerview.setadapter (adapter); }}
The Declaration and initialization of the component, the preparation of the data and the adapter, and then the specific use of the Recyclerview.
A big problem arises when setting the grid Layout manager for Recyclerview to implement a GridView-like effect. Class Two is a column, and the level three category is three columns, so you need to dynamically set the number of columns according to the type. This piece is written by the supervisor, calling the Mlayoutmanager.setspansizelookup method, overriding the Getspansize () method inside to return different values based on the type, and finally displaying the different number of columns.
At this point, we can achieve the beginning. For other uses of Recyclerview, click events, animation effects, etc., there will be time to continue to study the updated ~ ~
Android Recyclerview Cognition and combat