Use of the Android V7 Compatibility Pack Recyclerview (iii)--use of the layout manager

Source: Internet
Author: User

The first two articles
Use of the Android V7 Compatibility Pack Recyclerview (ii)
Use of the Android V7 Compatibility Pack Recyclerview (i)
This article introduces the basic usage of Recyclerview and several important classes related to it, and describes the specific usage of the layout manager.

In order to demonstrate the use of the layout manager, looking for a number of examples, did not find the feeling of the appropriate example, and then Google A, found a more appropriate to illustrate the problem example. So take the example to explain it.

Before we show the layout manager, we'll set up the UI section. Since we used another package CardView for the V7 Compatibility Pack, we first imported the project into Eclipse and set it to the library, which is in Sdk\extras\android\support\v7\cardview
Then we create a new project to introduce the library, and then add Recyclerview.jar to the Libs directory, so that the basic configuration is complete. Start writing code.
Item uses CardView, the code is as follows

<?xml version= "1.0" encoding= "Utf-8"?><android.support.v7.widget.cardview  xmlns:android  =" http://schemas.android.com/apk/res/android "  xmlns:card_view  = "HTTP +/ Schemas.android.com/apk/res-auto " android:layout_width  =" match_parent " android:layout_height  =     "match_parent"  android:layout_margin  = "5DP"  android:orientation  =" horizontal " card_view:cardcornerradius  = "5DP"  card_view:cardusecompatpadding  =;     <relativelayout  android: Layout_width  = "match_parent"  android:layout_ Height  = "match_parent" ;         <TextViewandroid:id="@+id/info_text"android:layout_width="Match _parent "android:layout_height=" Wrap_content "android:layout_centerinparent=  "True"android:gravity="center"android:textcolor="@android: color/ Black "android:textsize=" 24sp " />                                                                                        </relativelayout></android.support.v7.widget.CardView>

Create adapter, code the first two articles have already been explained, here no longer explain

 PackageCn.edu.zafu.layoutmanager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; Public  class cardviewadapter extendsrecyclerview. Adapter<cardviewadapter. Viewholder> {            //Data set     Publicstring[] data; Public Cardviewadapter(string[] data) { This. data = data; }@Override     PublicCardviewadapter.viewholderOncreateviewholder(ViewGroup parent,intViewType) {//Bind layoutView Itemlayoutview = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.item,NULL);//Create ViewholderViewholder Viewholder =NewViewholder (Itemlayoutview);returnViewholder; }@Override     Public void Onbindviewholder(Viewholder Viewholder,intPosition) {//Bind dataViewHolder.info.setText (Data[position].tostring ()); }@Override     Public int GetItemCount() {returnData.length; } Public Static  class viewholder extends recyclerview. Viewholder {         PublicTextView info; Public Viewholder(View Itemlayoutview) {Super(Itemlayoutview);        info = (TextView) Itemlayoutview.findviewbyid (R.id.info_text); }    }}

Declaring Recyclerview in the main layout file

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:background  =" #c9c9c9 " android:orientation  = "vertical" ;     <android.support.v7.widget.RecyclerViewandroid:id="@+id/recyclerview"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:scrollbars="vertical" />                                </linearlayout>

For use in activity, it's easy to create a linear layout manager first.

 PackageCn.edu.zafu.layoutmanager;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.support.v7.widget.GridLayoutManager;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.support.v7.widget.RecyclerView.LayoutManager;ImportAndroid.support.v7.widget.StaggeredGridLayoutManager; Public  class mainactivity extends Activity {    PrivateRecyclerview Mrecyclerview;PrivateRecyclerview.adapter Madapter;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); string[] data = {"Staggeredgridlayoutmanager","LayoutManager","Gridlayoutmanager","Adapter","Viewholder","Linearlayoutmanager","CardView","ListView","TextView","Vertical","Horizontal","Recyclerview"}; Mrecyclerview = (Recyclerview) Findviewbyid (R.id.recyclerview);//If the layout size is consistent to facilitate optimizationMrecyclerview.sethasfixedsize (true);//Using Linear layout managerLayoutManager layout =NewLinearlayoutmanager ( This); Mrecyclerview.setlayoutmanager (layout);//Initialize adapter and bind adapterMadapter =NewCardviewadapter (data);    Mrecyclerview.setadapter (Madapter); }}

Now look at the effect of the run.

The effect is not also, basically and the effect of the ListView consistent.
Okay, so now let's change the code and move the focus to the layout manager.
Look at Linearlayoutmanager first, the class has two constructors.

Linearlayoutmanager (context)
Linearlayoutmanager (context, orientation, reverselayout)

The first parameter is the context, the second argument is the layout direction, and its value can be taken

Linearlayoutmanager.horizontal level
Linearlayoutmanager.vertical Vertical

The third parameter is whether to reverse layout if set to True, then reverse start layout, now we set the layout to horizontal, and reverse layout, using the following code

new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);

Now the effect is like this.

As you can see, all the data is laid out from right to left, which is the reverse layout.

In addition, there are some other methods in the layout manager, see the API documentation.

With a linear layout, we'll change the layout,

LayoutManager layout=new GridLayoutManager(this,2);

Grid layout, set 2 columns, default vertical layout. As follows.

Change it to 3 columns

LayoutManager layout=new GridLayoutManager(this,3);

Like the linear layout, the grid layout also has a constructor, meaning the same as the linear layout, where we do not apply the reverse layout, set 2 columns, vertical, the effect is the same as the previous diagram.

LayoutManager layout=new GridLayoutManager(this2false);

There is also a layout manager that is a streaming layout manager, which has only one constructor, one for the number of columns or number of rows, the other for the layout direction, and still the code

LayoutManager layout=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

Effect, is now very popular flow layout, in the major shopping sites can see this effect.

At this point, the V7 compatibility package built several layout managers have used some, then what is the feeling, yes, two words, is flexible, you can see, from the opening to the end, we just modified a line of code, to achieve a different effect, this is the layout manager's flexibility, We can also go to inherit LayoutManager rewrite our own layout manager, I level limited, unable to reach this level, interested can go to try.
Of course, the use of layout management is not just the point mentioned above, there are many ways to wait for us to dig.

Source Download
http://download.csdn.net/detail/sbsujjbcy/8494401

Use of the Android V7 Compatibility Pack Recyclerview (iii)--use of the layout manager

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.