Advertisement
Guest User

DecorableFragment

a guest
Jan 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.63 KB | None | 0 0
  1. abstract class DecorableFragment: Fragment() {
  2.  
  3.     private lateinit var topContent: LinearLayout
  4.     private lateinit var content: LinearLayout
  5.     private lateinit var bottomContent: LinearLayout
  6.  
  7.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  8.         val view = inflater.inflate(R.layout.fragment_decorable, container, false)
  9.  
  10.         topContent = view.findViewById(R.id.fragment_decorable_top_content)
  11.         content = view.findViewById(R.id.fragment_decorable_content)
  12.         bottomContent = view.findViewById(R.id.fragment_decorable_bottom_content)
  13.         onCreateContent(inflater, content, savedInstanceState)
  14.         onCreateDecoration(DecorationInstaller(topContent), DecorationInstaller(bottomContent), savedInstanceState)
  15.  
  16.         return view
  17.     }
  18.  
  19.     open fun onCreateContent(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) {
  20.     }
  21.  
  22.     open fun onCreateDecoration(
  23.             topInstaller: DecorationInstaller,
  24.             bottomInstaller: DecorationInstaller,
  25.             savedInstanceState: Bundle?
  26.     ) {
  27.     }
  28.  
  29.     class DecorationInstaller (private val container: ViewGroup) {
  30.  
  31.         private fun ViewGroup.addDecoration(decoration: Decoration): View {
  32.             if(!decoration.initialized) {
  33.                 decoration.create(LayoutInflater.from(context), this)
  34.             }
  35.  
  36.             addView(decoration.decorationView)
  37.             return decoration.decorationView!!
  38.         }
  39.  
  40.         fun add(decoration: Decoration): View {
  41.             return container.addDecoration(decoration)
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement