Android-为什么回收器视图没有显示在设备上。我认为我的代码是正确的,但是视图没有显示出来

Android-为什么回收器视图没有显示在设备上。我认为我的代码是正确的,但是视图没有显示出来,android,android-recyclerview,android-adapter,android-viewholder,android-adapterview,Android,Android Recyclerview,Android Adapter,Android Viewholder,Android Adapterview,我刚刚创建了一个简单的RecyclerView项目,它将显示30个基本内容。但当我运行该文件时,它没有显示任何内容。事实上,我的全部源代码都很好 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.c

我刚刚创建了一个简单的
RecyclerView
项目,它将显示30个基本内容。但当我运行该文件时,它没有显示任何内容。事实上,我的全部源代码都很好

活动\u 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:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="12"
        tools:listitem="@layout/recycler_view_item" />

</androidx.constraintlayout.widget.ConstraintLayout> 

<?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="wrap_content"
    android:padding="5dp"
    android:layout_marginBottom="5dp"
    >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        tools:src="@drawable/sunny"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Today is Sunny"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        app:layout_constraintTop_toTopOf="@+id/imageView2"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="285dp"
        android:layout_height="20dp"
        tools:text="Today is sunny day lets go there"
        app:layout_constraintBottom_toBottomOf="@+id/imageView2"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="0.050" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Actions"
        app:layout_constraintTop_toTopOf="@+id/imageView2"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
PostAdapter.kt

package com.saddyahmed.recyclerview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

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

        rv_main.layoutManager = LinearLayoutManager(this)
        rv_main.adapter = PostAdapter()
    }
}
package com.saddyahmed.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView


class PostAdapter : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {

    class PostViewHolder(postView: View) : RecyclerView.ViewHolder(postView){

    }

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

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {

    }

    override fun getItemCount(): Int {
        return 30
    }

}

package com.saddyahmed.recyclerview
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入androidx.recyclerview.widget.recyclerview
类PostAdapter:RecyclerView.Adapter(){
类PostViewHolder(postView:View):RecyclerView.ViewHolder(postView){
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):PostViewHolder{
val postView=LayoutInflater.from(parent.context).充气(R.layout.recycler\u view\u项目,parent,false)
返回后视支架(后视)
}
覆盖BindViewHolder(holder:PostViewHolder,位置:Int){
}
重写getItemCount():Int{
返回30
}
}
post.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:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="12"
        tools:listitem="@layout/recycler_view_item" />

</androidx.constraintlayout.widget.ConstraintLayout> 

<?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="wrap_content"
    android:padding="5dp"
    android:layout_marginBottom="5dp"
    >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        tools:src="@drawable/sunny"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Today is Sunny"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        app:layout_constraintTop_toTopOf="@+id/imageView2"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        android:layout_marginStart="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="285dp"
        android:layout_height="20dp"
        tools:text="Today is sunny day lets go there"
        app:layout_constraintBottom_toBottomOf="@+id/imageView2"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="0.050" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Actions"
        app:layout_constraintTop_toTopOf="@+id/imageView2"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

首先,在PostAdapter中添加一个构造函数,该构造函数接受数据并将数据放入PostAdapter,例如:

myDataList=…;//从某处获取数据
rv_main=后适配器(myDataList);
然后将其绑定到您的
onBindViewHolder

覆盖BindViewHolder(holder:PostViewHolder,position:Int){
holder.bindData(myDataList.get(position));
}

另外,
getItemCount()
应该返回
myDataList.size()

是否检查了数据是否在那里???是的,它在那里但没有显示BindViewHolder()为空,这是获取数据并在布局上显示它们的方法。