Daniel
Daniel

Reputation: 1033

How to hide/show layout in response to scrolling

So here is my activity's layout

<?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="match_parent"
              android:orientation="vertical"
    <include
        layout="@layout/signs"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

    <android.support.v7.widget.RecyclerView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/rv"
            android:clipToPadding="false">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

The signs include:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:elevation="4dp">


    <ImageView
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/navi_door"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:paddingTop="5dp"
            android:src="@drawable/door_24"/>

</RelativeLayout>

When I scroll a list of items of RecyclerView the signs layout stays at it's place, of course. And that's why my question is how to make this layout also be scrolled then RecyclerView's list is scrolled.

Upvotes: 1

Views: 1162

Answers (1)

Kenan Begić
Kenan Begić

Reputation: 1228

Because RecyclerView does not support adding Header/Footer like ListView you can achieve this like following :

private class ViewType {
        public static final int Signs = 1;
        public static final int Normal = 2;
}

Then override getItemViewType:

@Override
public int getItemViewType(int position) {

    if(items.get(position).isHeader)
        return ViewType.Signs;
    else
        return ViewType.Normal;

}

And finally override onCreateViewHolder:

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View rowView;

    switch (i)
    {       
        case ViewType.Signs:
            rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.signs, viewGroup, false);
            break;

        default:
            rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal, viewGroup, false);
            break;
    }
    return new ViewHolder (rowView);
}

Upvotes: 3

Related Questions