0

I have a ListView. I notice that if I set the LayoutParams like the below, the ListView fetches each cell a lot of times, which makes things slow. [Code is C# as this is via Mono:]

  var layoutParams = new RelativeLayout.LayoutParams (someWidth, someHeight);
  layoutParams.LeftMargin = someLeftMargin;
  layoutParams.TopMargin = someTopMargin;
  this.LayoutParameters = layoutParams;

I can get rid of the slowness by doing the following:

  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
  this.LayoutParameters = layoutParams;

But that forces me to put an additional layer into the hierarchy unless I really do want to match the parent's size. I'd like to avoid that, if possible.

I'm wondering if it is possible for me to have the best of both worlds? Is it possible to set LayoutParams that put the ListView in an arbitrary location within its parent, without having the thing fetch each cell a whole bunch of times, thereby killing performance.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323

1 Answers1

0

It seems that the following does it:

  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
  layoutParams.SetMargins(leftMargin, topMargin, rightMargin, bottomMargin); // four ints
  this.LayoutParameters = layoutParams;

This is quite strange. I have no idea why Android would handle the two cases differently. But it does solve the problem.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323