1

I am implementing a RecyclerView, I require the width of the fragment as it directly affects the items in the RecyclerView. Where and how is the best option to do this? I am also looking for a solution that satisfies orientation changes (portrait/landscape) and that is able to redraw according to these changes.

There are two types of ways getting the width that I know of:

  • getView().getViewTreeObserver().addOnGlobalLayoutListener...
  • MyOnResizeListener orlResized = new MyOnResizeListener() {...};

I create my adapter in onViewCreated() and the two above methods are firing after my adapter item views are created, which is of no use then.

Is there any good practice way to solving this?

basickarl
  • 37,187
  • 64
  • 214
  • 335

1 Answers1

0

you have two options:

  • create your adapter inside the GlobalLayoutListener; or
  • use pre-determined information about your project to calculate the size. For example: the fragment is in a container inside the main activity that stretches to the whole width and have 12dp margins on the sides. Then during onCreateView you can check the device screen width, get the value of 12dp and calculate frgWidth = screenWidth - (2 * dp12);
Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144