IamNumber5
IamNumber5

Reputation: 351

How to swap Fragment dynamically?

I am little confuse How to use Fragments in Android apps! I am beginner in android and I am trying to build an apps for tablet 7 Landscape as you seen in the image below!

A, B, D How to swap fragment! By click event. When user click on A the content of A moved and shows in D and content of D shows in A and so on.

I want to be able to swap the content of A,B,and D dynamically!

What I should use? Fragment, Fragment Activity.

For example if D showing Video in VideoView, then when user click on A I want D to show TextView or any kind of data (but is not video View) as well as showing the Video in A.

<pre>
<!--> 
//////////////////////////////////////////// 
/ -------   -----------------------------  / 
/ -  A  -   -                           -  / 
/ -     -   -                           -  / 
/ -------   -                           -  /  
/ -  C  -   -            D              -  / 
/ -     -   -                           -  / 
/ -------   -                           -  / 
/ -  B  -   -                           -  / 
/ -     -   -                           -  / 
/ -------   -----------------------------  / 
//////////////////////////////////////////// 
<-->
</pre>

What I did is I made 4 fragment and put them inside the Main_Activity

fragment_A I have problem with swap the content with D.

fragment_B I have problem changing the content of D.

fragment_C I connected with Fragment_C class that shows some static data.

fragment_D I have problem with swap the content with A.

I am happy to for you suggestions! Thanks!

Upvotes: 1

Views: 845

Answers (2)

Cehm
Cehm

Reputation: 1512

You can try and go check FragmentTransaction

Check out replace / add / remove fragment methods. I'm sure you'll find what you are looking for.

ex :

FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(container, fragment);
        fragmentTransaction.commit();

and stuffs like :

FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(container, newFragment);
        fragmentTransaction.commit();

Where container is a simple View (RelativeLayout or other things).

EDIT :

Example :

main.xml

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

<LinearLayout
    android:id="@+id/fragment_snapshots"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/fragment_snap1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment_snap2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

<fragment
    android:id="@+id/big_fragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

</LinearLayout>

This layout specify, almost - except I only have 2 fragments on the left, what you are trying to do.

Then using onClickListeners and the function I gave you above, you might be able to do what you want.

Your containers will be the different IDs I put (big_fragment, fragment_snap1 & fragment_snap2.

Upvotes: 3

r4jiv007
r4jiv007

Reputation: 3104

you have to implement listeners .. go through following link :-

http://developer.android.com/training/basics/fragments/communicating.html

Upvotes: 0

Related Questions