Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package simplegyphy.view
- import android.os.Bundle
- import android.support.v7.app.AppCompatActivity
- import android.support.v7.widget.RecyclerView
- import android.support.v7.widget.StaggeredGridLayoutManager
- import android.util.Log
- import android.view.ViewGroup
- import android.widget.LinearLayout
- import kotlinx.coroutines.experimental.android.UI
- import kotlinx.coroutines.experimental.launch
- import org.jetbrains.anko.button
- import org.jetbrains.anko.doAsync
- import org.jetbrains.anko.editText
- import org.jetbrains.anko.linearLayout
- import org.jetbrains.anko.sdk25.coroutines.onClick
- class MainActivity : AppCompatActivity() {
- var recyclerView: RecyclerView? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- Log.isLoggable("MainActivity", Log.DEBUG)
- val context = this
- val recyclerViewGifAdapter = RecyclerViewGifAdapter(context)
- CreateView(context, recyclerViewGifAdapter)
- doAsync {
- val trendingUrls = Giphy("http://api.giphy.com", "dc6zaTOxFJmzC")
- .trending()
- .getOriginalImages()
- launch(UI) {
- recyclerViewGifAdapter.records = trendingUrls
- }
- }
- }
- private fun CreateView(context: MainActivity, recyclerViewGifAdapter: RecyclerViewGifAdapter) {
- linearLayout {
- orientation = LinearLayout.VERTICAL
- val editText = editText {
- hint = "Запрос"
- }
- button {
- text = "Go"
- onClick {
- doAsync {
- val queryText = editText.text.toString()
- if (queryText.isNotEmpty()) {
- val foundedGifUrls = Giphy("http://api.giphy.com", "dc6zaTOxFJmzC")
- .search(queryText)
- .getOriginalImages()
- launch(UI) {
- val adapter = RecyclerViewGifAdapter(context)
- recyclerView!!.adapter = adapter
- adapter.records = foundedGifUrls
- }
- }
- }
- }
- }
- recyclerView = recyclerView {
- val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
- staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
- layoutManager = staggeredGridLayoutManager
- adapter = recyclerViewGifAdapter
- layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
- setHasFixedSize(true)
- itemAnimator = null
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment