Android recyclerview Grid Layout

Source: Internet
Author: User

A simple grid layout activity_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><Android.support.constraint.ConstraintLayoutxmlns: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"Tools:context=". Mainactivity ">    <Android.support.v7.widget.RecyclerViewAndroid:id= "@+id/message_notice_list_item"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" /></Android.support.constraint.ConstraintLayout>

Message_main_notice_list.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "150DP"
android:gravity= "Center"
android:background= "@color/coloraccent"
android:orientation= "Vertical"
android:layout_margintop= "5DP"
>

<imageview
Android:id= "@+id/iv_image"
Android:layout_width= "100DP"
android:layout_height= "100DP"
android:background= "@mipmap/logo"
android:gravity= "Center" ></ImageView>

<linearlayout
Android:layout_width= "60DP"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
android:paddingleft= "5DP" >

<textview
Android:id= "@+id/tv_title"
Android:layout_width= "Match_parent"
android:layout_height= "30DP"
android:gravity= "Center"
android:text= "Bibia" ></TextView>


</LinearLayout>


</LinearLayout>


Adapter Myrecyclerviewadapter.java:

 Packagecom.example.administrator.recyclerviewtest;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;Importjava.util.List; Public classMyrecyclerviewadapterextendsRecyclerview.adapter<myrecyclerviewadapter.viewholder>{    PrivateList<itembean>mlist; Static classViewholderextendsrecyclerview.viewholder{View MyView;        ImageView ImageView;        TextView title;  PublicViewholder (View itemview) {Super(Itemview); MyView=Itemview; ImageView=(ImageView) Itemview.findviewbyid (r.id.iv_image); Title=(TextView) Itemview.findviewbyid (r.id.tv_title); }    }     PublicMyrecyclerviewadapter (list<itembean>list) {         This. mlist =list; } @Override PublicViewholder Oncreateviewholder (viewgroup parent,intViewType) {View View= Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.message_main_notice_list,NULL); FinalViewholder holder =Newviewholder (view); returnHolder; }    //to bind data to a control@Override Public voidOnbindviewholder (Viewholder holder,intposition) {Itembean Bean=Mlist.get (position);        Holder.imageView.setBackgroundResource (Bean.itemimage);    Holder.title.setText (Bean.itemtitle); } @Override Public intGetItemCount () {returnmlist.size (); }    //The following two methods are provided to the page to refresh and load when called     Public voidAdd (list<itembean>addmessagelist) {        //Add Data        intPosition =mlist.size ();        Mlist.addall (position, addmessagelist);    notifyiteminserted (position); }     Public voidRefresh (list<itembean>newlist) {        //Refresh DataMlist.removeall (mlist);        Mlist.addall (NewList);    Notifydatasetchanged (); }}

Main method:

 Packagecom.example.administrator.recyclerviewtest;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.support.v7.widget.GridLayoutManager;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List; Public classMainactivityextendsappcompatactivity {PrivateRecyclerview Recyclerview; PrivateList<itembean>list; PrivateMyrecyclerviewadapter myadapte1r; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); List=NewArraylist<itembean>();  for(inti=0; i< 19;i++) {List.add (NewItembean (R.mipmap.logo,"Hello",                    NewDate (). toString () + ""            )); } myadapte1r=Newmyrecyclerviewadapter (list); Recyclerview=(Recyclerview) Findviewbyid (R.id.message_notice_list_item); //Vertical Linear Layout//Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this); //Vertical Linear LayoutGridlayoutmanager LayoutManager =NewGridlayoutmanager ( This, 2);        Recyclerview.setlayoutmanager (LayoutManager);    Recyclerview.setadapter (MYADAPTE1R); }}

Effect:

Android recyclerview Grid Layout

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.