Determining Height Of View(s) That Use layout_rowWeight

Nathan Sokalski 4,121 Reputation points
2021-09-18T21:21:52.91+00:00

I have a View that uses layout_rowWeight in a GridLayout. However, I need to know how much space this actually is. How can I determine the amount of space available in this cell (what is the largest height I can use for a View in this cell)? I need to know this information at runtime so that I can calculate appropriate sizes for several of my View(s). Any ideas?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-09-22T06:46:35.657+00:00

    The RecyclerView will be using a GridLayoutManager, and the number of items is known, so I want to make it fit without the need for scrolling.

    If you want to achieve the above function, try using the following code. It doesn't need to caculate each row's height of the gridLayout.

    public class TestAdapter : RecyclerView.Adapter
    {
        public ObservableCollection<TestModel> list;
        TestViewHolder viewHolder;
        RecyclerView _recyclerView;
    
        public TestAdapter(ObservableCollection<TestModel> list, IOnStartDragListener dragStartListener)
        {
            this.list = list;
            this.dragStartListener = dragStartListener;
        }
        ...
    
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.From(MainActivity.Instance).Inflate(Resource.Layout.recyclerView_itemLayout, parent, false);
    
            TestViewHolder viewHolder = new TestViewHolder(itemView);
    
            var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
            var height = mainDisplayInfo.Height;
            var recyclerViewheight = _recyclerView.Height;
    
            var manager = _recyclerView.GetLayoutManager() as GridLayoutManager;
            int spanCount = manager.SpanCount;
    
            var rows = (int)Math.Ceiling(list.Count / Convert.ToDecimal(spanCount));
            int deviceheight = recyclerViewheight / rows;
    
            viewHolder.ItemView.LayoutParameters.Height = deviceheight;
    
            return viewHolder;
        }
    
        public override void OnAttachedToRecyclerView(RecyclerView recyclerView)
        {
            base.OnAttachedToRecyclerView(recyclerView);
            this._recyclerView = recyclerView;
        }
    }
    

    You only needs to specify the spanCount of the GridLayoutManager in the Activity class, the recyclerView will be placed to fit the screen automatically.

    public class MainActivity : AppCompatActivity
    {
        public static MainActivity Instance;
        public static RecyclerView recyclerView;
        RecyclerView.LayoutManager layoutManager;
        TestAdapter testAdapter;
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
    
            Instance = this;
            recyclerView = FindViewById<RecyclerView>(Resource.Id.test_recyclerView);
            layoutManager = new GridLayoutManager(this, 4);
    
            data = new ObservableCollection<TestModel>();
            //add items to the collection
    
            testAdapter = new TestAdapter(data, this);
            recyclerView.SetLayoutManager(layoutManager);
            recyclerView.SetAdapter(testAdapter);
        }
    }
    
    0 comments No comments