Search code examples
androidscrollviewonscrolllisteneronscrollchanged

How to hide a layout when a scrollview is scrolling in android


I am newbie to android and working on a scrollview,I have a footer layout and want to hide it while scrolling the scrollview,I have tried onscrollStatechange i have put that view's visibilty gone,But its not working.Can anybuddy help me to do this?

  <ScrollView
        android:id="@+id/scr_product"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/rl_botm">
.
.
.
.</ScrollView>
<RelativeLayout
        android:id="@+id/rl_botm"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:background="@color/blue_extra_light"
        android:padding="5dp">

        <View
            android:id="@+id/sep"
            android:layout_width="match_parent"
            android:layout_height="0.75dp"
            android:background="@color/dark_grey" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sep"
            android:layout_marginTop="5dp">

            <ImageView
                android:id="@+id/iv_share"
                android:layout_width="30dp"
                android:layout_height="35dp"
                android:layout_marginLeft="30dp"
                android:background="@drawable/ic_sare" />

            <View
                android:id="@+id/s1"
                android:layout_width="0.75dp"
                android:layout_height="20dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_toRightOf="@+id/iv_share"
                android:background="@color/grey_dark" />

            <ImageView
                android:id="@+id/iv_heart"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_toRightOf="@+id/s1"
                android:background="@drawable/ic_hrt" />

            <View
                android:id="@+id/s2"
                android:layout_width="0.75dp"
                android:layout_height="20dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_toRightOf="@+id/iv_heart"
                android:background="@color/grey_dark" />

            <ImageView
                android:id="@+id/iv_report"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_toRightOf="@+id/s2"
                android:background="@drawable/ic_flag" />

            <Button
                android:id="@+id/btn_rate_reviews"
                android:layout_width="150dp"
                android:layout_height="50dp"
                android:layout_alignParentRight="true"
                android:background="@drawable/btn_green_selector"
                android:padding="5dp"
                android:text="@string/rate_reviews"
                android:textColor="@color/white"
                android:textSize="16dp"
                android:textStyle="bold" />
        </RelativeLayout>

    </RelativeLayout>

java

public class ProductDescriptionActivity extends Activity implements AbsListView.OnScrollListener {
.
.
.
.
 @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        rl_botm.setVisibility(View.VISIBLE);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        rl_botm.setVisibility(View.GONE);
    }

Please help me to figure it.


Solution

  • Try this,

    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (scrollView != null) {
                if (scrollView.getChildAt(0).getBottom() <= (scrollView.getHeight() + scrollView.getScrollY())) {
                    relativeLayout.setVisibility(View.VISIBLE);
                }
                else {
                    relativeLayout.setVisibility(View.INVISIBLE);
                }
            }
        }
    });