{"id":504,"date":"2023-11-22T21:52:34","date_gmt":"2023-11-22T21:52:34","guid":{"rendered":"https:\/\/javigomez.org\/?p=504"},"modified":"2025-09-29T20:33:55","modified_gmt":"2025-09-29T20:33:55","slug":"kotlin-10-02-recyclerview-sencillo-solo-un-texto","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2023\/11\/22\/kotlin-10-02-recyclerview-sencillo-solo-un-texto\/","title":{"rendered":"10.02. Kotlin- RecyclerView sencillo (solo un texto)"},"content":{"rendered":"\n<p>Se construye un RecyclerView a partir de la informaci\u00f3n de una lista de cadenas.<\/p>\n\n\n\n<p>En la pantalla se pone la View del recyclerview y tambi\u00e9n se tiene que crear la activity que se va a repetir en la lista<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>activity_main.xml\n\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"&gt;\n\n    &lt;TextView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Clientes\"\n        android:textSize=\"30dp\"\n        android:layout_gravity=\"center\"\/&gt;\n\n    &lt;androidx.appcompat.widget.SearchView\n        android:id=\"@+id\/svBuscador\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        app:iconifiedByDefault=\"false\"\/&gt;\n\n    &lt;androidx.recyclerview.widget.RecyclerView\n        android:id=\"@+id\/rvClientes\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"\/&gt;\n&lt;\/LinearLayout&gt;\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>activity_item_client.xml\n\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"wrap_content\"\n    android:orientation=\"vertical\"&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/tvNombre\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:layout_margin=\"10dp\"\n        android:padding=\"10dp\"\n        android:text=\"Prueba\"\n        android:textColor=\"@color\/white\"\n        android:background=\"@color\/black\"\/&gt;\n&lt;\/LinearLayout&gt;\n<\/code><\/pre>\n\n\n\n<p>Se crea el adaptador que ser\u00e1 el encargado de mandar a la view todos los elementos de activity_item_client<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ClientAdapter.kt\n\npackage com.javi.u10c\n\nimport android.util.Log\nimport android.view.LayoutInflater\nimport android.view.View\nimport android.view.ViewGroup\nimport android.widget.TextView\nimport androidx.recyclerview.widget.RecyclerView\n\nclass ClientAdapter: RecyclerView.Adapter&lt;ClientAdapter.ViewHolder&gt;() {\n\n    private var clientList: List&lt;String&gt; = emptyList() \/\/Lista de informaci\u00f3n a visualizar\n\n    fun setData(clients: List&lt;String&gt;){ \/\/Funci\u00f3n que se llama desde MainActivity donde se manda los datos\n        clientList=clients\n        notifyDataSetChanged()\n    }\n\n        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ClientAdapter.ViewHolder {\n            val view= LayoutInflater.from(parent.context).inflate(R.layout.activity_item_cliente,parent,false)\n            return ViewHolder(view)\n    }\n\n    override fun getItemCount(): Int {\n        return clientList.size\n    }\n\n    override fun onBindViewHolder(holder: ViewHolder, position: Int) {\n        var client = clientList&#91;position]\n        holder.bind(client) \/\/por cada elemento, llama a la funci\u00f3n que lo construye\n    }\n\n    class ViewHolder(view: View): RecyclerView.ViewHolder(view) { \n        val tvNombre=view.findViewById&lt;TextView&gt;(R.id.tvNombre)\n        fun bind(client: String){ \/\/construye cada elemento\n            tvNombre.text=client\n        }\n\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>En el MainActivity.kt se crea el adaptador, se dice el aspecto de ese adaptador y se asocia al RecyclerView.<\/p>\n\n\n\n<p>Por \u00faltimo, se mandan los datos que se tiene que visualizar<\/p>\n\n\n\n<p>package com.javi.u10c<\/p>\n\n\n\n<p>import androidx.appcompat.app.AppCompatActivity<br>import android.os.Bundle<br>import androidx.recyclerview.widget.LinearLayoutManager<br>import androidx.recyclerview.widget.RecyclerView<\/p>\n\n\n\n<p>class MainActivity : AppCompatActivity() {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>override fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n    setContentView(R.layout.activity_main)\n\n    var rvClientes = findViewById&lt;RecyclerView&gt;(R.id.rvClientes)\n    var adapter = ClientAdapter()\n\n    rvClientes.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)\n    rvClientes.adapter = adapter\n\n    var listaClientes: List&lt;String&gt; = listOf(\"Uno\", \"Dos\", \"Tres\", \"Cuatro\")\n    adapter.setData(listaClientes)\n\n\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/javigomez.org\/wp-content\/uploads\/2023\/11\/image-25-1-12-1-1.png\" alt=\"\" class=\"wp-image-505\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Kotlin U10 02 RecyclerView String\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/9fT4XOHEwss?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Se construye un RecyclerView a partir de la informaci\u00f3n de una lista de cadenas. En la pantalla se pone la View del recyclerview y tambi\u00e9n &hellip; <\/p>\n","protected":false},"author":1,"featured_media":347,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[19,212,284],"class_list":["post-504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin","tag-adaptador","tag-kotlin","tag-recyclerview"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=504"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":922,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/504\/revisions\/922"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/347"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}