nestedScrollview中的平滑滚动缺少y位置

时间:2018-08-20 11:02:31

标签: android android-nestedscrollview

我有一个包含多个布局的图层 在我图层的顶部,有TabLayout和AppbarLayout。

我想在单击选项卡项目后,平滑滚动到该位置,但是在缺少y滚动位置之后。

  

我的方法:

 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            switch (tab.getPosition()) {
                case 3:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, 0));
                    break;
                case 2:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, includeAttributes.getTop()));
                    break;
                case 1:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, includeIntroduction.getTop()));
                    break;
                case 0:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, includeReview.getTop()));
            }

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

            switch (tab.getPosition()) {
                case 3:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, 0));
                    break;
                case 2:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, includeAttributes.getTop()));
                    break;
                case 1:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, includeIntroduction.getTop()));
                    break;
                case 0:
                    nestedScrollView.post(() -> nestedScrollView.smoothScrollTo(1, includeReview.getTop()));
            }

        }
    });
  

我的图层::: >>>>>

(多个文件)

  

fragment_product.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.product.ProductFragment">




<android.support.design.widget.AppBarLayout
    android:id="@+id/fragment_product_appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:visibility="gone">

    <include layout="@layout/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/fragment_product_tabs_tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:background="@color/colorPrimary"
        android:elevation="10dp"
        app:tabTextColor="@color/white"
        app:tabSelectedTextColor="@color/white"
        app:tabIndicatorColor="@color/colorPrimary"
        tools:targetApi="lollipop"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        app:layout_scrollFlags="scroll|enterAlways"
        fontPath="@string/font_path"/>


</android.support.design.widget.AppBarLayout>



<android.support.v4.widget.ContentLoadingProgressBar
    android:id="@+id/fragment_product_main_contnetloadingprogressbar"
    android:layout_width="wrap_content"
    style="?android:attr/progressBarStyle"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<include layout="@layout/fragment_product_content" />

  

fragment_product_content.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/product_scroll_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:visibility="gone">




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




    <include
        android:id="@+id/product_include_slider"
        layout="@layout/fragment_product_content_slider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <include
        android:id="@+id/product_include_desc"
        layout="@layout/fragment_product_content_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_include_slider" />

    <include
        android:id="@+id/product_include_vendor"
        layout="@layout/fragment_product_content_vendor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_include_desc"
        android:layout_marginTop="20dp" />


    <include
        android:id="@+id/product_include_attributes"
        layout="@layout/fragment_product_content_attribute"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/product_include_vendor"
        android:layout_marginTop="20dp" />


    <include
        android:id="@+id/product_include_introduction"
        layout="@layout/fragment_product_content_introduction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/product_include_attributes"
        android:layout_marginTop="20dp" />


    <include
        android:id="@+id/product_include_review"
        layout="@layout/fragment_product_content_review"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/product_include_introduction"
        android:layout_marginTop="20dp" />


    <include
        android:id="@+id/product_include_products"
        layout="@layout/fragment_product_content_products"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/product_include_review"
        android:layout_marginTop="20dp" />


    <include
        android:id="@+id/product_include_related"
        layout="@layout/fragment_product_content_related"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/product_include_products"
        android:layout_marginTop="20dp" />


    <include
        android:id="@+id/product_include_add_to_cart"
        layout="@layout/fragment_product_content_add_to_cart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/product_include_related"
        android:layout_marginTop="50dp" />

  

fragment_product_content_attribute.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/fragment_product_attributes_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/product_attributes"
        android:textColor="@color/colorTextDark"
        android:textSize="@dimen/attributes_product"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/fragment_product_attributes_more_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_keyboard_arrow_left"
        android:drawableStart="@drawable/ic_keyboard_arrow_left"
        android:drawableTint="@color/more_product"
        android:focusable="true"
        android:foreground="?android:selectableItemBackground"
        android:text="@string/more"
        android:textColor="@color/more_product"
        android:textSize="@dimen/more_product"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="m" />


    <LinearLayout
        android:id="@+id/fragment_product_attributes_line_linearlayout"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/divider"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment_product_attributes_textview"
        android:orientation="horizontal" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_product_attributes_list_recyclerview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clipToPadding="false"
        android:nestedScrollingEnabled="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment_product_attributes_line_linearlayout"
        tools:targetApi="lollipop" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment_product_attributes_line_linearlayout"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/ic_noun_weight"
        android:contentDescription="@string/todo" />

</android.support.constraint.ConstraintLayout>

  

fragment_product_content_review.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="wrap_content">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    // other view ....


    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_product_list_review_recyclerview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:clipToPadding="false"
        android:nestedScrollingEnabled="false"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment_product_write_review_button"
        tools:targetApi="lollipop" />


        // other view ....



</android.support.constraint.ConstraintLayout>

  

fragment_product_content_products.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_product_list_product_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment_product_vendor_name_products_textview"
        android:clipToPadding="false"
        android:nestedScrollingEnabled="false"
        tools:targetApi="lollipop" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text="@string/with_management"
        android:textColor="@color/colorTextHint"
        android:textSize="@dimen/product_from_vendor_product"
        app:layout_constraintBottom_toBottomOf="@+id/textView15"
        app:layout_constraintEnd_toStartOf="@+id/textView15"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView15"
        app:layout_constraintVertical_bias="1.0" />

    <ir.basalam.app.utils.SquareCardView
        android:id="@+id/cardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:elevation="10dp"
        app:cardCornerRadius="@dimen/margin_side"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:targetApi="lollipop">


        <ImageView
            android:id="@+id/fragment_product_vendor_logo_products_imageview"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@mipmap/ic_launcher"
            android:contentDescription="@string/todo" />

    </ir.basalam.app.utils.SquareCardView>

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/product_from_vendor"
        android:textColor="@color/colorTextHint"
        android:textSize="@dimen/product_from_vendor_product"
        app:layout_constraintBottom_toBottomOf="@+id/cardView"
        app:layout_constraintEnd_toStartOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="@+id/cardView"
        app:layout_constraintVertical_bias="0.0" />


    <TextView
        android:id="@+id/fragment_product_person_name_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/vendor_management_name"
        android:textColor="@color/colorTextPrimary"
        android:textSize="@dimen/vendor_name_product"
        app:layout_constraintEnd_toEndOf="@+id/textView3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/fragment_product_vendor_name_products_textview" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/fragment_product_person_cover_circleimageview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:elevation="10dp"
        android:src="@drawable/no_avatar"
        app:layout_constraintBottom_toBottomOf="@+id/cardView"
        app:layout_constraintStart_toEndOf="@+id/fragment_product_person_name_textview"
        app:layout_constraintTop_toTopOf="@+id/cardView"
        tools:targetApi="lollipop" />


    <Button
        android:id="@+id/fragment_product_all_product_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/outline_button"
        android:clickable="true"
        android:drawableLeft="@drawable/ic_keyboard_arrow_left"
        android:drawableStart="@drawable/ic_keyboard_arrow_left"
        android:drawableTint="@color/colorPrimary"
        android:elevation="5dp"
        android:focusable="true"
        android:foreground="?android:selectableItemBackground"
        android:padding="5dp"
        android:text="@string/see_another_product_of_vendor"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/see_all_review_product"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fragment_product_list_product_recyclerview"
        tools:targetApi="m" />

    <TextView
        android:id="@+id/fragment_product_vendor_name_products_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/vendor_name"
        android:textColor="@color/colorTextPrimary"
        android:textSize="@dimen/vendor_name_product"
        app:layout_constraintEnd_toStartOf="@+id/cardView"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/fragment_product_person_cover_circleimageview"
        app:layout_constraintTop_toBottomOf="@+id/textView15" />


</android.support.constraint.ConstraintLayout>

  

fragment_product_content_related.xml

`

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

<TextView
    android:id="@+id/textView19"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="@string/related_product"
    android:textColor="@color/colorTextPrimary"
    android:textSize="@dimen/related_product"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.972"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/fragment_product_list_related_recyclerview"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView19"
    android:clipToPadding="false"
    android:nestedScrollingEnabled="false"
    tools:targetApi="lollipop" />

0 个答案:

没有答案