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。

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题