Code Monkey home page Code Monkey logo

twofragmentsexamplepart2's Introduction

When I started to study fragments on Android, I discover the following example on the Developer Android Site: http://developer.android.com/guide/components/fragments.html

Basically, 1 Activity that changes two fragments according to the layout. It sounds very useful but this documentation lacks code that can help me to understand how to create this kind of layouts and fragment interaction.

This is because I created this small example focused on solve the two fragments interaction with 1 main layout.

  1. Layouts

I created two activity_main.xml layouts, one in the "layout" folder (default) and the second one in the "layout_land" folder. Android will use this layout on screen rotation to landscape.

activity_main.xml (portrait)

The main layout in portrait mode will only include the main fragment directly.

activity_main.xml (landscape)

The main layout in landscape mode will include the main fragment and a second framelayout that I will use as a "container" for the second fragment that will be added programmatically.

Notice that this layout use a LinearLayout with an Horizontal Divider and of course, Horizontal orientation:

android:divider="?android:dividerHorizontal"android:orientation="horizontal"

Also, notice that I'm using the "android:layout_weight" property according to the Android Developer documentation: http://developer.android.com/guide/topics/ui/layout/linear.html#Weight

  1. Second fragment detection

Because the main activity will use both activity_main.xml according to the layout orientation I need to find a way to detect if I'm using the layout with one fragment or the layout with two fragments because if not..., well..., nothing it's going to work well.

To do this, I basically "ask" with the following line if the second framework exists or not:

if (findViewById(R.id.fragment_detail)!=null)

If this conditions its "True", it's because I'm in landscape mode and I'm using the Two Fragments Layout so I need to call the second fragment and show it. If not, there is nothing to worry, because I'm using the main fragment layout that it's already included and called in the layout.

As a result, the main activity will add the second fragment only when he detects need to do it.

  1. Fragment calls a method in the Activity (NO hardcoding)

I need to find a way to manage the OnItemClickListener from the ListView in the DetailFragment but calling a method in the MainActivity.

Why?

Basically:

a) I don't want to put references or calls to others Activities inside the OnItemClickListener event in the DetailFragment. It supposed that fragments need to be "reusable", so you should not put references to external activities inside.

b) I want to call an independent method created in the MainActivity from the OnItemClickListener event in the DetailFragment but, I don't want to create a "hardcoding" relationship between both parts. Because again..., I don't know who is going to reuse this fragment in the feature calling it from another activity!

The Result:

  • I created an Interface that includes a method.
  • I implemented this method in the MainActivity.
  • I got an instance of the Activity that creates the DetailFragment in the "onAttach" event inside the DetailFragment.
  • I used the instance to access the method that then is called in the ListView. This is a "clear" way to call a method from a Fragment to an Activity without compromise the independence of the fragment.

As a result, I created what I understand it's a very simple example that covers:

  • Managing different layouts between landscape and portrait.
  • Implementing and reusing fragments between layouts.
  • Uncouple methods calling between fragments and activities.

Main Layout in Portrait:

Portrait Layout

Portrait Layout

Main Layout in Landscape:

Portrait Layout

Hope it helps you to understand these concepts. But if you find any doubts or if you find any issue or suggestions to improve this example, please, don't hesitate to contact me.


Pablo Angel Veliz https://ar.linkedin.com/in/pabloveliz

twofragmentsexamplepart2's People

Contributors

paveliz avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.