2

I have a Gridview filled with images, when you click in one of those images it starts a detais activity.
It's all working fine, but now I want to make a Master-Detail layout. So I created that "layout-land" folder and instead of only a gridview, it was this:

<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:background="@color/background"
tools:context=".MainActivityFragment">

<GridView
    android:id="@+id/main_grid"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:numColumns="auto_fit" />

<fragment
    android:id="@+id/fragment"
    android:name="com.example.lucas.popularmovies.DetailActivityFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    tools:layout="@layout/fragment_detail" />

</LinearLayout>


Before that, I was passing the Details' data as an Extra of the intent and retrieving it in the Fragment.
But when I'm displaying it all in the same screen, how do I pass the data in a way that will update the Details when I click in an image without starting a new activity? Thanks.

Cas
  • 336
  • 1
  • 3
  • 18
  • 2
    The [communicating with other fragments](http://developer.android.com/training/basics/fragments/communicating.html) documentation is the simplest example of master-detail fragments I've found. – OneCricketeer Dec 14 '15 at 21:33

5 Answers5

1

Simple.

DetailActivityFragment fragment= (DetailActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
    if (fragment != null) {
        fragment.updateImage(url);
    }
0

Since you have a static fragment in your layout which I assume you will not remove, I would say use a simple logic like this:

In your activity, create a method which will update your static fragment like:

public void updateImage(String imageUrl) {
    DetailActivityFragment fragment= (DetailActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
    if (fragment != null) {
        fragment.updateImage(imageUrl);
    }
}

Whenever you click an image, you call this method.

And inside your fragment load your image in your ImageView(you should have one).

Mike
  • 4,550
  • 4
  • 33
  • 47
0

Use EventBus by square.

Create a Bus Object and register it in Activity

Bus bus = new Bus();
bus.register(this);

Create a public method in Activity with argument as your Model.

@Subscribe 
public void getDataFromGrid(MainGridItem item) {
    // TODO: React to the event somehow!
}

At GridView onItemClick post the item -

bus.post(mainGridItems.get(position));
mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
0

You can find the fragment using

Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentLoadingSpace)
if(fragment instanceof  Fragment1){
    ((Fragment1) fragment).updateSelectedObjec(Objects object);
}
Mike
  • 4,550
  • 4
  • 33
  • 47
anoopg87
  • 82
  • 6
-1

I posted a full answer on another SO link @ Passing data between fragments contained in an activity. Simply find my user name and it is the only answer for that post. Let us know of your progress.

Community
  • 1
  • 1
The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • 1
    If this is a duplicate, you should've voted it as such, rather than posting a link-only answer that just points to another post. – Mike M. Dec 14 '15 at 23:40
  • @MikeM. I'll think about it next time. I am reluctant to vote any post as duplicate, and then it might be closed, as voted by others. I remember one time I actually disagreed with that on another post by someone else. – The Original Android Dec 15 '15 at 01:02