Warm tip: This article is reproduced from serverfault.com, please click

Recyclerview view not populating

发布于 2020-11-28 17:10:08

I'm a Kotlin newbie learning how to create simple recyclerview apps. My code is supposed to list the integers 1..10 in vertically stacked cells. However, it only lists the first item. I've consulted several tutorials and reviewed my code several times(after long breaks), but I can't see anything wrong in my code.

I got the bright idea early today to print Log statements. Examining them, I note that my onBindViewHolder function is only called once. What blunder am I making?

Here is my log output:

D/QuoteAdapter: value is: 1
D/QuoteAdapter: index is: 0
D/QuoteAdapter: Size is: 10

my activity:

class MainActivity : AppCompatActivity() {
    lateinit var mRecyclerView: RecyclerView

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

        mRecyclerView = findViewById(R.id.recyclerView)
        mRecyclerView.layoutManager = LinearLayoutManager(this)

        mRecyclerView.adapter = QuoteAdapter()
        //mRecyclerView.setHasFixedSize(true)
    }
}

my adapter:

class QuoteAdapter : RecyclerView.Adapter<QuoteViewHolder>() {
    private val listOfInts = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    private val TAG = "QuoteAdapter"

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

    override fun getItemCount(): Int {
        Log.d(TAG, "Size is: ${listOfInts.size.toString()}")
        return listOfInts.size
    }

    override fun onBindViewHolder(holder: QuoteViewHolder, position: Int) {
        val item = listOfInts[position]
        Log.d(TAG, "value is: ${item.toString()}")
        Log.d(TAG, "index is: ${position.toString()}")
        holder.listTitle.text = item.toString()
    }
}

my viewholder:

class QuoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val listTitle = itemView.findViewById(R.id.itemString) as TextView
}

my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/itemString"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

my main layout is shown below:

<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:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Questioner
user1801060
Viewed
0
David Kroukamp 2020-11-29 02:32:37

In your "my layout" try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/itemString"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Notice layout_height of LinearLayout has changed to wrap_content

Also doubt you need the android:orientation="vertical" on your ViewHolders item xml unless you will add more than just 1 TextView in the future.