1

Suppose you have a FrameLayout containing 10 LinearLayouts, where only one is visible per time.

Each LinearLayout is a complex view, containing Button, EditText, TextView, etc.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/alice
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <!-- complex stuff -->

    </LinearLayout>

    <!-- many more linear layouts... -->

    <LinearLayout
        android:id="@+id/juliett
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <!-- last complex stuff -->

    </LinearLayout>

</FrameLayout>

Thus:

  1. Changing the LinearLayout visibility, in order to show another item, would be a huge performance issue?
  2. Given it is an issue, why using ViewFlipper does not slow down the app performance?
JP Ventura
  • 5,564
  • 6
  • 52
  • 69

3 Answers3

1

This is not a good way to implement because each time you need to show another view, other views must be gone. So that, you are going to write duplicated lines of codes for it. Viewswitcher is better choice. So what about performance then? View switcher is going to measure all children views which make only draw inside of itself. This trick makes view switcher faster because it does not need to recalculate dimensions for itself unless you disable it to use heterogeneous children views.

İf your views are homogeneous, the best way is implement a custom view and giving a class to changing state. For example, you set Alice object to your custom view to show Alice's properties and changing it programmaticly up to your business logic.

Good luck

Emre

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • The methods `onLayout`, `onMeasure`, `onRequestFocusInDescendants`, `onDraw` are called (on this sequence) only on the view that is being set to `View.VISIBLE`, not on the ones where `View.GONE`. I am aware that `Fragment` or `ViewFlipper` is a _cleaner_ solution, but I am questioning if there is a _performance issue_ as well. – JP Ventura Jun 28 '17 at 20:56
1

It's bad practice because the code easily become a mess. Ignoring that and focusing only on performance, when you set the visibility to GONE, the view isn't measured (it's different from INVISIBLE). The view occupies a little bit of memory, though. Depending on what you're doing, consider using ViewGroup.removeView().

It's hard to say without a benchmark, but theoretically it shouldn't have performance issues.

-1

Seriously, you need to consider fragment for above situation. why to inflate un-necessary views.

AKASH WANGALWAR
  • 1,326
  • 1
  • 14
  • 21