android - RecyclerView.Adapter 问题绑定
问题描述
六个小时,我无法弄清楚发生了什么,我有一个最简单Adapter
的Activity
活动
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_stories_comments1"
android:background="@color/_light_green"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
/>
</LinearLayout>
在OnCreate
var RV = FindViewById<AndroidX.RecyclerView.Widget.RecyclerView>(Resource.Id.rv_stories_comments1);
var ad2 = new Stories_Comments_Adapter(comments);
RV.SetAdapter(ad2);
Stories_Comments_Adapter
也很简单
internal class Comment_ViewHolder : RecyclerView.ViewHolder
{
public TextView username { get; set; }
public Comment_ViewHolder(View itemView) : base(itemView)
{
username = itemView.FindViewById<TextView>(Resource.Id.story_comment_username);
}
}
public class Stories_Comments_Adapter : RecyclerView.Adapter
{
private List<CommentItem> comments;
public Stories_Comments_Adapter(List<CommentItem> comments)
{
this.comments = comments;
}
public override int ItemCount => comments.Count;
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
Android.Util.Log.Debug("CommentsDebug", "OnBind");
var Current_Item = comments[position];
Comment_ViewHolder viewHolder = holder as Comment_ViewHolder;
viewHolder.username.Text = Current_Item.name;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
Android.Util.Log.Debug("CommentsDebug", "OnCreate");
LayoutInflater inflater = LayoutInflater.From(parent.Context);
View itemView = inflater.Inflate(Resource.Layout.item_story_comment, parent, false);
return new ChatViewHolder(itemView);
}
}
“item_story_comment.xml”在哪里
<RelativeLayout
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:background="?attr/colorBackground"
android:orientation="vertical">
<TextView
android:id="@+id/story_comment_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo_about"
android:textColor="@color/red"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
当 I 时RV.SetAdapter(ad2);
,它永远不会绑定,我永远不会看到任何数据或看到debug
消息
我在这个应用程序中有很多 RecyclerViews 并且一切正常,我觉得我犯了一些简单或愚蠢的错误,但无法弄清楚是什么。
任何的想法?
解决方案
ARecyclerView
既需要Adapter
又LayoutManager
需要展示它的物品。
推荐阅读
- c++ - push_back 使用原始指针与使用智能指针
- python - 我的带有聊天过滤器事件的 discord.py 机器人发送 2 条或更多消息而不是 1 条
- python - 图片无法在移动设备上加载
- php - Laravel 6.0 Eloquent - 按日期和状态排序
- java - 如何用另一种类型的二维数组的维度初始化一种类型的二维数组
- database - 了解 DISTINCT vs DISTINCT ON vs Group by
- mysql - 日期格式不正确
- node.js - 将变量作为值传递时,Model.findOne() 函数不起作用
- git - 哪些信息存储为每个 git commit 的树对象内容
- selenium-webdriver - 在 POM 中使用 sendkey 传递值时获取 NullPointerException