【问题标题】:NestedScrollView is not working with ViewPagerNestedScrollView 不适用于 ViewPager
【发布时间】:2021-04-30 19:14:00
【问题描述】:

NestedScrollView 不能与 ViewPager 一起使用

这是我的代码。你能建议或帮助我吗?当我在没有 ViewPager 的情况下尝试 NestedScrollView 时,它在其他页面上工作。这是我的布局文件。

如果需要,可以删除这个 RelativeLayout,因为我已经添加了这个

 <RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorAccent">

            <ImageView
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/back"
                android:layout_marginLeft="10dp"
                android:layout_centerVertical="true"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="मेरी प्रोफाइल"
                android:textColor="#ffffff"
                android:textSize="17sp"
                android:layout_centerInParent="true"/>

        </RelativeLayout>

使用了NestedScrollView

 <androidx.core.widget.NestedScrollView
        android:id="@+id/nest_scrollview"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:nestedScrollingEnabled="true"
            >

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="true"
                >

                <com.test.utils.RoundedImageView
                    android:id="@+id/iv_user_image"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    android:src="@drawable/uploadphoto"
                    android:tint="@color/colorPrimary"
                    android:layout_margin="10dp"/>

            <TextView
                android:id="@+id/tv_user_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@+id/iv_user_image"
                app:layout_constraintLeft_toRightOf="@+id/iv_user_image"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="20dp"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textAlignment="center"
                android:text="Hitesh"/>

            <TextView
                android:id="@+id/tv_user_phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@+id/tv_user_name"
                app:layout_constraintLeft_toRightOf="@+id/iv_user_image"
                android:textAlignment="center"

                android:layout_marginTop="5dp"
                android:layout_marginLeft="20dp"
                android:textColor="@color/quantum_orange"
                android:textSize="15sp"/>
         
            <TextView
                android:id="@+id/tv_address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                app:layout_constraintTop_toBottomOf="@+id/iv_user_image"
                app:layout_constraintLeft_toLeftOf="@+id/iv_user_image"
                android:layout_marginTop="20dp"

                android:textColor="@color/black"
                android:drawableLeft="@drawable/gglocation"


                android:text="Hitesh"/>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/cl_referral"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@+id/tv_address"
                android:background="@drawable/border_home_blue"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:padding="5dp">

                <ImageView
                    android:id="@+id/iv_referral"
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:src="@drawable/ic_trophy"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"/>

                <TextView
                    android:id="@+id/tv_referral_code"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toRightOf="@+id/iv_referral"
                    android:textColor="@color/black"
                    android:layout_marginLeft="5dp"
                    android:textAllCaps="true"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:text="Hitesh"/>

                <TextView
                    android:id="@+id/tv_hint"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toRightOf="@+id/tv_referral_code"
                    app:layout_constraintRight_toLeftOf="@+id/iv_whatsapp"
                    android:layout_margin="10dp"
                    android:textAllCaps="true"
                    android:textSize="12sp"
                    android:text="अपना रेफरल कोड शेयर करे"/>

                <ImageView
                    android:id="@+id/iv_whatsapp"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@drawable/ic_whatsapp"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    />


            </androidx.constraintlayout.widget.ConstraintLayout>
            <androidx.cardview.widget.CardView
                android:id="@+id/rateus"
                app:cardCornerRadius="6dp"
                android:layout_margin="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                app:layout_constraintTop_toBottomOf="@+id/cl_referral"
                app:layout_constraintLeft_toLeftOf="parent"
                android:nestedScrollingEnabled="true">
                <ImageView

                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:layout_centerVertical="true"
                    android:src="@drawable/rateus"

                    />
            </androidx.cardview.widget.CardView>
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@+id/rateus"
                app:tabSelectedTextColor="@color/colorPrimary"
                app:tabTextColor="@color/black"
                app:tabIndicatorColor="@color/colorPrimary"
                android:layout_marginTop="5dp"
                android:nestedScrollingEnabled="true"
                />

这是我的浏览页面

         <androidx.viewpager.widget.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintTop_toBottomOf="@id/tab_layout"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"


                    android:nestedScrollingEnabled="true"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

        </LinearLayout>

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

【问题讨论】:

    标签: android xml android-studio android-layout android-fragments


    【解决方案1】:

    应该由setFillViewport (true)解决:

    <android.support.v4.widget.NestedScrollView
    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"
    android:layout_width="match_parent"
    android:id="@+id/nest_scrollview"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="mio.kon.yyb.nestedscrollviewbug.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">
    
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    

    在你的activity 集合中:

      NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.nest_scrollview);
        scrollView.setFillViewport (true);
    

    【讨论】:

    • 试试看,我更新了我的答案。现在它应该可以工作了。
    • tools:showIn="@layout/activity_scrolling" 它给了我错误
    猜你喜欢
    • 2015-10-22
    • 2015-09-25
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2018-08-27
    • 1970-01-01
    相关资源
    最近更新 更多