Skip to content

Instantly share code, notes, and snippets.

@am5a03
Last active June 23, 2020 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am5a03/5c33532a81fc0e93e924bf5fb52ea910 to your computer and use it in GitHub Desktop.
Save am5a03/5c33532a81fc0e93e924bf5fb52ea910 to your computer and use it in GitHub Desktop.
BaseViewStubFragment.kt
package com.raymond.viewstubfragmentdemo
import android.os.Bundle
import androidx.annotation.CallSuper
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.ProgressBar
abstract class BaseViewStubFragment : Fragment() {
private var mSavedInstanceState: Bundle? = null
private var hasInflated = false
private var mViewStub: ViewStub? = null
private var visible = false
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_viewstub, container, false)
mViewStub = view.findViewById(R.id.fragmentViewStub) as ViewStub
mViewStub!!.layoutResource = getViewStubLayoutResource()
mSavedInstanceState = savedInstanceState
if (visible && !hasInflated) {
val inflatedView = mViewStub!!.inflate()
onCreateViewAfterViewStubInflated(inflatedView, mSavedInstanceState)
afterViewStubInflated(view)
}
return view
}
protected abstract fun onCreateViewAfterViewStubInflated(
inflatedView: View,
savedInstanceState: Bundle?
)
/**
* The layout ID associated with this ViewStub
* @see ViewStub.setLayoutResource
* @return
*/
@LayoutRes
protected abstract fun getViewStubLayoutResource(): Int
/**
*
* @param originalViewContainerWithViewStub
*/
@CallSuper
protected fun afterViewStubInflated(originalViewContainerWithViewStub: View?) {
hasInflated = true
if (originalViewContainerWithViewStub != null) {
val pb =
originalViewContainerWithViewStub.findViewById<ProgressBar>(R.id.inflateProgressbar)
pb.visibility = View.GONE
}
}
override fun onResume() {
super.onResume()
visible = true
if (mViewStub != null && !hasInflated) {
val inflatedView = mViewStub!!.inflate()
onCreateViewAfterViewStubInflated(inflatedView, mSavedInstanceState)
afterViewStubInflated(view)
}
}
override fun onDestroyView() {
super.onDestroyView()
hasInflated = false
}
override fun onPause() {
super.onPause()
visible = false
}
// Thanks to Noa Drach, this will fix the orientation change problem
override fun onDetach() {
super.onDetach()
hasInflated = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment