Search code examples
androidandroid-layoutandroid-recyclerviewandroid-actionbar

Placing Recylerview beneath transparent actionbar?


I currently have a fragment with a recylerView and an actionBar that is transparent. I am trying to create a space at the top of my recylerView so that my actionBar isn't immediately overlapping my recylerView when viewing the top of the recylerView. This is the effect I am trying to create (taken from default gallery app):

https://i.sstatic.net/GvCBi.jpg

This is what I currently have in my own app:

enter image description here

How do I get that little white space at the top before my recylerView so that my transparent actionBar isn't overlapping the topmost recylerView row immediately?

This is the relevant code:

ActivityMain.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);


    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);
    if (fragment == null) {
        fragment = FragmentAlbumGallery.newInstance();
        fm.beginTransaction().add(R.id.fragment_container, fragment, Constants.FRAGMENT_ALBUMS_GALLERY).commit();


    }
}

FragmentAlbumGallery.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_recycler_gallery, container, false);
    mAlbumRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

    mAlbumRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    //onResume() gets called here. No need to set up adapter since we do it there
   // setupAdapter();
    return v;
}

activity_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">
</FrameLayout>

fragment_recyler_gallery.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityGallery"/>

Solution

  • Add a View and RecyclerView under it:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#c4c4c4" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom" />
    
    </LinearLayout>
    

    Output:

    enter image description here