xml 带有CardView项目的Kotlin RecyclerView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 带有CardView项目的Kotlin RecyclerView相关的知识,希望对你有一定的参考价值。

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView

class HorizontalAdapter(private val titles: Array<String>) : RecyclerView.Adapter<ViewHolder>() {

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.trade_current_item, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val title = titles[position]
        holder.title.text = title

        holder.title.setOnClickListener({
            Toasty.normal(holder.title.context, "Clicou no item " + holder.title.text).show()
        })
    }

    override fun getItemCount(): Int {
        return titles.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.title) as TextView

    }
}
// CardView
implementation"com.android.support:cardview-v7:27.1.1"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</RelativeLayout>
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val titles = arrayOf("Android", "Beta", "Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow", "Nougat", "Oreo")
        val adapter = MyAdapter(titles)
        val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        currentTradesRecyclerView.layoutManager = layoutManager
        currentTradesRecyclerView.adapter = adapter

    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:maxHeight="250dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            tools:text="Titulo" />

        <TextView
            android:id="@+id/item_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            tools:text="Descrição" />

    </LinearLayout>

</android.support.v7.widget.CardView>

以上是关于xml 带有CardView项目的Kotlin RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章

使用 Kotlin 在 CardView 上设置 OnClickListener? [复制]

在ConstraintLayout中为CardView设置约束参数

如何在recyclerview中为cardview android设置带有int数组的onclicklistener

如何在Android中加载带有动画的cardview GridView?

回收站视图滚动不顺畅 - Cardview

RecyclerView可扩展的cardView