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....
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...
Yes, you can. Just call "setContentView" with the same xml and it will use the same layout. Change whatever you need to programmatically during runtime.
To efficiently reuse complete layouts, you can use the <include> and <merge> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.
Yes its possible. You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With