11

Please explain ArticleListFragment and ArticleReaderFragmet as they are in this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

I don't know what they refer to? Is it either the Fragment class (or its subclass) that use in source code or XML files for layout?

If they are XML files where must they be located?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
hamid_c
  • 849
  • 3
  • 11
  • 29

4 Answers4

13

The ArticleListFragment and ArticleReaderFragment are names of classes that contain java code for these fragments.

As it was mentioned before you can have your fragment inside containing activity, but it is not a good practice to do so.

For a good example try to create "Blank Activity with Fragment" using Android Studio wizard. It will create an activity class and a fragment class along with 2 XML files for activity and fragment respectively.

Ivan V
  • 3,024
  • 4
  • 26
  • 36
6
<fragment android:name="com.example.app.myFragment"/>

The name attribute is used to specify the Fragment class that is used to create the View hierarchy - in this case myFragment.java.

eddykordo
  • 607
  • 5
  • 14
  • so u mean that i have to implement myFragment class in a separate file??? not in the activity source code that i want to implement fragment for that??? – hamid_c Jul 02 '15 at 13:34
  • You can do both ways , first you can implement the fragment in your activity or you can implement an extra fragment class, i refer the second way, look at this example http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ – eddykordo Jul 02 '15 at 13:36
2

They refer to two subclass of Fragment, one called ArticleReaderFragment the other called ArticleListFragment. The package for both is the same, com.example.news. Android will take care of instantiate both for you

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

The ArticleListFragment and ArticleReaderFragment point to the class you can find on the path com/example/news/... Those classes should be subclasses of the Fragment class.

That means those classes need to exist in your code, to make this example XML working.

In my opinion this is all really clean explained on http://developer.android.com/guide/components/fragments.html#Adding

ndsmyter
  • 6,535
  • 3
  • 22
  • 37