android - Recyclerview Horizontal span and spacing issues -


so im trying achieve, recycler view 3x3 grid , when items exceed 3x3 ,they spill next screen/page.

so ive used recyclerview horizontal scrolling

this ive achieved far

enter image description here

and current code work when count of items 7 , above.

when count below 7 im facing issues need help.

listing issues questions

question 1:

why recyclerview items populated in vertical fashion?

1 4 2 5 3 6 

i've set recyclerview scroll horizontally,

new gridlayoutmanager(this, span, gridlayoutmanager.horizontal, false); 

question 2:

when item count 6 , how avoid space between rows?

enter image description here

question 3:

when items 4 ,i want show items

1 2 3 4 

or

1 3 4 2 

currently shows

enter image description here

if set span 1,ill have grid as

1 2 3 4 

causing 4 go off-screen so

1 2 3 |4 

which want avoid.

==============

code ive used

layout:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="88dp"     android:layout_height="88dp"     android:gravity="center"     android:layout_margin="10dp"     android:background="@drawable/border"     android:orientation="vertical">      <relativelayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:gravity="center"         android:background="@android:color/darker_gray"         android:orientation="vertical"         android:weightsum="1">          <textview             android:id="@+id/lbltitle"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_centerhorizontal="true"             android:text="test"             android:textsize="20sp" />          <textview             android:id="@+id/lbldesc"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_below="@+id/lbltitle"             android:layout_centerhorizontal="true"             android:text="test"             android:textsize="13sp" />     </relativelayout> </linearlayout> 

adapter:

public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.customviewholder> {     context ctx;     list<string> listtext;      public class customviewholder extends recyclerview.viewholder {         textview lbltext;          public customviewholder(view view) {             super(view);             view.setclickable(true);             lbltext = (textview) view.findviewbyid(r.id.lbldesc);         }     }      public recyclerviewadapter(context ctx, list<string> listtext) {         this.ctx = ctx;         this.listtext = listtext;     }      @override     public recyclerviewadapter.customviewholder oncreateviewholder(viewgroup parent, int viewtype) {         view itemview = layoutinflater.from(parent.getcontext())                 .inflate(r.layout.recycler_child_row, parent, false);         return new customviewholder(itemview);     }       @override     public void onbindviewholder(customviewholder holder, int position) {         holder.lbltext.settext(listtext.get(position));     }       @override     public int getitemcount() {         return listtext.size();     } } 

activity

public class mainactivity extends appcompatactivity {     private recyclerview recyclertest;     private gridlayoutmanager gridlayoutrecyclerview;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         recyclertest = (recyclerview) findviewbyid(r.id.recyclertest);         list<string> items = arrays.aslist("1", "2", "3", "4");         /*list<string> items = arrays.aslist("1", "2", "3", "4", "5");*/         /*list<string> items = arrays.aslist("1", "2", "3", "4", "5", "6", "7");*/         /*list<string> items = arrays.aslist("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");*/         /*list<string> items = arrays.aslist("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13","14");*/         /*list<string> items = arrays.aslist("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13","14","15");*/          /*list<string> items = arrays.aslist("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13","14","15","16","17");*/              int span = 0;             if (items.size() <= 3) {             span = 1;         } else if (items.size() <= 6) {             span = 2;         } else {             span = 3;         }          gridlayoutrecyclerview = new gridlayoutmanager(this, span, gridlayoutmanager.horizontal, false);          recyclertest.setlayoutmanager(gridlayoutrecyclerview);          snaphelper snaphelper = new gravitysnaphelper(gravity.start);         snaphelper.attachtorecyclerview(recyclertest);          recyclertest.setadapter(new recyclerviewadapter(this, items));     } } 

why recyclerview items populated in vertical fashion?

1 4 2 5 3 6 

a recyclerview intended lists scroll. if have horizontal layout, fill first column, followed second, third, etc. after all, that's how expect list work. , that's why "numbering off". starts first column, going on next once filled.

when item count 6 , how avoid space between rows?

i don't know setup, i'm guessing recyclerviews height set match_parent, it's using available height. wrap_content height recyclerview might help.

when items 4 ,i want show items as

1 2 3 4 

or

1 3 4 2 

again, see above, that's not how recyclerview works. fill columns 1 one, , not start row. 4 items column count of 2 fill first columns first, below.

1 3 2 4 

a recyclerview might not fit needs. switch vertical grid layout 3 columns less 9 items. way not scroll horizontally, , fill views described. more 9 items you'd still show horizontal version , have scroll.

what you're describing custom layout: want view behaves in specific fashion. have on how create own viewgroup , use lay out views like. unless want show long lists, might alternative option.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -