如何在Activity中膨胀由Android Studio向导创建的Fragment(列表)?

huangapple go评论65阅读模式
英文:

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 :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.recyclerview.widget.RecyclerView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/list&quot;
    android:name=&quot;de.afu.development.test.afu_portfolio_searcher_app_test.ItemFragment&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_marginLeft=&quot;16dp&quot;
    android:layout_marginRight=&quot;16dp&quot;
    app:layoutManager=&quot;LinearLayoutManager&quot;
    tools:context=&quot;.ItemFragment&quot;
    tools:listitem=&quot;@layout/fragment_item&quot; /&gt;

and fragment_item.xml :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;horizontal&quot;&gt;

&lt;TextView
    android:id=&quot;@+id/item_number&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_margin=&quot;@dimen/text_margin&quot;
    android:textAppearance=&quot;?attr/textAppearanceListItem&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/content&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_margin=&quot;@dimen/text_margin&quot;
    android:textAppearance=&quot;?attr/textAppearanceListItem&quot; /&gt;

</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为...的视图”)。RecyclerViewFragment布局的一部分(在创建和添加之后),在其中你已经实现了Adapter。当Fragment运行时将会可用,你在尝试添加Fragment的代码行上发生了崩溃。

.add(R.id.list, new ItemFragment())中的R.id.list替换为你在Activity中填充的容器ID(即传递给setContentView的布局)。

英文:

you should have some ViewGroup in your Activitys 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 Fragments 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)

huangapple
  • 本文由 发表于 2020年9月7日 18:41:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63775969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定