英文:
How to inflate a Fragment (List) that was created by the Android studio wizard in an Activity?
问题
在Android Studio中(版本4.0.1),选择 文件 -> 新建 -> Fragment -> Fragment(列表),会创建一个带有所有所需组件(适配器、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" />
</LinearLayout>
我无法弄清楚如何在我的Activity中填充该Fragment,我尝试过:
getSupportFragmentManager().beginTransaction().add(R.id.list, new ItemFragment()).commit();
或者
getSupportFragmentManager().beginTransaction().add(R.id.list, ItemFragment.newInstance(1)).commit();
我知道错误在于'R.id.list',因为我收到以下堆栈跟踪错误:
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,我在其他答案中看到过这种方法,但也没有奏效。
那么,我应该如何在我的Activity中填充由向导创建的Fragment(列表),为什么我的方法不起作用呢?
英文:
In Android Studio (v.4.0.1) File -> New -> Fragment -> Fragment (List)
creates a Dummy List with all components needed (Adapter, XMLs etc). When leaving the default names given by the wizard it creates these xml files :
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" />
and 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" />
</LinearLayout>
I can´t figure out how to inflate the Fragment in my Activity, I tried with :
getSupportFragmentManager().beginTransaction().add(R.id.list, new ItemFragment()).commit();
or
getSupportFragmentManager().beginTransaction().add(R.id.list, ItemFragment.newInstance(1)).commit();
where I know the error lies within 'R.id.list' because I get the following stacktrace error :
java.lang.IllegalArgumentException: No view found for id 0x7f....:id/list) for fragment ItemFragment ...
'R.id.list' is the ID given to the RecyclerView by the wizard.
I also tried wrapping fragment_item_list.xml and fragment_item.xml into a FrameLayout and calling R.id.blabla from within the Framelayout, I saw this in other answers, but that did not work either.
So how do I inflate that Wizard created Fragment (List) in my Activity and why does my approach not work?
答案1
得分: 1
你的Activity
布局中应该有一些ViewGroup
,例如RelativeLayout
,它将成为你的Fragment
的容器。你将RecyclerView
设置为容器,但这样做是行不通的,因为这个元素不是适当的容器,而且根本没有添加到Activity
中(因此在异常中会出现“找不到ID为...的视图”)。RecyclerView
是Fragment
布局的一部分(在创建和添加之后),在其中你已经实现了Adapter
。当Fragment
运行时将会可用,你在尝试添加Fragment
的代码行上发生了崩溃。
将.add(R.id.list, new ItemFragment())
中的R.id.list
替换为你在Activity
中填充的容器ID(即传递给setContentView
的布局)。
英文:
you should have some ViewGroup
in your Activity
s layout, e.g. RelativeLayout
, which would be container for your Fragment
. You are setting RecyclerView
as container, this wont work as this item isn't proper container and isn't added to Activity
at all (so No view found for id
in Exception
). RecyclerView
is a part of Fragment
s layout (after creating and adding), in which you have implemented Adapter
. Will be available when Fragment
will run and you are crashing on line which tries to add Fragment
.add(R.id.list, new ItemFragment())
- replace R.id.list
with your container id inflated in Activity
(layout passed to setContentView
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论