android - Cardview is all going into one card -


i trying create card view checklist going app school. of items in arraylist going 1 card , cant figure out why. have looked @ every tutorial can find no luck. here relevant code. , appreciated.

activity

recyclerview = (recyclerview) findviewbyid(r.id.masterlistrecview);     recyclerview.sethasfixedsize(true);  layoutmanager = new linearlayoutmanager(this); recyclerview.setlayoutmanager(layoutmanager); recyclerview.setitemanimator(new defaultitemanimator());  thelist = new arraylist<masterlistitem>();  thelist.add(new masterlistitem("test item1",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item2",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item3",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item4",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item5",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item6",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item7",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item8",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item9",new date(),new date(),"here notes",false)); thelist.add(new masterlistitem("test item10",new date(),new date(),"here notes",false));  masterlistadapter = new recviewadapter(thelist); recyclerview.setadapter(masterlistadapter);  masterlistadapter.notifydatasetchanged(); 

recycleview adapter

public class recviewadapter extends recyclerview.adapter<recviewadapter.myviewholder> {     textview title;     textview duedate;     textview completeddate;     textview notes;     checkbox completed;     private arraylist<masterlistitem> thelist;     //private layoutinflater inflater;      public recviewadapter(arraylist<masterlistitem> thelist) {         this.thelist = thelist;         //inflater = layoutinflater.from(context);     }      @override     public myviewholder oncreateviewholder(viewgroup parent, int viewtype) {         view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.master_list_item,parent,false);          myviewholder myviewholder = new myviewholder(view);         return myviewholder;     }      @override     public void onbindviewholder(myviewholder holder, int position) {         title = holder.title;         duedate = holder.duedate;         completeddate = holder.completeddate;         notes = holder.notes;         completed = holder.completed;          title.settext(thelist.get(position).gettitle());         duedate.settext(thelist.get(position).getduedate().tostring());         completeddate.settext(thelist.get(position).getcompleteddate().tostring());         notes.settext(thelist.get(position).getnotes());         completed.setchecked(thelist.get(position).iscompleted());     }        @override     public int getitemcount() {         return thelist.size();     }      @override     public void onattachedtorecyclerview(recyclerview recyclerview) {         super.onattachedtorecyclerview(recyclerview);     }       public class myviewholder extends recyclerview.viewholder{         private textview title;         private textview duedate;         private textview completeddate;         private textview notes;         private checkbox completed;         public myviewholder(view itemview){             super(itemview);             this.title = (textview) itemview.findviewbyid(r.id.masterlisttitle);             this.duedate = (textview) itemview.findviewbyid(r.id.masterlistduedate);             this.completeddate = (textview) itemview.findviewbyid(r.id.masterlistcompleteddate);             this.notes = (textview) itemview.findviewbyid(r.id.masterlistnotes);             this.completed = (checkbox) itemview.findviewbyid(r.id.masterlistcompleted);         }     }    } 

recycleview xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context="edu.uco.weddingcrashers.hitched.masterweddinglist">      <button         style="?android:attr/buttonstylesmall"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="+"         android:id="@+id/addtaskbutton"         android:layout_alignparenttop="true"         android:layout_alignparentend="true" />      <android.support.v7.widget.recyclerview         android:layout_width="match_parent"         android:layout_height="match_parent"         android:scrollbars="vertical"         android:id="@+id/masterlistrecview"         >        </android.support.v7.widget.recyclerview>   </relativelayout> 

card xml

    <?xml version="1.0" encoding="utf-8"?>   <android.support.v7.widget.cardview     android:id="@+id/masterlistcardview"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:padding="16dp"     xmlns:card_view="http://schemas.android.com/apk/res-auto"     card_view:cardcornerradius="4dp"     xmlns:android="http://schemas.android.com/apk/res/android">       <!--     android:layout_alignparentstart="true"         android:layout_gravity="center"     android:layout_below="@+id/addtaskbutton"     -->           <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"             android:orientation="vertical" android:layout_width="match_parent"             android:layout_height="match_parent">               <textview                 android:id="@+id/masterlisttitle"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:text="test title" />             <textview                 android:id="@+id/masterlistduedate"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:text="test title" />             <textview                 android:id="@+id/masterlistcompleteddate"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:text="test title" />             <textview                 android:id="@+id/masterlistnotes"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:text="test title" />             <checkbox                 android:id="@+id/masterlistcompleted"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="completed"                 android:editable="false"                 android:layout_gravity="center_horizontal" />         </linearlayout>       </android.support.v7.widget.cardview> 

once again thank much!

you must add divider between recyclerview item. adding divider , decoration recyclerview different other list views. can achieve answer:

unlike listview, recyclerview class has no divider-related parameters. instead, need extend itemdecoration, recyclerview's inner class:

an itemdecoration allows application add special drawing , layout offset specific item views adapter's data set. can useful drawing dividers between items, highlights, visual grouping boundaries , more.

all itemdecorations drawn in order added, before item views (in ondraw() , after items (in ondrawover(canvas, recyclerview, recyclerview.state).

vertical spacing itemdecoration extend itemdecoration, add custom constructor takes space height parameter , override getitemoffsets():

public class verticalspaceitemdecoration extends recyclerview.itemdecoration {      private final int mverticalspaceheight;      public verticalspaceitemdecoration(int mverticalspaceheight) {         this.mverticalspaceheight = mverticalspaceheight;     }      @override     public void getitemoffsets(rect outrect, view view, recyclerview parent,             recyclerview.state state) {         outrect.bottom = mverticalspaceheight;     } } 

if don't want insert space below last item, add following condition:

if (parent.getchildadapterposition(view) !=                               parent.getadapter().getitemcount() - 1) {             outrect.bottom = mverticalspaceheight; } 

note: can modify outrect.top, outrect.left , outrect.right properties desired effect.

divider itemdecoration extend itemdecoration , override ondraw():

public class divideritemdecoration extends recyclerview.itemdecoration {     private static final int[] attrs = new int[]{android.r.attr.listdivider};      private drawable mdivider;      /**      * default divider used      */     public divideritemdecoration(context context) {         final typedarray styledattributes = context.obtainstyledattributes(attrs);         mdivider = styledattributes.getdrawable(0);         styledattributes.recycle();     }      /**      * custom divider used      */     public divideritemdecoration(context context, int resid) {         mdivider = contextcompat.getdrawable(context, resid);     }      @override     public void ondraw(canvas c, recyclerview parent, recyclerview.state state) {         int left = parent.getpaddingleft();         int right = parent.getwidth() - parent.getpaddingright();          int childcount = parent.getchildcount();         (int = 0; < childcount; i++) {             view child = parent.getchildat(i);              recyclerview.layoutparams params = (recyclerview.layoutparams) child.getlayoutparams();              int top = child.getbottom() + params.bottommargin;             int bottom = top + mdivider.getintrinsicheight();              mdivider.setbounds(left, top, right, bottom);             mdivider.draw(c);         }     } } 

you can either call first constructor uses default android divider attributes, or second 1 uses own drawable, example drawable/divider.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"        android:shape="rectangle">     <size             android:height="1dp" />     <solid android:color="#ff992900" /> </shape> 

note: if want divider drawn on items, override ondrawover() instead.

usage

to use new class add verticalspaceitemdecoration or dividerspaceitemdecoration recyclerview, example in fragment's oncreateview():

private static final int vertical_item_space = 48; private recyclerview muirecyclerview; private linearlayoutmanager mlinearlayoutmanager; @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     view rootview = inflater.inflate(r.layout.fragment_feed, container, false);      muirecyclerview = (recyclerview) rootview.findviewbyid(r.id.fragment_home_recycler_view);     mlinearlayoutmanager = new linearlayoutmanager(getactivity());     muirecyclerview.setlayoutmanager(mlinearlayoutmanager);      //add itemdecoration     muirecyclerview.additemdecoration(new verticalspaceitemdecoration(vertical_item_space));     //or     muirecyclerview.additemdecoration(             new divideritemdecoration(getactivity());     //or     muirecyclerview.additemdecoration(             new divideritemdecoration(getactivity(), r.drawable.divider));      muirecyclerview.setadapter(...);      return rootview; } 

there's lucas rocha's library supposed simplify item decoration process. haven't tried though.

among features are:

a collection of stock item decorations including:

  • item spacing horizontal/vertical dividers.
  • list item

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -