1

I've created an app using the Master/Detail template, but instead of a list on the master view, I want to add other elements (in fact replace the list with a different layout and add the initial list in this layout)

How to acheive this?

The top layout contains a list fragment on the left for the master, and framelayout on the right that will contains the detail fragment...

Is it possible to replace the master list fragment with a view that will contains the fragment?

I've created a specific layout (left_layout), that is referenced in the fragment xml, and changed my class inheritence to inherit from fragment and not list fragment, and I have the following exceptio when the application try to instanciate this fragment:

lang.RuntimeException: Unable to start activity ComponentInfo{com.digiwiz.tvtweet/com.digiwiz.tvtweet.ChannelListActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

which is called from:

setContentView(R.layout.activity_channel_list);

and activity_channel_list is the master activity that has been modified:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/channel_list"
          android:name="com.digiwiz.tvtweet.ChannelListFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:layout_marginLeft="16dp"
          android:layout_marginRight="16dp" 
          tools:context=".ChannelListActivity"
          tools:layout="@layout/left_layout" />
Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
tomsoft
  • 4,448
  • 5
  • 28
  • 35
  • I think you need to put your `` inside something like a `FrameLayout` – Squonk Aug 07 '15 at 14:25
  • @Squonk That should only be the case for dynamic Fragments. – PPartisan Aug 07 '15 at 14:27
  • Do you use `android.app.Fragment` or `android.support.v4.app.Fragment` ? If it's the second one, is your Activity a `FragmentActivity` or of a class that extends `FragmentActivity` (i.e. `AppCompatActivity`)? It may be an incompatibility. – PPartisan Aug 07 '15 at 14:31
  • I use android.app.Fragment – tomsoft Aug 07 '15 at 14:33
  • 1
    @PPartisan :Yeah, I wasn't sure as I only ever use dynamic Fragments added to containers at runtime. In saying that though, the Android dev docs show using a `FrameLayout` in layout XML files to contain `` blocks so I thought it might be worth trying. – Squonk Aug 07 '15 at 14:38
  • @tomsoft Try changing your import declaration to `android.support.v4.app.Fragment` - like Squonk, I never use static fragments, but [this answer](http://stackoverflow.com/questions/6424853/error-inflating-class-fragment) suggests it could cause the problem. – PPartisan Aug 07 '15 at 14:44

1 Answers1

2

(this isn't realy a full answer to your question, but couldn't fit it in a comment.)

I think your book makes reference to the 'old' template for master-detail flow. The new template was introduced around the time of SDK 23 I think.

The new one uses a different technique: there is no Master/List fragment class, there is just the layout for the list which acts as the list fragment. Note that the list layout has a name attribute that ends with 'Fragment' - this attribute is not actually used but is an indication the element is being used like a fragment. I believe you would replace this element with the layout or view that you want to use instead of the list.

More generally, if your master layout should have more then just the list (leaving aside header, toolbar, etc. which must go in the activity), you can add that stuff either: - to the activity, along with header/toolbar, which only makes sense if you expect it to be different for single vs. dual pane (which is perhaps what Google has in mind for this). - or to the item_list.xml files (one for each layout configuration) and replace the recyclerview with a different layout.

I'm sorry that they changed the template to do it this way. I think the old template with 2 fragment classes and layouts was easier to comprehend and more flexible. eg. for situations like yours and for any case where one wants the list/master fragment to be something more then just a list (isn't that normal?). I wasn't able to find any mention or explanation of the change they made.

Tom
  • 17,103
  • 8
  • 67
  • 75