当我们从中隐藏布局时,如何阻止Nestedscrollview滚动

时间:2019-09-16 06:11:49

标签: android scroll nestedscrollview

我有一个Nestedscrollview,当我向上滚动时,我必须隐藏一个当前未在屏幕上显示的视图。但是当我隐藏它时,Nestedscrollview会自动将自身滚动到adujst。我想阻止它滚动直到用户再次手动滚动

<?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>

当我滚动到recyclerView rvRyBusList的第二个位置时,我隐藏了llytBusFeaturesBig和NestedScrollView开始滚动,但是我想在那时停止滚动。我正在使用以下方法在滚动时隐藏布局。

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()
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以使用VISIBILITY.INVISIBLE代替GONE。 像这样

return su.getText()

答案 1 :(得分:0)

您可以在嵌套滚动视图上设置setOnScrollChangeListener

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...");
           }
       }
    });