Saturday, 15 January 2011

how to dynamically change the gridview rows and columns in Android -


I am using Gridview in my activity. I also have a custom grid adapter to show the image and text view on each grid.

On starting my activity, I need to create dynamic grid rows and columns based on the values ​​

  count = 4 2x2 grid number = 9 3x3 grid count = 16 4x4 grid  

Please tell me how I can get this functionality.

My Gridview:

I have created the column Android: numColumns = "auto_fit" so it's default for 2 columns.

I also have a custom grid adapter

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" android: background = "@ drawable / Grid_ color_selector" & gt; & Lt; ImageView android: id = "@ + id / image" Android: Leaut_vidth = "100 DP" Android: Leaut_hait = "100 DP" Android: Leaut_margin Right = "10 DP" / & gt; & Lt; TextView android: id = "" @ + id / Day_TextView Android: Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" android: background = "@ drawable / gridview_line_norecord" android: gravity = "center" Android: textcolor = "@ android : Color / White "Android: Text Size =" 24sp "Android: Text =" Hello "/>   

350); }

But I'm worried about manually giving value inside the activity. As I think it can give unusual UI for landscape mode.

Please help me move forward.

Homie's comment is correct

Use the recycle view and grid layout manager. ! In .xml

  & lt; Android.support.v7.widget.RescyclerView Android: layout_width = "match_parent" android: layout_height = "match_parent" android: numColumns = "2" Android: horizontalSpacing = "@ Dimen / gridview_horizontal_spacing" Android: verticalSpacing = "@ Dimen / gridview_vertical_spacing" Android: ID = "@ + id / recyclerView" />  

and recyclerView.setLayoutManager (New GridLayoutManager (reference, number));

or

Gridview.setNumColumns (number);


No comments:

Post a Comment