Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. fun ViewManager.appBarLayout(init: AppBarLayout.() -> Unit = {}) =
  2. __dslAddView({ AppBarLayout(it) }, init, this)
  3.  
  4. fun ViewManager.collapsingToolbarLayout(init: CollapsingToolbarLayout.() -> Unit = {}) =
  5. __dslAddView({ CollapsingToolbarLayout(it) }, init, this)
  6.  
  7. fun ViewManager.coordinatorLayout(init: CoordinatorLayout.() -> Unit = {}) =
  8. __dslAddView({ CoordinatorLayout(it) }, init, this)
  9.  
  10. fun ViewManager.floatingActionButton(init: FloatingActionButton.() -> Unit = {}) =
  11. __dslAddView({ FloatingActionButton(it) }, init, this)
  12.  
  13. fun ViewManager.navigationView(init: NavigationView.() -> Unit = {}) =
  14. __dslAddView({ NavigationView(it) }, init, this)
  15.  
  16. fun ViewManager.tabLayout(init: TabLayout.() -> Unit = {}) =
  17. __dslAddView({ TabLayout(it) }, init, this)
  18.  
  19. fun ViewManager.textInputLayout(init: TextInputLayout.() -> Unit = {}) =
  20. __dslAddView({ TextInputLayout(it) }, init, this)
  21.  
  22. fun View.snackbar(text: CharSequence, duration: Int = Snackbar.LENGTH_SHORT, init: Snackbar.() -> Unit = {}): Snackbar {
  23. val snack = Snackbar.make(this, text, duration)
  24. snack.init()
  25. snack.show()
  26. return snack
  27. }
  28.  
  29. fun View.snackbar(text: Int, duration: Int = Snackbar.LENGTH_SHORT, init: Snackbar.() -> Unit = {}): Snackbar {
  30. val snack = Snackbar.make(this, text, duration)
  31. snack.init()
  32. snack.show()
  33. return snack
  34. }
  35.  
  36. private object ViewCounter {
  37. private var viewCounter = AtomicInteger(1)
  38. public fun generateViewId(): Int {
  39. while (true) {
  40. val result = viewCounter.get()
  41. // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
  42. var newValue = result + 1
  43. if (newValue > 16777215) newValue = 1 // Roll over to 1, not 0.
  44. if (viewCounter.compareAndSet(result, newValue)) {
  45. return result
  46. }
  47. }
  48. }
  49. }
  50.  
  51. fun View.generateViewIdCompat(): Int {
  52. if (android.os.Build.VERSION.SDK_INT >= 19)
  53. return View.generateViewId()
  54. else
  55. return ViewCounter.generateViewId()
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement