android - xml - 多次包含相同的布局不起作用

时间:2022-08-31 08:45:36

I'm trying to include the following layout twice:

我试图将以下布局包括两次:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.viewpagerindicator.TabPageIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

like the following

如下

<include
    android:id="@+id/include1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/view_pager" />

<include
    android:id="@+id/include2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/view_pager" />

Actually, the second view pager does not work if I do it like that... The second view pager always stays empty... If I copy my view_pager layout and change the ids in this layout (pager to pager2 and indicator to indicator2) everything works fine. Is there a better way to do that? Copying layouts to achieve that seems to make the include useless for multiple includes of the same layout....

实际上,如果我这样做,第二个视图寻呼机不起作用...第二个视图寻呼机总是保持空...如果我复制我的view_pager布局并更改此布局中的ID(寻呼机到pager2和指示器到指示器2)一切正常。有没有更好的方法呢?复制布局以实现这似乎使包含无用的多个包括相同的布局....

I'm getting the references correctly I think, but though it just does not work if I include the same layout...

我认为我正确地得到了参考文献,但是如果我包含相同的布局它只是不起作用...

pager1= (ViewPager)(findViewById(R.id.include1).findViewById(R.id.pager));
pager2= (ViewPager)(findViewById(R.id.include2).findViewById(R.id.pager));

Everything works perfectly if I copy the layout...

如果我复制布局,一切都很完美......

Edit:

编辑:

I think it has to do with the FragmentManager, because the view pagers have the same id... But I don't know how to solve that correctly...

我认为它与FragmentManager有关,因为视图寻呼机具有相同的ID ...但我不知道如何正确解决...

1 个解决方案

#1


2  

Yes it can be done. You can inflate the layout many times, but you have to make the inclusion programmatically. See the answer to same kind of question.

是的,它可以做到。您可以多次为布局充气,但必须以编程方式进行包含。查看同类问题的答案。

#1


2  

Yes it can be done. You can inflate the layout many times, but you have to make the inclusion programmatically. See the answer to same kind of question.

是的,它可以做到。您可以多次为布局充气,但必须以编程方式进行包含。查看同类问题的答案。