Joe
Joe

Reputation: 246

Android fragment scrolling all elements when using recyclerview

I am developing an android app. I am using Fragment and RecyclerView. In my screen have SearchView, header, RecyclerView and a Button. I want the screen like I have shown below image.

enter image description here

But it will scrolling all view. The bottom buttons are coming at the last of recyclerview last element. My layout code shown below.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:queryHint="Search Item" />

        <LinearLayout
            android:id="@+id/lItemsHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="2dip"
            android:layout_weight="0.25"
            android:background="@color/title_background">

            <TextView
                android:id="@+id/item2"
                style="@style/ItemsHeader"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="3dip"
                android:width="0dip"
                android:text="@string/caption_item"
                android:textColor="@color/background1" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:scrollbars="vertical" />

        <LinearLayout
            androidrientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="60dp"
            android:layout_weight="0.25">

            <Button
                android:id="@+id/btn1"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:layout_marginBottom="2dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@color/title_background"
                android:text="Save &amp; Close"
                android:textColor="#FFFFFF"
                android:textSize="@dimen/text_size_small" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

How to solve the problem? Please help me.

Upvotes: 1

Views: 105

Answers (4)

Insane Developer
Insane Developer

Reputation: 1102

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:queryHint="Search Item" />

<LinearLayout
android:id="@+id/lItemsHeader"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_below="@+id/search_view"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="2dip"
android:background="@color/title_background">
<TextView
android:id="@+id/item2"
style="@style/ItemsHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="3dip"
android:width="0dip"
android:text="@string/caption_item"
android:textColor="@color/background1" />
</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearFooter"
android:layout_below="@+id/lItemsHeader"
android:scrollbars="vertical" />
<LinearLayout
android:id="@+id/linearFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/btn1"
android:layout_width="75dp"
android:layout_height="35dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@color/title_background"
android:text="Save"
android:textColor="#000"
android:textSize="@dimen/text_size_small" />
</LinearLayout>
</RelativeLayout>

Upvotes: 0

Navneet Krishna
Navneet Krishna

Reputation: 5017

try this one

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">


    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search Item" />

    <LinearLayout
        android:background="@color/colorPrimary"
        android:id="@+id/lItemsHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_marginTop="2dip"
        >


        <TextView
            android:id="@+id/item2"
            android:text="HEADER"
            android:textAlignment="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dip"
             />




    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

    <LinearLayout
        android:layout_below="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <Button
            android:id="@+id/btn1"
            android:layout_width="75dp"
            android:layout_height="35dp"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:text="Save &amp; Close"
            android:textColor="#FFFFFF"
             />

    </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Upvotes: 0

Reaz Murshed
Reaz Murshed

Reputation: 24211

Your layout construction for the desired screen that you want is a bit complex. However, I would like to suggest you a simpler one. Try the layout I am proposing below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:queryHint="Search Item" />

    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:layout_below="@+id/search_view"
        android:layout_margin="8dp"
        android:background="@color/colorPrimary"
        android:text="@string/title_winner"
        android:textColor="@color/segmented_button_text_color" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:clipToPadding="true"
        android:paddingBottom="35dp"
        android:scrollbars="vertical" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"
        android:text="Save and Close"
        android:textColor="#FFFFFF" />

</RelativeLayout>

Note that, I have used a paddingBottom in your RecyclerView and added clipToPadding to true. So that the last item of your RecyclerView will not be hidden under the Save and Close button your have in the bottom of your screen.

Hope that helps.

Upvotes: 0

MJM
MJM

Reputation: 5301

Updated the xml

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_alignParentTop="true"
            android:layout_height="match_parent"
            android:queryHint="Search Item" />

        <LinearLayout
            android:id="@+id/lItemsHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="2dip"
            android:layout_below="@+id/search_view"
            android:background="@color/title_background">


            <TextView
                android:id="@+id/item2"
                style="@style/ItemsHeader"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="3dip"

                android:width="0dip"
                android:text="@string/caption_item"
                android:textColor="@color/background1" />

        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_below="@+id/lItemsHeader"
            android:layout_width="match_parent"
            android:layout_above="@+id/linearFooter"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

        <LinearLayout
            android:id="@+id/linearFooter"
            androidrientation="horizontal"
             android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="60dp">

            <Button
                android:id="@+id/btn1"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:layout_marginBottom="2dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@color/title_background"
                android:text="Save &amp; Close"
                android:textColor="#FFFFFF"
                android:textSize="@dimen/text_size_small" />

        </LinearLayout>


    </RelativeLayout>

Upvotes: 2

Related Questions