Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
Fixes #4 -> Support nested fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor committed May 6, 2016
1 parent e64019c commit 46a5a96
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/app/HostActivitySampleFragmentNested.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package app;

/**
* Created by victor on 06/05/16.
*/
public class HostActivitySampleFragmentNested {
}
79 changes: 79 additions & 0 deletions app/src/main/java/app/SampleParentFragmentNested.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import io.victoralbertos.app.R;
import rx_activity_result.RxActivityResult;

/**
* Created by victor on 06/05/16.
*/

public class SampleParentFragmentNested extends Fragment {

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_parent_fragment_nested, container, false);
return view;
}

@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

getChildFragmentManager()
.beginTransaction()
.add(R.id.fl_fragment_container, new SampleFragmentNested())
.commit();
}

public static class SampleFragmentNested extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_layout, container, false);
return view;
}

@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().findViewById(R.id.bt_camera).setOnClickListener(view -> camera());
}

private void camera() {
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

RxActivityResult.on(this).startIntent(takePhoto)
.subscribe(result -> {
Intent data = result.data();
int resultCode = result.resultCode();

if (resultCode == Activity.RESULT_OK) {
result.targetUI().showImage(data);
} else {
result.targetUI().printUserCanceled();
}
});
}

private void showImage(Intent data) {
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
((ImageView) getView().findViewById(R.id.iv_thumbnail)).setImageBitmap(imageBitmap);
}

private void printUserCanceled() {
Toast.makeText(getActivity(), getString(R.string.user_canceled_action), Toast.LENGTH_LONG).show();
}
}


}


14 changes: 14 additions & 0 deletions app/src/main/res/layout/host_activity_sample_fragment_nested.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/sample_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="app.SampleFragment"
tools:layout="@layout/sample_layout" />

</LinearLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/sample_parent_fragment_nested.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>

0 comments on commit 46a5a96

Please sign in to comment.