【问题标题】:Adding a fragment to RecyclerView将片段添加到 RecyclerView
【发布时间】:2017-02-02 19:37:14
【问题描述】:

我有一个应用程序可以显示一条记录或它们的列表。我有一个单条记录的片段,所以我决定一个列表,我想用卡片填充 RecyclerView,并在每张卡片中添加一个片段。我收到 IllegalArgumentException: Cannot add a null child to a ViewGroup on a line in onBindViewHolder():

holder.frameLayout.addView(ResultFragment.newInstance(id).getView())

【问题讨论】:

  • Fragment 尚未初始化为调用 getView ,这在调用 onViewCreated 后可用,并且在将 Fragment 添加到 FragmentManager 后开始 Fragment 的整个生命周期,Fragment 不是为回收商意见。

标签: java android android-recyclerview fragment


【解决方案1】:

我不太确定如何使用回收视图和片段,但如果您使用线性布局或相对布局,您可以在 onCreateView 方法中使用 FragmentManager 或生成您的 pwn 方法来更改视图中的内容(片段)你想改变:

FragmentManager fm = getSupportFragmentManager();
  transaction = fm.beginTransaction();
  oneFragment = new OneFragment();
  transaction.replace(R.id.fragmentContent, oneFragment);
  transaction.commit();

R.id.fragmentManager 在您的视图中是 <fragment> 部分

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多