Creating square layouts in RecyclerView items

There are a couple of different ways to create a square child layout in RecyclerView. One way is, using the View.post(...) method where, you set the height of the view to it's width. This involves, laying out the view first and then setting their width/height which in turn incurs a bit of performance. Besides, run-time changes of child views inside RecyclerView doesn't work that well (as of writing this) and cause stuttering scrolling. So, a much more simpler way is to use a SquareFrameLayout! Copy the code (or grab the file from the gist) below to your project and you're all set!


Use the layout like:

<android.sz.common.widgets.SquareFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your views -->

</android.sz.common.widgets.SquareFrameLayout>

No comments:

Post a Comment

Power function

In C/C++ (and also other languages), we have the built in power function to raise a number to a given power. What if we need to calculate ...