SLaks 2013-09-13 05:30 采纳率: 0%
浏览 954

如何添加一个 inline ListFragment?

我有如下的 layout,包含一些 TextView 和 ImageView。下面的代码我想添加一个ListFragment。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name: "
        android:id="@+id/textView"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/name"
        android:layout_toRightOf="@+id/textView"
        android:layout_gravity="left"
        android:text="Large Text" />
...
    <fragment android:id="@+id/list"
        android:layout_below="@id/name"
        class="com.snot.bodyweightworkout.ExerciseListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

现在我想使用下面的代码添加 ListFragment。但是我想向上面描述的添加。我猜我应该用相同的方法添加,但是在一个container 的布局中我应该使用什么元素呢?

FragmentManager fm = getSupportFragmentManager();
if (fm.findFragmentById(android.R.id.content) == null) {
    ProgramListFragment list = new ProgramListFragment();
    fm.beginTransaction().add(android.R.id.content, list).commit();
}

如何在 inline 中添加?

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-02-10 11:00
    关注

    该回答引用ChatGPT

    您可以在您的布局文件中添加一个容器,例如 FrameLayout,并将 ListFragment 添加到该容器中,如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name: "
        android:id="@+id/textView"
        android:layout_gravity="left|center_vertical" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/name"
        android:layout_toRightOf="@+id/textView"
        android:layout_gravity="left"
        android:text="Large Text" />
    ...
    <FrameLayout
         android:id="@+id/list_container"
         android:layout_below="@id/name"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    </RelativeLayout>
    

    然后,在您的代码中添加如下代码:

    FragmentManager fm = getSupportFragmentManager();
    
    if (fm.findFragmentById(R.id.list_container) == null) {
        ProgramListFragment list = new ProgramListFragment();
        fm.beginTransaction()
            .add(R.id.list_container, list)
            .commit();
    }
    

    这样,您就可以通过使用具有唯一 ID 的容器,而不是 android.R.id.content 来动态添加您的 ListFragment。

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!