3

I was facing WRAP_CONTENT not work on a RecyclerView , so I googled it and found the workaround. When I tried to implement the workaround in c#, I was stuck at this line:

insetsDirtyField = RecyclerView.LayoutParams.class.getDeclaredField("mInsetsDirty");

Here I tried to port the original java code,and ported code avilable in gist

Community
  • 1
  • 1
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52

1 Answers1

15

I was stuck at this line:

insetsDirtyField = RecyclerView.LayoutParams.class.getDeclaredField("mInsetsDirty");

The .class "keyword" in Java is equivalent to the typeof() keyword in C#, so this is (kinda/sorta) like:

var insetsDirtyField = typeof(RecyclerView.LayoutParams).GetDeclaredField("mInsetsDirty");

Except it isn't, because typeof() returns a System.Type, which doesn't know anything about java.lang.Object instances.

Instead, you should use Java.Lang.Class.FromType(Type) to obtain a Java.Lang.Class instance, which will then allow you to use Java Reflection:

var klass = Java.Lang.Class.FromType (typeof (RecyclerView.LayoutParams));
var insetsDirtyField = klass.GetDeclaredField("mInsetsDirty");
Community
  • 1
  • 1
jonp
  • 13,512
  • 5
  • 45
  • 60