Use ViewStub in combination with ViewPager

Tram Ho

1. Math problems

If anyone has used via Viewpager surely everyone knows that ViewPager is responsible for saving the state of the views inside it is determined to be 1 view before invisible view and 1 view after invisible view. But the problem here is only suitable when your data is not too large, when creating Viewpager, creating at the same time the views does not take too much resources and time, it is extremely convenient for users. reload for the next time. But with large data pages, loading multiple views at once makes the app become annoying lag.

People try to think of a website with big data like shopee that only uses ViewPager to load data of 5 large tabs in which each tab has 3 more sub-tabs. If the tabs load at the same time when the user enters the app, it will cause the app to freeze for a long time.

So maybe you just have to load each tab once the tab is clicked to display but when you come back, do you keep the state of that tab?

It is entirely possible that now I will share with everyone a way to solve this rather difficult problem. It is a combination of ViewStub and ViewPager.

2. Introducing too much to ViewStub

The ViewStub is an invisible View, the size is “zero-size” and when needed a certain view will be created. When the ViewStub is set to “visible” or when the inflate () method is called, a View If desired, the ViewStub will replace itself by the View (or the View) that has been inflated.Therefore, ViewStub only exists in the system until the setVisibility (int) or inflate () methods are allowed. The added view will follow the layout parameters of the previously defined ViewStub, so this is also a way that can help improve the performance of the application – only create when needed.

Defining a ViewStub:

And use ViewStub

At a glance, many will feel that ViewStub will be similar to Include . But actually Include is just dividing the layout of the xml code into many different files for visualization and design. As for drawing on the view, it is still normal and ViewStub is different. It is not added. It is only uploaded when you actually use it as “visible” or when the inflate () method is called.

This is a good optimization because you can have complex layouts with tons of small views, complex views or titles anywhere and still have your activity uploading very fast.

2.1 Important methods of ViewStub:

  1. getInflatedId (): This method is used to get the id of ViewStub
  2. setLayoutResource (int layoutResource): This method is used to set the writing layout when StubbedView.
  3. getLayoutResource (): This method is used to get the layout that will be used by setsetVisibility (int) or Inflate () to replace this StubbedView in its parent with another view.
  4. inflate (): This method is used to display the layout defined by getLayoutResource () and replace this StubbedView in its parent with the specified layout resource.
  5. setVisibility (int visual): This method is used to display StubView. When visibility is set to VISIBLE or INVISIBLE
  6. setOnInflateListener (OnInflateListenerinflateListener): This method is used to listen for the event when we add ViewStub.

2.2 Attributes of ViewStub:

  1. id: This attribute is used to uniquely identify a ViewStub.
  2. inflatedId: This property is used to set the id of the added view.
  3. layout: This property is used to provide an identifier for the layout to be displayed when ViewStub is displayed or when forced to do so.

3. Use ViewStub in combination with ViewPager

Step 1: Build the main container containing ViewPager

Step 2: Build the framework that needs to be added to the ViewPager containing the ViewStub

fragment_viewstub.xml

Step 3: Process the logic of displaying the view when the perceived view is being displayed via userVisibleHint

BaseViewStubFragment.class

Step 4: And finally setLayoutResuorce for the view to display

HeavyFragment.class

Result

4. Summary

Above are some basic introduction about ViewStub and tip in handling performance improvement on Android application combining ViewPage and ViewStub

Hopefully these shares will help you in your real project !! People can refer to the project here

Reference source

Share the news now

Source : Viblo