Set Rowspan or Colspan of a Child of a Gridlayout Programmatically

Set rowSpan or colSpan of a child of a GridLayout programmatically?

OK, I spent some hours figuring out what's going on here. Well, I didn't find any working way to set columnSpan or rowSpan at runtime.

But I found a solution that works (at least for me):

Java code

private LinearLayout addNewSpannedView(Integer resourceId, ViewGroup rootElement) {
return (LinearLayout) ((ViewGroup) getLayoutInflater().inflate(resourceId, rootElement, true)).getChildAt(rootElement.getChildCount() - 1);
}
// set columnSpan depending on some logic (gridLayout is the layout to add the view's to -> in my case these are LinearLayouts)
shape = addNewSpannedView(columnSpan == 1 ? R.layout.grid_ll_col_span_1 : R.layout.grid_ll_col_span_2, gridLayout);

grid_ll_col_span_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/shapeWidth"
android:layout_height="wrap_content"
android:layout_columnSpan="2"/>

Hint: It's very important, that you set the width and height attribute, before inflate() adds the view to the root element (i.e. the parent element).

I hope, someone can use this ;-)

Set ColumnSpan and ColumnWeight programmatically

So,
Trying a lot, and spending a lot of time finnally work with this:

  GridLayout.LayoutParams doubleLayoutParams = new GridLayout.LayoutParams();
doubleLayoutParams.rowSpec = GridLayout.spec(i,1);
// GridLayout.spec(rowNumber,rowSpan);
doubleLayoutParams.columnSpec = GridLayout.spec(0, 2f);
// GridLayout.spec(columnNumber,columnSpan);

TextView newName = new TextView(myContext);
newName.setText(newPlayer.getName().trim() + " " + newPlayer.getLastName().trim());
newName.setTextColor(ContextCompat.getColor(myContext, R.color.black_50));
newName.setTextSize(12);
newName.setEllipsize(TextUtils.TruncateAt.END);
newName.setSingleLine();
newName.setWidth(0);
grdTeamAPlayers.addView(newName, doubleLayoutParams);

Setting the param of a button programmatically

You can use the following approach with API 21 and later mention in:
Set rowSpan or colSpan of a child of a GridLayout programmatically?

https://developer.android.com/reference/android/widget/GridLayout.Spec

Spec spec(int start, int size, float weight)

param.rowSpec = GridLayout.spec(0, 1, (float)1.0);    
param.columnSpec = GridLayout.spec(0, 1, (float)1.0);

Setting the param of a button programmatically

You can use the following approach with API 21 and later mention in:
Set rowSpan or colSpan of a child of a GridLayout programmatically?

https://developer.android.com/reference/android/widget/GridLayout.Spec

Spec spec(int start, int size, float weight)

param.rowSpec = GridLayout.spec(0, 1, (float)1.0);    
param.columnSpec = GridLayout.spec(0, 1, (float)1.0);

How to set Grid row and column positions programmatically

For attached properties you can either call SetValue on the object for which you want to assign the value:

tblock.SetValue(Grid.RowProperty, 4);

Or call the static Set method (not as an instance method like you tried) for the property on the owner type, in this case SetRow:

Grid.SetRow(tblock, 4);

How to set different columns for rows in android gridview

I have something similar and i solved with the new RecyclerView.

I created a Fragment with an a RecyclerView.
RecyclerView on xml:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/filter_subtypes" android:layout_width="match_parent" android:layout_height="match_parent" />

On your Fragment/Activity (OnViewCreated in my fragment case).
I find the RecyclerView and set an Adapter- a normal Adapter class inherit from RecyclerView.Adapter< YOUR VIEW HOLDER class >-
And then i create a GridLayoutManager

final GridLayoutManager mng_layout = new GridLayoutManager(this.getActivity(), TOTAL_CELLS_PER_ROW/*In your case 4*/);



Then i override this method to set a dynamic numbers of columns (cells)

mng_layout.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch( adapterSubtype.getItemViewType(position) ) {
case FilterSubtypesAdapter.TYPE_LOW:
return TOTAL_CELLS_PER_ROW;
case FilterSubtypesAdapter.TYPE_HIGH:
return 2;
default:
return -1;
}
}
});
myRecyclerView.setLayoutManager(mng_layout);

With this you will get dynamic numbers of cell on your rows.

EXTRA:
Then if you are using the same view/type view on your adapter, you will get the same w & h view. You will need to create 2 xml views for TYPE_HIGH and other view for TYPE_LOW.

So, in your adapter, you need to have 2 kind of data (1 for high images and 1 for low images).
You must override this methods

@Override
public SubtypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
if (viewType==TYPE_HIGH) {
view = inflater.inflate(R.layout.item_image_high, parent, false);
} else {
view = inflater.inflate(R.layout.item_image_low, parent, false);
}
return new SubtypeViewHolder(view, viewType);
}

@Override
public int getItemViewType(int position) {
return (list.get(position).getType()==Subtype_type.HIGH) ? TYPE_HIGH : TYPE_LOW;
}

I hope i was clear, any problem tell me.



Related Topics



Leave a reply



Submit