Android: name attribute in <fragment>

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 do not know what they refer to? Is it either a fragment class (or a subclass of it) that is used in the source code or XML files for the layout?

If these are XML files, where should they be located?

+7
source share
4 answers

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

As already mentioned, you can have your fragment inside the containment activity, but this is not a good practice.

As a good example, try creating "Blank Activity with Fragment" using the Android Studio wizard. It will create an activity class and a fragment class along with two XML files for the activity and fragment, respectively.

+7
source
 <fragment android:name="com.example.app.myFragment"/> 

The name attribute is used to indicate the Fragment class, which is used to create the View hierarchy — in this case, myFragment.java.

+4
source

They belong to two subclasses of Fragment , one of which is called ArticleReaderFragment , the other, called ArticleListFragment . The package is the same for both, com.example.news . Android will take care of creating the instance for you.

+2
source

ArticleListFragment and ArticleReaderFragment point to a class that you can find on the path com/example/news/.. These classes must be subclasses of the Fragment class.

This means that these classes must exist in your code for this example to work XML.

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

+1
source

All Articles