Guest User

Untitled

a guest
Jun 18th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.95 KB | None | 0 0
  1. package simplegyphy.view
  2.  
  3. import android.os.Bundle
  4. import android.support.v7.app.AppCompatActivity
  5. import android.support.v7.widget.RecyclerView
  6. import android.support.v7.widget.StaggeredGridLayoutManager
  7. import android.util.Log
  8. import android.view.ViewGroup
  9. import android.widget.LinearLayout
  10. import kotlinx.coroutines.experimental.android.UI
  11. import kotlinx.coroutines.experimental.launch
  12. import org.jetbrains.anko.button
  13. import org.jetbrains.anko.doAsync
  14. import org.jetbrains.anko.editText
  15. import org.jetbrains.anko.linearLayout
  16. import org.jetbrains.anko.sdk25.coroutines.onClick
  17.  
  18. class MainActivity : AppCompatActivity() {
  19.  
  20.     var recyclerView: RecyclerView? = null
  21.  
  22.     override fun onCreate(savedInstanceState: Bundle?) {
  23.         super.onCreate(savedInstanceState)
  24.  
  25.         Log.isLoggable("MainActivity", Log.DEBUG)
  26.  
  27.         val context = this
  28.         val recyclerViewGifAdapter = RecyclerViewGifAdapter(context)
  29.  
  30.         CreateView(context, recyclerViewGifAdapter)
  31.  
  32.         doAsync {
  33.             val trendingUrls = Giphy("http://api.giphy.com", "dc6zaTOxFJmzC")
  34.                     .trending()
  35.                     .getOriginalImages()
  36.  
  37.             launch(UI) {
  38.                 recyclerViewGifAdapter.records = trendingUrls
  39.             }
  40.         }
  41.     }
  42.  
  43.     private fun CreateView(context: MainActivity, recyclerViewGifAdapter: RecyclerViewGifAdapter) {
  44.         linearLayout {
  45.             orientation = LinearLayout.VERTICAL
  46.             val editText = editText {
  47.                 hint = "Запрос"
  48.             }
  49.             button {
  50.                 text = "Go"
  51.                 onClick {
  52.                     doAsync {
  53.                         val queryText = editText.text.toString()
  54.                         if (queryText.isNotEmpty()) {
  55.                             val foundedGifUrls = Giphy("http://api.giphy.com", "dc6zaTOxFJmzC")
  56.                                     .search(queryText)
  57.                                     .getOriginalImages()
  58.  
  59.                             launch(UI) {
  60.                                 val adapter = RecyclerViewGifAdapter(context)
  61.                                 recyclerView!!.adapter = adapter
  62.                                 adapter.records = foundedGifUrls
  63.                             }
  64.                         }
  65.                     }
  66.  
  67.                 }
  68.             }
  69.             recyclerView = recyclerView {
  70.                 val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
  71.                 staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
  72.                 layoutManager = staggeredGridLayoutManager
  73.                 adapter = recyclerViewGifAdapter
  74.                 layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
  75.                 setHasFixedSize(true)
  76.                 itemAnimator = null
  77.             }
  78.         }
  79.     }
  80. }
Add Comment
Please, Sign In to add comment