0

好吧,我知道如何在 android studio 中实现嵌套的 recyclerview,但我正在进行一项新工作,我们正在使用 xamarin .. 我在实现父适配器时遇到问题。有什么建议吗?

我试图制作 2 个 diffs recyclerview,一些谷歌搜索,什么都没有。

class ParentAdapter(private val parents : List<ParentModel>) :    
RecyclerView.Adapter<ParentAdapter.ViewHolder>(){
private val viewPool = RecyclerView.RecycledViewPool()
override fun onCreateViewHolder(parent: ViewGroup, 
                           viewType: Int): ViewHolder {
    val v = LayoutInflater.from(parent.context)
               .inflate(R.layout.parent_recycler,parent,false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
   return parents.size
}

override fun onBindViewHolder(holder: ViewHolder, 
                                  position: Int) {
    val parent = parents[position]
    holder.textView.text = parent.title
    val childLayoutManager = LinearLayoutManager(             
    holder.recyclerView.context, LinearLayout.HORIZONTAL, false)
    childLayoutManager.initialPrefetchItemCount = 4
    holder.recyclerView.apply {
    layoutManager = childLayoutManager
    adapter = ChildAdapter(parent.children)
    setRecycledViewPool(viewPool)}}


inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
    val recyclerView : RecyclerView = itemView.rv_child
    val textView:TextView = itemView.textView}
}

下面代码的问题在于 onBindViewHolder.. 在线性布局管理器中,我无法放置正确的上下文。

4

1 回答 1

1

我无法重复您的问题,这是我的简单示例:

外部recyclerview适配器:

class RecyclerAdapter : RecyclerView.Adapter
{
    // add this variable

    private List<ListItem> mEmails;    

    public RecyclerAdapter(List<ListItem> mEmails, Context context)
    {
        this.mEmails = mEmails;

    }

    public override int ItemCount
    {
        get { return mEmails.Count; }
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {        
            RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
            LinearLayoutManager mLayoutManager = new LinearLayoutManager(myHolder.mRc.Context, LinearLayoutManager.Horizontal, false);
            mLayoutManager.InitialPrefetchItemCount = 4;
            myHolder.mRc.SetLayoutManager(mLayoutManager);
            myHolder.mRc.SetAdapter(new MyRcAdapter());

            myHolder.mName.Text = ((Email)mEmails[position]).date;
            myHolder.mSubject.Text = ((Email)mEmails[position]).Subject;
            myHolder.mMessage.Text = ((Email)mEmails[position]).Message;          
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {

            View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);

            RecyclerViewHolder view = new RecyclerViewHolder(row);
            return view;
    }

    class RecyclerViewHolder1 : RecyclerView.ViewHolder
    {

        public TextView mName;
        public TextView mSubject;
        public TextView mMessage;      
        public RecyclerView mRc;
        public RecyclerViewHolder1(View itemView) : base(itemView)
        {
            mName = itemView.FindViewById<TextView>(Resource.Id.ore);
            mSubject = itemView.FindViewById<TextView>(Resource.Id.contratti);
            mMessage = itemView.FindViewById<TextView>(Resource.Id.resa);
            mRc = itemView.FindViewById<RecyclerView>(Resource.Id.inside_rc);

        }
    }
}     

和内部 recyclerview 适配器:

class MyRcAdapter : RecyclerView.Adapter
{
    private string[] ssss = new string[] { "1", "2", "3", "1", "2", "3" };
    public override int ItemCount => ssss.Length;

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        InsideHolder myHolder = holder as InsideHolder;
        myHolder.mName.Text = ssss[position];
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.inside, parent, false);
        InsideHolder  view = new InsideHolder(row);
        return view;
    }


    class InsideHolder : RecyclerView.ViewHolder
    {
        public TextView mName;
        public InsideHolder(View itemView) : base(itemView)
        {
            mName = itemView.FindViewById<TextView>(Resource.Id.inside);

        }
    }
}
于 2019-10-02T07:03:37.630 回答