如果ID不是唯一的,就用片段替换容器[英] Fragment - replace container, if id is not unique

本文是小编为大家收集整理的关于如果ID不是唯一的,就用片段替换容器的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的布局具有两个具有相同ID的视图.如果我想找到视图,我只会调用parentView1.findViewById(R.id.content)或parentView2.findViewById(R.id.content)获取正确的视图.

如果我想用片段替换容器,我可以以某种方式定义我要替换哪个容器?<​​/p>

推荐答案

Fragment Manager仅接受ID,因此,我认为,答案是否,您无法指定容器.但是你仍然可以找到他们两个.

作为解决方法,您可以用片段包裹容器,然后在这些片段内部ID将是唯一的;因此,您可以做fragment1.replaceContent(...)之类的事情,其中​​.replaceContent(...)是您自己的方法. OFC,您需要确保您的参考正确,状态保存等.

其他推荐答案

您应该在父布局中包装相同的ID布局.然后将第二个容器的ID设置为其他整数.例如,布局文件看起来像这样:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parent_one">

    <include layout="@layout/container" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parent_two">

    <include layout="@layout/container"/>

</LinearLayout>
</LinearLayout>

您将首先使用父ID访问它们.然后将第二个容器的ID设置为其他容器.例如:

    val containerOneId = parent_one.container.id
    val containerTwoId = 1
    parent_two.container.id = containerTwoId

    openFragment(TestFragment().apply {
        val b = Bundle()
        b.putString(TestFragment.ARG, "Fragment one")
        arguments = b
    }, containerOneId)

    openFragment(TestFragment().apply {
        val b = Bundle()
        b.putString(TestFragment.ARG, "Fragment two")
        arguments = b
    }, containerTwoId)

相同的访问逻辑,但在Java中(如果您不使用Kotlin)

    LinearLayout parentOne = (LinearLayout) findViewById(R.id.parent_one);
    LinearLayout parentTwo = (LinearLayout) findViewById(R.id.parent_two); 

    FrameLayout containerOne = parentOne.findViewById(R.id.container);
    FrameLayout containerTwo = parentTwo.findViewById(R.id.container);

    int containerOneId = containerOne.getId();
    int containerTwoId = 1;
    containerTwo.setId(containerTwoId);

    openFragment(new TestFragment(), containerOneId);
    openFragment(new TestFragment(), containerTwoId);

本文地址:https://www.itbaoku.cn/post/2091066.html

问题描述

I have a layout that has two views with the same id. If I want to find the view I just call parentView1.findViewById(R.id.content) or parentView2.findViewById(R.id.content) to get the correct view.

If I want to replace a container with a fragment, can I somehow define which one I want to be replaced?

推荐答案

Fragment manager accepts only id, so, i think, answer is no, you cannot specify container. But you still can found both of them.

As a workaround you can wrap your containers with fragments and then inside these fragments ids will be unique; so you can do something like fragment1.replaceContent(...), where .replaceContent(...) is your own method. Ofc, you need to ensure that your references is correct, state saves, etc.

其他推荐答案

You should wrap the same id layouts in a parent layout. And then set the id for second container to a different integer. For example layout file would look like this:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parent_one">

    <include layout="@layout/container" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parent_two">

    <include layout="@layout/container"/>

</LinearLayout>
</LinearLayout>

And you would access them from using parent ids first. Then setting the id for second container to a different one. For example:

    val containerOneId = parent_one.container.id
    val containerTwoId = 1
    parent_two.container.id = containerTwoId

    openFragment(TestFragment().apply {
        val b = Bundle()
        b.putString(TestFragment.ARG, "Fragment one")
        arguments = b
    }, containerOneId)

    openFragment(TestFragment().apply {
        val b = Bundle()
        b.putString(TestFragment.ARG, "Fragment two")
        arguments = b
    }, containerTwoId)

Same access logic but in Java (in case you're not using Kotlin)

    LinearLayout parentOne = (LinearLayout) findViewById(R.id.parent_one);
    LinearLayout parentTwo = (LinearLayout) findViewById(R.id.parent_two); 

    FrameLayout containerOne = parentOne.findViewById(R.id.container);
    FrameLayout containerTwo = parentTwo.findViewById(R.id.container);

    int containerOneId = containerOne.getId();
    int containerTwoId = 1;
    containerTwo.setId(containerTwoId);

    openFragment(new TestFragment(), containerOneId);
    openFragment(new TestFragment(), containerTwoId);