Home > Back-end >  In Kotlin RecyclerView items not displayed
In Kotlin RecyclerView items not displayed

Time:03-28

I am a beginner Kotlin student and am following an example RecyclerView. I start the coding but do not get the results I should be getting, even after checking and rechecking the code. Then I notice that even with very basic code it still does not behave as it should. I am going to include the basic code, which should display a generic list when tools:listitem is used. I only get 1 item in the list. I am suspecting that something other than the code is affecting the outcome; however I am not at a level to know.

Please see the Activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/recyclerview_item"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@ id/recycler_view"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Notice it has the tools:listitem line.

Now here is the list (recyclerview_item.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:id="@ id/titleTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Title"/>

    <TextView
        android:id="@ id/timeTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12:42"/>

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="#C3C3C3"/>

</LinearLayout>

Originally it started off as a constraintlayout, but I read somewhere that a LinearLayout could be the cure.

Then the MainActivity.kt is the standard when you start a project. Nothing more nothing less:

package com.example.recyclerexample2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

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

This is a capture showing the design of the xml file showing just 1 item, instead of being completely populated with items.

This is how it looks after listitem(only item displayed)

and this is before adding the listitem line in the xml file.

without listitem

I am using: Android Studio Arctic Fox | 2020.3.1 Patch 4 Build #AI-203.7717.56.2031.7935034, built on November 21, 2021 Runtime version: 11.0.10 0-b96-7249189 amd64 VM: OpenJDK 64-Bit Server VM by Oracle Corporation Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 12 Registry: external.system.auto.import.disabled=true Non-Bundled Plugins: com.intellij.marketplace, com.thoughtworks.gauge

Any help will be very appreciated.

Ray

CodePudding user response:

enter image description here

enter image description here

You should use "wrap_content" for your list items

  • Related