Multiple FragmentManager Fragments on Screen?

In Android 3.0 and up, the android team is actively working to get you to use fragments over actions. And I see this is useful, but I want to be able to handle click events in my application. I am using a list fragment on the right side of my application, so when using the object that the fragment is hosted in, an onclick (or any click listeners) occurs. So I had to move from object to XML in order to use the fragment manager.

The design docs show this image: Misleading photo

http://developer.android.com/training/basics/fragments/fragment-ui.html

What I want is a tablet interface for A / B slices. However, nowhere on this page does it give you an example of this - it seems like the fragment manager only works on one fragment at a time, which is the exact opposite of what the image depicts. Which makes me think what it is using in XML ... but then how can I get the onclick? These docs don't make a lot of sense to me. It shows one thing and then says something else. What if I want to delete fragment A on the tablet? Add a C snippet that doesn't exist yet? Is this possible even if you tried using the Fragment Manager ??

I think it doesn't work if the chunk manager uses more than one chunk, and if so, how should I use that to get an item in the picture like a tablet - left (A) is a list, and right (B) is that's whatever. Without the fragment ID, I cannot access it.

Not sure if this is relevant, but here are some of my codes:

Adds a snippet to a single frame, which I did as in the tutorial

//Activity              
FragLaunch launchPane = new FragLaunch();
                // In case this activity was started with special instructions from an Intent,
                // pass the Intent extras to the fragment as arguments
                // firstFragment.setArguments(getIntent().getExtras());

                // Add the fragment to the 'fragment_container' FrameLayout
                getFragmentManager().beginTransaction()
                        .add(R.id.launch_frag_container, launchPane).commit();
            }

      

Also, in portrait mode from a 7 '' tablet, I want it to use the swipable viewpager. It worked like a charm when I designed it in XML, but now when I need to access the list, it doesn't work (no access since I don't have two fragments)

XML presentation of FragLaunch content:

<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:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/initial_directions"
        style="@style/textScalar.Roboto.Light"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/initial_directions"
        android:layout_marginBottom="30dp"
        tools:context=".Launch" />

</LinearLayout>

      

I want this file to appear as fragment A in photo: FragHistory.java/xml for fragment:

<?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="vertical" >

    <Button
        android:id="@+id/spamhistory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Spam History" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

</LinearLayout>

      

Does anyone know about this?

+3


source to share


2 answers


If you want your fragments to be able to communicate, you need to use interfaces like this .

For onClick events, you simply set an onClickListener for the view that is required to receive the onClick event. For example:

sampleView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        //Your code here
    }
});

      

As far as fragment transactions are concerned, it says somewhere in there (I don't remember exactly where) when two fragments are displayed on the screen at the same time (as with large screens), instead of swapping fragments, it just updates it. All you need to worry about is the right calls. Therefore, if you want to remove fragment A, just call remove (). Or if you want to replace it with a C fragment by calling replace () and pass the C fragment as a parameter.

Can you clarify your question about the ViewPager? What does it mean to "have access to it"?

Hope this helps!



EDIT: I apoplogize, I misunderstood your question. Here you can quickly start how to add several fragments to the screen at once.

1. Perform a runtime check to make sure the device screen is large enough to display more than one slice.

2. If the screen is large enough, set the presentation of the content to use a layout that has a FrameLayout for each piece you want to add.

3. After that, grab the link for each snippet you want to use.

4. Then use the FragmentManager to add a fragment to each layout. Like this:

FirstExampleFragment fragment1 = new FirstExampleFragment();
SecondExampleFragment fragment2 = new SecondExampleFragment();

getSupportFragmentManager().beginTransaction().add(R.id.example_framelayout1, fragment1)
.add(R.id.example_framelayout2, fragment2).commit();

      

0


source


Another great way to allow communication between fragments is to use an event bus like the Otto event bus. Otto allows components to publish events and subscribe to events separately.

In your specific case, when a user selects an item in the list, your list fragment can post an event (which can include the item that was selected), and your content fragment can subscribe to those events and update its content accordingly when it receives a new event. All this is done without two chunks related to each other and without the need to define additional interfaces.



I know this doesn't answer your whole question, but thought it might be helpful when it comes to messages between your fragments .... YMMV.

+1


source







All Articles