简体   繁体   English

RecyclerView.Adapter 问题绑定

[英]RecyclerView.Adapter Issue Binding

Six hours and I cannot figure out what is going on, I have a simplest Adapter and an Activity六个小时,我不知道发生了什么,我有一个最简单的Adapter和一个Activity

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>

In OnCreateOnCreate

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 is also as simple as it gets 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);
        }
    }

Where 'item_story_comment.xml' “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>

When I RV.SetAdapter(ad2);当我RV.SetAdapter(ad2); , it never binds, I never see any data or see the debug messages ,它永远不会绑定,我永远不会看到任何数据或看到debug消息

I have so many RecyclerViews in this app and all working fine, I feel like I am making some simple or dumb mistake but cannot figure it out what.我在这个应用程序中有很多 RecyclerViews 并且一切正常,我觉得我犯了一些简单或愚蠢的错误,但无法弄清楚是什么。

Any idea?任何想法?

A RecyclerView needs both Adapter and LayoutManager to show up its items. RecyclerView需要AdapterLayoutManager来显示它的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM