简体   繁体   English

如何膨胀由 Android Studio 向导在活动中创建的片段(列表)?

[英]How to inflate a Fragment (List) that was created by the Android studio wizard in an Activity?

In Android Studio (v.4.0.1) File -> New -> Fragment -> Fragment (List) creates a Dummy List with all components needed (Adapter, XMLs etc).在 Android Studio (v.4.0.1) File -> New -> Fragment -> Fragment (List) 中创建一个包含所有所需组件(适配器、XML 等)的虚拟列表。 When leaving the default names given by the wizard it creates these xml files :当保留向导给出的默认名称时,它会创建这些 xml 文件:

fragment_item_list.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" />

and fragment_item.xml :和 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" />

I can´t figure out how to inflate the Fragment in my Activity, I tried with :我不知道如何在我的 Activity 中给 Fragment 充气,我尝试了:

 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 :我知道错误在“R.id.list”中,因为我收到以下堆栈跟踪错误:

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. 'R.id.list' 是向导赋予 RecyclerView 的 ID。

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.我还尝试将 fragment_item_list.xml 和 fragment_item.xml 包装到 FrameLayout 并从 Framelayout 中调用 R.id.blabla,我在其他答案中看到了这一点,但这也不起作用。

So how do I inflate that Wizard created Fragment (List) in my Activity and why does my approach not work?那么我如何在我的活动中增加向导创建的片段(列表),为什么我的方法不起作用?

you should have some ViewGroup in your Activity s layout, eg RelativeLayout , which would be container for your Fragment .你应该在你的Activity的布局中有一些ViewGroup ,例如RelativeLayout ,这将是你的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设置为容器,这将不起作用,因为该项目不是正确的容器,并且根本没有添加到Activity (因此在Exception No view found for id )。 RecyclerView is a part of Fragment s layout (after creating and adding), in which you have implemented Adapter . RecyclerViewFragment布局的一部分(在创建和添加之后),你已经在其中实现了Adapter Will be available when Fragment will run and you are crashing on line which tries to add Fragment将在Fragment运行并且您在线崩溃并尝试添加Fragment时可用

.add(R.id.list, new ItemFragment()) - replace R.id.list with your container id inflated in Activity (layout passed to setContentView ) .add(R.id.list, new ItemFragment()) - 用你在Activity膨胀的容器 id 替换R.id.list (布局传递给setContentView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM