Created
September 8, 2018 17:01
-
-
Save khrlimam/116acb6915e41a229870e3a4859a331a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.submissions.dicoding.footballmatchschedule | |
import android.annotation.SuppressLint | |
import android.content.Context | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.support.v7.widget.LinearLayoutManager | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import org.jetbrains.anko.* | |
import org.jetbrains.anko.recyclerview.v7.recyclerView | |
class CobaRecyclerViewAnko : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
RecyclerView().setContentView(this) | |
} | |
class RecyclerView : AnkoComponent<Context> { | |
override fun createView(ui: AnkoContext<Context>): View = with(ui) { | |
return verticalLayout { | |
lparams(width = matchParent, height = matchParent) | |
recyclerView { | |
layoutManager = LinearLayoutManager(ctx) | |
adapter = MyAdapter(listOf("Anwar", "Aufa Ahdi", "Jihad", "Lian", "Bang Cepot", "Bang Rizal")) | |
}.lparams(width = matchParent, height = matchParent) | |
} | |
} | |
} | |
class MyAdapter(private val items: List<String>) : android.support.v7.widget.RecyclerView.Adapter<MyAdapter.VH>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { | |
val viewItem = MyViewHolderItem().createView(AnkoContext.create(parent.context, parent)) | |
val vh = VH(viewItem) | |
return vh | |
} | |
override fun getItemCount(): Int { | |
return items.size | |
} | |
override fun onBindViewHolder(holder: VH, position: Int) { | |
holder.textView.text = items[position] | |
} | |
class VH(val view: View) : android.support.v7.widget.RecyclerView.ViewHolder(view) { | |
@SuppressLint("ResourceType") | |
val textView: TextView = view.findViewById(1) | |
} | |
} | |
class MyViewHolderItem : AnkoComponent<ViewGroup> { | |
@SuppressLint("ResourceType") | |
override fun createView(ui: AnkoContext<ViewGroup>): View = with(ui) { | |
return linearLayout { | |
textView { | |
id = 1 | |
padding = dip(10) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment