Using Staggeredgridlayoutmanager to achieve waterfall flow effects

Source: Internet
Author: User

Development environment: Android Studio

Prepare to work: Open the project's Build.gradle Add reference code to support V7 as follows

dependencies {    ...    ‘com.android.support:appcompat-v7:21.0.3‘    ...}
    • First create an XML layout file that contains Recyclerview Activity_main.xml
<android.support.v7.widget.RecyclerView     xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/recyclerView"    android:scrollbars="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent" />

The recyclerview here are longitudinal.

    • Complete the View section code in Mainactivity
 Public  class mainactivity extends Activity {    PrivateRecyclerview Mrecyclerview;PrivateItemadapter Madapter;PrivateRecyclerview.layoutmanager Mlayoutmanager;Privatearraylist<string> items =NewArraylist<> ();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }Private void Initview() {Mrecyclerview = (Recyclerview) Findviewbyid (R.id.recyclerview);//Column count is two columns        intSpancount =2; Mlayoutmanager =NewStaggeredgridlayoutmanager (Spancount, staggeredgridlayoutmanager.vertical); Mrecyclerview.setlayoutmanager (Mlayoutmanager);//Build a temporary data source         for(inti =0; I < -; i++) {Items.Add ("I:"+ i); } Madapter =NewItemadapter (items);    Mrecyclerview.setadapter (Madapter); }}
    • The code for Itemadapter is as follows:
 Public  class itemadapter extends recyclerview. Adapter<itemadapter. Viewholder> {    Privatearraylist<string> items =NewArraylist<> (); PublicItemadapter (arraylist<string> items) { This. items = items; } @Override PublicViewholder Oncreateviewholder (ViewGroup viewgroup,intViewType) {View view = Layoutinflater.from (Viewgroup.getcontext ()). Inflate (R.layout.item_card, view Group,false);return NewViewholder (view); } @Override Public intGetitemviewtype (intPosition) {return Super. Getitemviewtype (position); } @Override Public voidOnbindviewholder (Viewholder Viewholder,intPosition) {String info = items.get (position);        View view = Viewholder.itemview;        TextView TextView = (TextView) View.findviewbyid (R.id.info_text); Textview.settext (info);//Manually change height, different height varies by locationTextview.setheight ( -+ (position%3) * -); } @Override Public intGetItemCount () {returnItems.size (); } class viewholder extends recyclerview. Viewholder {         PublicViewholder (View itemview) {Super(Itemview); }    }}
    • The code for the Item_card.xml layout file is as follows:
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:card_view="Http://schemas.android.com/apk/res-auto"Android:layout_width="Fill_parent"android:gravity="Center"android:layout_height="Fill_parent"> <android. Support. V7. Widgets. CardViewAndroid:id="@+id/card_view"android:layout_gravity="Center"Android:layout_width="150DP"android:layout_height="Wrap_content"card_view:cardusecompatpadding="true"card_view:cardcornerradius="4DP"> <textview android:id="@+id/info_text"Android:layout_width="Fill_parent"android:gravity="Center"android:layout_height="Wrap_content"/> </android. Support. V7. Widgets. CardView></LinearLayout>

CardView is used here, if you want to call to CardView correctly, add the reference code to CardView in Build.gradle as follows

dependencies {    ...    ‘com.android.support:cardview-v7:21.0.3‘    ...}
    • The result of running the last build project is as follows:

Using Staggeredgridlayoutmanager to achieve waterfall flow effects

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.