Fragment - replace container, if id is not unique

本秂侑毒 提交于 2019-12-12 18:28:48

问题


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?


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/33697204/fragment-replace-container-if-id-is-not-unique

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!