我是Kotlin的新手,现在正尝试制作一个可以在在线课程上显示俱乐部名单回收者视图的应用程序。如果我单击该项目之一,则应转到新活动,该活动可以显示有关该项目的详细信息。它应该使用anko公共空间和布局(意图和详细信息布局)。听起来很简单,但我无法确定recyclerview物品的位置并获得意向性的附加物品,之后,我必须将附加物品放入Details活动中并进行显示。我真的需要你的帮助。这是我的代码:
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var items: MutableList<Item> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initData()
club_list.layoutManager = LinearLayoutManager(this)
club_list.adapter = RecyclerViewAdapter(this, items)
}
private fun initData(){
val name = resources.getStringArray(R.array.club_name)
val image = resources.obtainTypedArray(R.array.club_image)
items.clear()
for(i in name.indices){
items.add(Item(name[i],
image.getResourceId(i, 0)))
}
image.recycle()
}
}
Item.kt
data class Item (val name: String?, val image: Int?)
RecyclerViewAdapter.kt
class RecyclerViewAdapter(private val context: Context, private val items: List<Item>)
: RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_list, parent, false))
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItem(items[position])
}
override fun getItemCount(): Int = items.size
class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
init{
view.setOnClickListener {
view.context.startActivity(view.context.intentFor<DetailsActivity>("name" to ""))
}
}
fun bindItem(items: Item) {
itemView.name.text = items.name
Glide.with(itemView.context).load(items.image).into(itemView.image)
}
}
} DetailsActivity.kt
class DetailsActivity : AppCompatActivity() {
private var name: String = ""
private var desc: String = ""
lateinit var nameTextView: TextView
lateinit var descTextView: TextView
lateinit var imgPhotos: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
verticalLayout {
topPadding = 20
gravity = Gravity.CENTER_HORIZONTAL
/*imgPhotos = imageView(R.drawable.ic_launcher_background).
lparams(width= matchParent) {
padding = dip(20)
margin = dip(15)
}*/
// TextView nama tim
nameTextView = textView {
text = "ContohTeam FC"
textSize = 36F
typeface = Typeface.DEFAULT_BOLD
}.lparams {
width = matchParent
}
//TExtView deskripsi tim
/*descTextView = textView {
text = "insert some random text here"
textSize = 12F
}.lparams {
width = matchParent
}*/
}
val intent = intent
name = intent.getStringExtra("name")
//desc = intent.getStringExtra("desc")
nameTextView.text = name
//descTextView.text = desc
}
}
谢谢:)
答案 0 :(得分:1)
通过适配器位置:
class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
init{
view.setOnClickListener {
view.context.startActivity<MyActivity>("key" to getAdapterPosition())
}
}
}
要检索您的活动
val position = intent!!.extras.getInt("key")
UPD : 与其他任何数据完全相同:
class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
fun bindItem(items: Item) {
itemView.name.text = items.name
Glide.with(itemView.context).load(items.image).into(itemView.image)
view.setOnClickListener {
view.context.startActivity<MyActivity>("image" to items.image, "name" to items.name)
}
}
}