Android 回收器视图未在AVD上生成

Android 回收器视图未在AVD上生成,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,希望你们都好 Android开发的新手。我正在试用RecyclerViews,但它根本无法渲染。我是否错过了某一步 这是我的主要活动kotlin文件: package com.example.alphablog import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import android

希望你们都好

Android开发的新手。我正在试用RecyclerViews,但它根本无法渲染。我是否错过了某一步

这是我的主要活动kotlin文件:

package com.example.alphablog

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.alphablog.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val rv = findViewById<RecyclerView>(R.id.recycler_view_main)
        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = PostAdapter()
    }
}
package com.example.alphablog
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入androidx.recyclerview.widget.LinearLayoutManager
导入androidx.recyclerview.widget.recyclerview
导入com.example.alphablog.databinding.ActivityMainBinding
类MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv=findViewById(R.id.recycler\u view\u main)
rv.layoutManager=LinearLayoutManager(此)
rv.adapter=PostAdapter()
}
}
以下是我的主要活动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/recycler_view_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:i`enter code here`temCount="30"
        tools:listitem="@layout/recycler_view_item" />

</androidx.constraintlayout.widget.ConstraintLayout>

以下是我的回收器视图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="wrap_content">

  <ImageView
      android:id="@+id/imageView2"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:src="@drawable/ic_baseline_add_business_24"
      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"
      android:text="Title of Blog Post"
      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"
      android:text="Body of first blog post"
      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"
      android:text="Edit and Delete action"
      app:layout_constraintTop_toTopOf="@+id/imageView2"
      app:layout_constraintEnd_toEndOf="parent"
      android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

最后,这里是我的适配器类锅炉板:

package com.example.alphablog

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 { // creates view to display
        val postView = LayoutInflater.from(parent.context).inflate(R.layout.post, parent,false)
        return PostViewHolder(postView)
    }

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

    override fun getItemCount(): Int { // amount of items you will be working with in your recycler view
        return 30
    }

}
package com.example.alphablog
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入androidx.recyclerview.widget.recyclerview
类PostAdapter:RecyclerView.Adapter(){
类PostViewHolder(postView:View):RecyclerView.ViewHolder(postView){
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):PostViewHolder{//创建要显示的视图
val postView=LayoutInflater.from(parent.context)。充气(R.layout.post,parent,false)
返回后视支架(后视)
}
覆盖BindViewHolder(holder:PostViewHolder,位置:Int){
}
override fun getItemCount():Int{//您将在回收器视图中处理的项目数量
返回30
}
}

您需要为适配器充电。。并将这些数据映射到
onBindViewHolder()
适配器回调中的
ViewHolder
视图。您不必这样做,此代码仍应显示30个相同的项目。你检查过布局检查器了吗?@HenryTwist:对不起,我是Android开发新手,还没有使用布局检查器。我将尝试看看这是否带来了什么。@HenryTwist:我没有看到任何布局出现在Inspector的组件部分,这很奇怪……我刚刚复制了您的代码,它工作得非常好。是否确实显示了
main活动