简体   繁体   English

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

[英]android - xml - include same layout multiple times does not work

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. 实际上,如果我这样做,第二个视图分页器将无法工作...第二个视图分页器始终保持空白...如果我复制view_pager布局并更改此布局中的ID(将分页器更改为pager2,将指示器更改为indicator2)一切正常。 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.... 复制布局以实现该目的似乎会使包含对于同一布局的多个包含物无效。

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 ...但是我不知道如何正确解决...

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. 参见同类问题的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM