Jemo Mgebrishvili
Jemo Mgebrishvili

Reputation: 5487

How can I change views position inside scrollView, to pin it on top of the screen

I have a scrollview, inside scrollview I have items and I want to pin some of them to top of the screen (Like stickyheaderListview) depend on scrollView scrollY . I have tried almost everything.

If I set my header item Y position to zero it changes position,but after that it is first item inside scrollView.

header.animate().y(0).setDuration(0).start(); // first attempt
header.setTop(0); // second attempt
header.animate().y(mScrollView.getLastItem().getBottom).setDuration(0).start();// third attempt

P.S

My goal is to make stickyheader list items inside scrollView I tried this library inside scrollview, but it does not worked, all items were scrolling usually

Upvotes: 1

Views: 1572

Answers (1)

super1ha1
super1ha1

Reputation: 629

Well, I think there might have multiple ways to achieve the same UI in Android.

1) First way, wrap your scroll view into a relative layout like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="top here"
            android:id="@+id/text"
            />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="sample"
                />
        </ScrollView>
    </RelativeLayout>

2) Or an other way is you can divide your main layout into 2 fragment, one fragment show the view that is always on top, and the other will load the content of the scrollview.

3) This post might be a good reference for you Android: pin TabLayout to top of Scrollview

Upvotes: 1

Related Questions