如何在Activity中为Android Studio向导创建的片段列表充气?

如何解决如何在Activity中为Android Studio向导创建的片段列表充气?

在Android Studio(v.4.0.1)中-文件->新建->片段->片段(列表) 创建一个具有所有所需组件(适配器,XML等)的虚拟列表。当保留向导给出的名称时,它将创建以下xml文件

fragment_item_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/list"
    android:name="de.afu.development.test.afu_portfolio_searcher_app_test.ItemFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="linearlayoutmanager"
    tools:context=".ItemFragment"
    tools:listitem="@layout/fragment_item" />

和fragment_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<TextView
    android:id="@+id/item_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:textAppearance="?attr/textAppearanceListItem" />

<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:textAppearance="?attr/textAppearanceListItem" />

我尝试用:

 getSupportFragmentManager().beginTransaction().add(R.id.list,new ItemFragment()).commit();

 getSupportFragmentManager().beginTransaction().add(R.id.list,ItemFragment.newInstance(1)).commit();

我知道该错误位于'R.id.list'内,因为我收到以下stacktrace错误

java.lang.IllegalArgumentException: No view found for id 0x7f....:id/list) for fragment ItemFragment ...

“ R.id.list”是向导提供给RecyclerView的ID。

我还尝试将fragment_item_list.xml和fragment_item.xml包装到一个FrameLayout中,然后从Framelayout中调用R.id.blabla,我在其他答案中也看到了这一点,但这也不起作用。

那我该如何在该向导中为该向导创建的Fragment(列表)充气,为什么我的方法无效?

解决方法

ViewGroup的布局中应该有一些Activity,例如RelativeLayout,它是您Fragment的容器。您正在将RecyclerView设置为容器,因为该项目不是正确的容器,并且根本没有添加到Activity中(因此No view found for id中的Exception),所以此操作将无效。 RecyclerViewFragment布局的一部分(在创建和添加之后),您已经在其中实现了Adapter。将在Fragment运行并且您在试图添加Fragment

的行上崩溃时可用

.add(R.id.list,new ItemFragment())-将R.id.list替换为在Activity中膨胀的容器ID(布局已传递到setContentView

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?