Kalpana
Kalpana

Reputation: 23

How to stop Nestedscrollview from scrolling when we hide a Layout from it

I have a Nestedscrollview, when I scroll up I have to Hide a view which is currently not shown in the screen. But when I hide it Nestedscrollview automatically scrolls itself to adujst. I want to stop it from scrolling until user scrolls manually again

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:id="@+id/clMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar_with_boarding_point"
            layout="@layout/toolbar_with_boarding_point"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <LinearLayout
                    android:id="@+id/lytHeader"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/smart_bus_gradient_bg"
                    android:gravity="center_horizontal"
                    android:orientation="vertical"
                    android:visibility="gone">

                    <ImageView
                        android:id="@+id/ivFirstHeader"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/dimen_60dp"
                        android:layout_marginTop="@dimen/gen_mar_pad_five"
                        android:layout_marginRight="@dimen/dimen_60dp" />

                    <ImageView
                        android:id="@+id/ivSecondHeader"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/gen_mar_pad_twenty"
                        android:layout_marginEnd="@dimen/gen_mar_pad_twenty" />
                </LinearLayout>


                <androidx.core.widget.NestedScrollView
                    android:id="@+id/nsvMain"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fillViewport="true">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:id="@+id/lytMain"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/color_flexi"
                            android:gravity="center_horizontal"
                            android:orientation="vertical">

                            <FrameLayout
                                android:id="@+id/rytViewPager"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content">

                                <View
                                    android:id="@+id/viewPagerBg"
                                    android:layout_width="match_parent"
                                    android:layout_height="@dimen/fast_scroll_overlay_text_size"
                                    android:layout_gravity="bottom"
                                    android:background="@drawable/half_arc_white_bg"
                                    android:visibility="gone" />

                                <View
                                    android:id="@+id/viewPagerBlueBg"
                                    android:layout_width="match_parent"
                                    android:layout_height="@dimen/fast_scroll_overlay_text_size"
                                    android:layout_gravity="bottom"
                                    android:background="@drawable/half_arc_blue_big_bg"
                                    android:visibility="gone" />

                                <androidx.viewpager.widget.ViewPager
                                    android:id="@+id/vpSmartBus"
                                    android:layout_width="match_parent"
                                    android:layout_height="@dimen/dimen_180dp"
                                    android:layout_marginStart="@dimen/fab_margin"
                                    android:layout_marginTop="@dimen/gen_mar_pad_five"
                                    android:layout_marginBottom="@dimen/gen_mar_pad_two"
                                    android:elevation="@dimen/gen_mar_pad_ten" />
                            </FrameLayout>

                            <RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/rytViewPager">

                                <LinearLayout
                                    android:id="@+id/llytBusFeaturesBig"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/white"
                                    android:orientation="vertical">

                                    <ImageView
                                        android:id="@+id/ivBackgroundImage"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content" />

                                    <RelativeLayout
                                        android:id="@+id/rlytBusAmenities"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content">

                                        <LinearLayout
                                            android:id="@+id/lytBusAmenities"
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:orientation="horizontal">

                                            <TextView
                                                android:id="@+id/tvBusAmenities"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:background="@drawable/rounded_right_side_only_12dp"
                                                android:fontFamily="@font/roboto"
                                                android:paddingStart="@dimen/fab_margin"
                                                android:paddingTop="@dimen/gen_mar_pad_five"
                                                android:paddingEnd="@dimen/gen_mar_pad_eight"
                                                android:paddingBottom="@dimen/gen_mar_pad_five"
                                                android:text="@string/str_smart_bus_amenities"
                                                android:textColor="@color/color_blue_boarding_point"
                                                android:textSize="@dimen/text_size_micro" />

                                            <View
                                                android:layout_width="match_parent"
                                                android:layout_height="2dp"
                                                android:layout_gravity="center_vertical"
                                                android:background="@drawable/dashed_amenities_blue_line" />
                                        </LinearLayout>

                                        <androidx.recyclerview.widget.RecyclerView
                                            android:id="@+id/rvBusAmenities"
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:layout_below="@id/lytBusAmenities"
                                            android:layout_marginTop="@dimen/gen_mar_pad_ten"
                                            android:visibility="visible"
                                            tools:itemCount="1" />
                                    </RelativeLayout>

                                    <LinearLayout
                                        android:id="@id/lytBusSchedule"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="@dimen/gen_mar_pad_twenty"
                                        android:background="@drawable/half_arc_blue_bg"
                                        android:orientation="vertical">

                                        <LinearLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:layout_marginTop="@dimen/gen_mar_pad_twenty"
                                            android:background="@color/color_flexi"
                                            android:orientation="vertical">

                                            <TextView
                                                android:id="@+id/tvBusSchedule"
                                                android:layout_width="match_parent"
                                                android:layout_height="wrap_content"
                                                android:fontFamily="@font/roboto"
                                                android:gravity="center_horizontal"
                                                android:text="@string/str_smart_bus_schedule"
                                                android:textColor="@color/white"
                                                android:textSize="@dimen/text_size_medium" />

                                            <com.railyatri.in.bus.common.FlowLayout
                                                android:id="@+id/flytBusSchedule"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:layout_gravity="center"
                                                android:layout_marginTop="@dimen/gen_mar_pad_ten"
                                                android:layout_marginBottom="@dimen/gen_mar_pad_ten"
                                                android:gravity="center"
                                                android:orientation="vertical" />

                                        </LinearLayout>


                                    </LinearLayout>
                                </LinearLayout>

                                <LinearLayout
                                    android:id="@+id/lytLowerMain"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                android:layout_below="@id/llytBusFeaturesBig"
                                    android:orientation="vertical">

                                    <View
                                        android:id="@+id/viewPagerBlueLowerBg"
                                        android:layout_width="match_parent"
                                        android:layout_height="@dimen/fast_scroll_overlay_text_size"
                                        android:layout_gravity="bottom"
                                        android:layout_marginBottom="-2dp"
                                        android:background="@drawable/half_arc_blue_big_bg"
                                        android:visibility="gone" />

                                    <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:background="@color/color_flexi"
                                        android:orientation="vertical">

                                        <LinearLayout
                                            android:id="@+id/llytBusFeaturesSmall"
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:layout_marginBottom="@dimen/gen_mar_pad_twenty"
                                            android:orientation="vertical"
                                            android:visibility="gone">

                                            <LinearLayout
                                                android:layout_width="match_parent"
                                                android:layout_height="wrap_content"
                                                android:layout_marginTop="@dimen/gen_mar_pad_ten"
                                                android:layout_marginBottom="@dimen/gen_mar_pad_twenty"
                                                android:background="@color/color_flexi"
                                                android:weightSum="3">

                                                <LinearLayout
                                                    android:layout_width="@dimen/zero_dp"
                                                    android:layout_height="wrap_content"
                                                    android:layout_gravity="center"
                                                    android:layout_weight="1"
                                                    android:gravity="center"
                                                    android:orientation="horizontal">

                                                    <ImageView
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:src="@drawable/ontime_promise_icon_new" />

                                                    <TextView
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:layout_marginStart="@dimen/gen_mar_pad_five"
                                                        android:text="@string/str_on_time_promise"
                                                        android:textColor="@color/white"
                                                        android:textSize="@dimen/text_size_micro"
                                                        app:fontFamily="@font/roboto" />

                                                </LinearLayout>

                                                <View
                                                    android:layout_width="1dp"
                                                    android:layout_height="match_parent"
                                                    android:layout_marginStart="@dimen/gen_mar_pad_ten"
                                                    android:layout_marginTop="@dimen/gen_mar_pad_five"
                                                    android:background="@color/color_white_30" />

                                                <LinearLayout
                                                    android:layout_width="@dimen/zero_dp"
                                                    android:layout_height="match_parent"
                                                    android:layout_gravity="center"
                                                    android:layout_weight="1"
                                                    android:gravity="center"
                                                    android:orientation="horizontal"
                                                    android:visibility="visible">

                                                    <ImageView
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:src="@drawable/flexi_icon_new" />

                                                    <TextView
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:layout_marginStart="@dimen/gen_mar_pad_five"
                                                        android:text="@string/flexi_ticket"
                                                        android:textColor="@color/white"
                                                        android:textSize="@dimen/text_size_micro"
                                                        app:fontFamily="@font/roboto" />

                                                </LinearLayout>

                                                <View
                                                    android:layout_width="1dp"
                                                    android:layout_height="match_parent"
                                                    android:layout_marginStart="@dimen/gen_mar_pad_ten"
                                                    android:layout_marginTop="@dimen/gen_mar_pad_five"
                                                    android:background="@color/color_white_30" />

                                                <LinearLayout
                                                    android:layout_width="@dimen/zero_dp"
                                                    android:layout_height="wrap_content"
                                                    android:layout_gravity="center"
                                                    android:layout_weight="1"
                                                    android:gravity="center"
                                                    android:orientation="horizontal">

                                                    <ImageView
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:layout_gravity="center"
                                                        android:src="@drawable/safety_promise_new" />

                                                    <TextView
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:layout_marginStart="@dimen/gen_mar_pad_five"
                                                        android:text="@string/safety_promise"
                                                        android:textColor="@color/white"
                                                        android:textSize="@dimen/text_size_micro"
                                                        app:fontFamily="@font/roboto" />

                                                </LinearLayout>
                                            </LinearLayout>

                                            <TextView
                                                android:id="@+id/tvExclusiveFeatures"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:layout_gravity="center_horizontal"
                                                android:layout_marginTop="@dimen/gen_mar_pad_four"
                                                android:background="@drawable/rounded_corner_exclusive_blue"
                                                android:paddingLeft="@dimen/gen_mar_pad_ten"
                                                android:paddingTop="@dimen/gen_mar_pad_five"
                                                android:paddingRight="@dimen/gen_mar_pad_ten"
                                                android:paddingBottom="@dimen/gen_mar_pad_five"
                                                android:text="@string/str_exclusive_features"
                                                android:textColor="@color/color_exclusive_offers"
                                                android:textSize="@dimen/text_size_micro"
                                                app:fontFamily="@font/roboto"
                                                app:theme="@style/RippleTheme" />
                                        </LinearLayout>

                                        <androidx.recyclerview.widget.RecyclerView
                                            android:id="@+id/rvRyBusList"
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:visibility="visible" />

                                    </LinearLayout>

                                </LinearLayout>

                            </RelativeLayout>

                        </RelativeLayout>
                    </RelativeLayout>
                </androidx.core.widget.NestedScrollView>
            </LinearLayout>

            <Button
                android:id="@+id/btnBookNow"
                style="@style/Widget.Button"
                android:layout_width="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_marginStart="@dimen/gen_mar_pad_twenty_five"
                android:layout_marginTop="@dimen/gen_mar_pad_twenty"
                android:layout_marginEnd="@dimen/gen_mar_pad_twenty_five"
                android:layout_marginBottom="@dimen/fab_margin"
                android:background="@drawable/rounded_corner_flexi_green"
                android:text="@string/str_book_bus"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_small"
                android:visibility="gone"
                app:theme="@style/RippleTheme" />

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

When I scroll to second postion of recyclerView rvRyBusList I hide llytBusFeaturesBig and NestedScrollView start scrolling but I want to stop the scrolling at that time. I am using below method to hide the layout while scrolling.

binding.smartBusListingLayout.nsvMain.setOnScrollChangeListener(object: NestedScrollView.OnScrollChangeListener {
            override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {

                showHideExculsiveFeatures()
            }
        })


private fun showHideExculsiveFeatures() {
        val scrollBounds = Rect()
        binding.smartBusListingLayout.nsvMain.getHitRect(scrollBounds)
        if (binding.smartBusListingLayout.rvRyBusList.getChildAt(2).getLocalVisibleRect(scrollBounds)) {
            //showBusList()
            binding.smartBusListingLayout.llytBusFeaturesBig.visibility = View.GONE
            binding.smartBusListingLayout.llytBusFeaturesSmall.visibility = View.VISIBLE
            showHideBtnFilterView()
        }
    }

Upvotes: 1

Views: 1611

Answers (2)

user12072760
user12072760

Reputation:

You may set setOnScrollChangeListeneron nested scroll view

NestedScrollView nsView=  findViewById(R.id.nsView);
nsView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            if (scrollY > oldScrollY) {
                Log.i("scroll", "Scroll DOWN....");
            }
            if (scrollY < oldScrollY) {
                //you can write here 
                Log.i("scroll", "Scroll UP.....");
            }

            if (scrollY == 0) {
                Log.i("scroll", "TOP SCROLL.....");
            }

           if (scrollY == ( v.getMeasuredHeight() - v.getChildAt(0).getMeasuredHeight() )) {
               Log.i("scroll", "BOTTOM SCROLL...");
           }
       }
    });

Upvotes: 0

V K
V K

Reputation: 65

You can use the VISIBILITY.INVISIBLE instead of GONE. something like this

 if (layout.getVisibility() == View.GONE)
                layout.setVisibility(View.VISIBLE);

Upvotes: 1

Related Questions