0

In AndroidStudio, I selected "New->Fragment->Fragment (List)" to create a list of items. It works fine and displays all the items; however, I would like to have a title at the top.

If I add another TextView to the "fragment_item_list.xml" file, then the title appears in all the items in the list. From other questions I figured that if I create another LinearLayout xml layout file (something like "list_header.xml") and put a TextView in it, I can use a layout inflater to inflate this layout in the fragment activity, e.g. in onCreateView,

View header = inflater.inflate(R.layout.list_header,container,false);

but then I don't know what to do with header to make it display somewhere in the fragment. I can add it to the original activity by calling container.addView(header); but how can I add it to the fragment?

I don't want to change the ActionBar title, firstly just for aesthetics, and secondly because the fragment covers it up. Ideally there would be a header like in an alert dialog.

Sam Jaques
  • 291
  • 3
  • 13
  • check this http://stackoverflow.com/questions/15560904/setting-custom-actionbar-title-from-fragment – Carlos Laspina Apr 17 '17 at 18:17
  • Possible duplicate of [Setting Custom ActionBar Title from Fragment](http://stackoverflow.com/questions/15560904/setting-custom-actionbar-title-from-fragment) – Carlos Laspina Apr 17 '17 at 18:17
  • so if you already have actionBar then where exactly do you want to put title of your fragment. Can you attach a screenshot – Ali Apr 17 '17 at 18:59

2 Answers2

1

Let me try to understand you. You want to add a title to your fragment list (like an header), not in the ActionBar. Right?

Looking the fragment_item_list.xml we have this: (I follow your steps: "New->Fragment->Fragment (List)")

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:listitem="@layout/fragment_item" />

The RecyclerView is a container used for populate a list, in other words, the RecyclerView (or ListView) contain all the list only. So, you have to add another container (like LinearLayout, RelativeLayout, etc.) as root of the layout to add more views to the layout. Example using the LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Hello World! I'm a header!"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:name="com.veroapps.myapplication.ItemFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/fragment_item" />
</LinearLayout>

With this you will have a list with a title at the top. Now, you can manage the title TextView in the ItemFragment(name of the fragment generated by Android Studio). And of course there are more ways to do that, but this is an easy one.

Hope this help you.

Luis E. Vega
  • 399
  • 2
  • 14
0

In your Fragment java class, there should be a method called onCreateView this is where you set the layout for your Fragment (and where the list is added). It will look something like this:

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

In this Fragment the layout is inflated by this code inflater.inflate(R.layout.example_fragment, container, false); and this means there's is an xml file defining the layout called example_fragment.xml.

Your code will be the same, when you find this XML file. You can add other views inside it.

You will want to add another TextView above the RecyclerView or ListView already in that file.

Something like this:

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
>
 <TextView
   ...
   android:text="YOUR TITLE" />
 <RecyclerView
   ... />

</LinearLayout> 
Blundell
  • 75,855
  • 30
  • 208
  • 233