Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fling not working for Nested ScrollView

I have implemented coordinator layout with nested scrollview. All working fine but nested scrollview is not giving smooth scroll on a fling.Here is my code:

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <RelativeLayout
                    android:id="@+id/imagesPagerLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:orientation="vertical"
     app:layout_collapseMode="parallax">
      <android.support.v4.view.ViewPager
                        android:id="@+id/imagesPager"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent"
                          android:fitsSystemWindows="true"/>

   <com.gcloud.gaadi.ui.viewpagerindicator.CirclePageIndicator
                        android:id="@+id/circlePagerIndicator"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:background="@android:color/transparent"
                        android:padding="2dp"
                        app:fillColor="@color/actionbar_background"
                        app:pageColor="@color/white"
                        app:radius="2dp"
                        app:strokeColor="@color/hintColor"
                        app:strokeWidth="1dp"/>
     </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/anim_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"

                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    >

                    <ImageView
                        android:id="@+id/iv_back"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="10dp"
                        android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha"/>
                </android.support.v7.widget.Toolbar>

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

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

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nestedScrollVw"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/time_label_size"
            android:layout_gravity="fill_vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
      <ImageView
                    android:id="@+id/trustmark"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/trustmarkCertified"
                    android:src="@drawable/trustmark_big"/>

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

                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"

                    android:layout_marginRight="8dp">
      <LinearLayout
                        android:id="@+id/makeModelColorLayout"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:baselineAligned="false"
                        android:orientation="horizontal">

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.7">

                            <ImageView
                                android:id="@+id/inr"
                                android:layout_width="25dp"
                                android:layout_height="25dp"
                                android:layout_centerVertical="true"
                                android:layout_marginLeft="4dp"
                                android:contentDescription="@string/emptyString"
                                android:padding="4dp"
                                android:src="@drawable/inr"/>

                            <TextView
                                android:id="@+id/stockPrice"
                                style="@style/textStyleHeading"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@+id/inr"
                                android:text="2.48 Lacs"
                                android:textSize="22sp"/>

                            <RelativeLayout
                                android:layout_width="@dimen/time_label_size"
                                android:layout_height="match_parent"
                                android:layout_alignParentRight="true"
                                android:layout_marginRight="8dp">

                                <ImageView
                                    android:id="@+id/stockColor"
                                    android:layout_width="15dp"
                                    android:layout_height="15dp"
                                    android:layout_centerHorizontal="true"
                                    android:layout_centerInParent="true"
                                    android:layout_marginTop="12dp"
                                    android:background="@drawable/oval_white_black_stroke"
                                    android:contentDescription="@string/emptyString"
                                    />

                                <TextView
                                    android:id="@+id/colorValue"
                                    style="@style/textStyleHeading2"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_alignParentBottom="true"
                                    android:layout_marginBottom="8dp"
                                    android:gravity="center_horizontal"
                                    android:text="Red"
                                    android:visibility="gone"/>

                            </RelativeLayout>
     </RelativeLayout>
       <View
                            android:layout_width="@dimen/separator_dimen"
                            android:layout_height="match_parent"
                            android:background="@color/strokeColor"/>

                        <RelativeLayout
                            android:id="@+id/totalLeadsLayout"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="0.3"
                            android:background="@drawable/leads_background_drawable">

                            <TextView
                                android:id="@+id/leadCount"
                                style="@style/textStyleHeading"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:layout_marginTop="8dp"
                                android:layout_toLeftOf="@+id/showLeads"
                                android:gravity="center"
                                android:padding="2dp"
                                android:text="8"
                                android:textColor="@color/white"
                                android:textSize="22sp"/>

                            <TextView
                                style="@style/textStyleHeading2.customFont"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_marginBottom="8dp"
                                android:layout_toLeftOf="@+id/showLeads"
                                android:gravity="center"
                                android:text="@string/leads"
                                android:textColor="@color/white"/>

                            <ImageView
                                android:id="@+id/showLeads"
                                android:layout_width="20dp"
                                android:layout_height="match_parent"
                                android:layout_alignParentRight="true"
                                android:contentDescription="@string/emptyString"
                                android:src="@drawable/show_more"/>
                            </RelativeLayout>
                           </LinearLayout>

                    <View
                        android:id="@+id/horizontalSeparator"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/separator_dimen"
                        android:layout_below="@+id/makeModelColorLayout"
                        android:background="@color/strokeColor"/>

                    <LinearLayout
                        android:id="@+id/pricekmsfuelLayout"
                        android:layout_width="match_parent"
                        android:layout_height="56dp"
                        android:layout_below="@+id/horizontalSeparator"
                        android:baselineAligned="false"
                        android:orientation="horizontal"
                        android:paddingBottom="4dp"
                        android:paddingTop="4dp">

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <ImageView
                                android:id="@+id/calendar"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:contentDescription="@string/emptyString"
                                android:padding="4dp"
                                android:src="@drawable/calendar"/>

                            <TextView
                                android:id="@+id/stockYear"
                                style="@style/textStyleHeading2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_below="@+id/calendar"
                                android:gravity="center"
                                android:text="2014"/>

                        </RelativeLayout>
                      <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <ImageView
                                android:id="@+id/kms"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:contentDescription="@string/emptyString"
                                android:padding="4dp"
                                android:src="@drawable/kms"/>

                            <TextView
                                android:id="@+id/kmsDriven"
                                style="@style/textStyleHeading2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_below="@+id/kms"
                                android:gravity="center"
                                android:text="50,000"/>

                        </RelativeLayout>

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <ImageView
                                android:id="@+id/fuel"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:padding="4dp"
                                android:src="@drawable/fuel"/>

                            <TextView
                                android:id="@+id/stockFuelType"
                                style="@style/textStyleHeading2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_below="@+id/fuel"
                                android:gravity="center"
                                android:text="Petrol"/>

                        </RelativeLayout>

                        <RelativeLayout
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <ImageView
                                android:id="@+id/ownershipImage"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_alignParentTop="true"
                                android:layout_centerHorizontal="true"
                                android:padding="4dp"
                                android:src="@drawable/leads"/>

                            <TextView
                                android:id="@+id/ownership"
                                style="@style/textStyleHeading2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_below="@+id/ownershipImage"
                                android:gravity="center"
                                android:text="Fourth"/>

                        </RelativeLayout>

                    </LinearLayout>

                </RelativeLayout>

                <View
                    android:id="@+id/separator"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/separator_dimen"
                    android:layout_below="@id/stockDetails"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@color/strokeColor"/>
                    <RelativeLayout
                    android:id="@+id/additionalDetails"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/separator"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"

                    android:layout_marginTop="8dp">

                    <RelativeLayout
                        android:id="@+id/color"
                        android:layout_width="match_parent"
                        android:layout_height="42dp"

                        android:padding="8dp">

                        <TextView
                            android:id="@+id/colorlabel"
                            style="@style/textStyleHeading2.customFont"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:text="Color"/>

                        <TextView
                            android:id="@+id/stockcolorvalue"
                            style="@style/textStyleHeading"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:text="White"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/registrationCityLayout"
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_below="@+id/color"
                        android:padding="8dp">

                        <TextView
                            android:id="@+id/registeredLabel"
                            style="@style/textStyleHeading2.customFont"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:text="Registration City"/>

                        <TextView
                            android:id="@+id/registrationCity"
                            style="@style/textStyleHeading"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:text="Mumbai"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/registrationNumberLayout"
                        android:layout_width="match_parent"
                        android:layout_height="42dp"
                        android:layout_below="@+id/registrationCityLayout"
                        android:padding="8dp">

                        <TextView
                            android:id="@+id/regNoLabel"
                            style="@style/textStyleHeading2.customFont"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:layout_toLeftOf="@+id/registrationNumber"
                            android:text="Registration No."/>

                        <TextView
                            android:id="@+id/registrationNumber"
                            style="@style/textStyleHeading"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:text="UP 14 BS 6463"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/postedOnLayout"
                        android:layout_width="match_parent"
                        android:layout_height="42dp"

                        android:layout_below="@+id/dealer2dealerLayout"
                        android:padding="8dp">

                        <TextView
                            android:id="@+id/postedOnLabel"
                            style="@style/textStyleHeading2.customFont"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:layout_toLeftOf="@+id/postedOn"
                            android:text="Posted On"/>

                        <TextView
                            android:id="@+id/postedOn"
                            style="@style/textStyleHeading"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"

                            android:layout_centerVertical="true"
                            android:text="Aug 1, 2012"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/taxLayout"
                        android:layout_width="match_parent"
                        android:layout_height="42dp"

                        android:layout_below="@+id/postedOnLayout"
                        android:padding="8dp">

                        <TextView
                            android:id="@+id/taxLabel"
                            style="@style/textStyleHeading2.customFont"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:layout_toLeftOf="@+id/taxValue"
                            android:text="Tax"/>

                        <TextView
                            android:id="@+id/taxValue"
                            style="@style/textStyleHeading"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"

                            android:layout_centerVertical="true"
                            android:text="Individual"/>

                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/insuraneLayout"
                        android:layout_width="match_parent"
                        android:layout_height="42dp"

                        android:layout_below="@+id/taxLayout"
                        android:padding="8dp">

                        <TextView
                            android:id="@+id/insuranceLabel"
                            style="@style/textStyleHeading2.customFont"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:layout_toLeftOf="@+id/insuranceValue"
                            android:text="Insurance"/>

                        <TextView
                            android:id="@+id/insuranceValue"
                            style="@style/textStyleHeading"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"

                            android:layout_centerVertical="true"
                            android:text="Third Party"/>
     </RelativeLayout>
     </RelativeLayout>
     </RelativeLayout>
      </android.support.v4.widget.NestedScrollView>
       </android.support.design.widget.CoordinatorLayout>

I have searched almost everywhere but no hints till yet. Thanks in advance !!

like image 936
Anupriya Avatar asked Aug 24 '15 06:08

Anupriya


1 Answers

Taken from here:

In the NestedScrollView, try to add

...

    <android.support.v4.widget.NestedScrollView

        android:id="@+id/mainSV"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1"

        android:fillViewport="true"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
...

and for your RecyclerView:

...
commentsRV.setNestedScrollingEnabled(false); // Disables scrolling 


    final CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    commentsRV.setLayoutManager(layoutManager);
...

and the CustomLinearLayoutManager

public class CustomLinearLayoutManager extends LinearLayoutManager {

    public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    private int[] mMeasuredDimension = new int[2];



    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                          int widthSpec, int heightSpec) {
        final int widthMode = View.MeasureSpec.getMode(widthSpec);
        final int heightMode = View.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);
        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            if (getOrientation() == HORIZONTAL) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);

                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);
                height = height + mMeasuredDimension[1];
                if (i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }
        switch (widthMode) {
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        switch (heightMode) {
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(width, height);
    }

    private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {
        View view = recycler.getViewForPosition(position);
        recycler.bindViewToPosition(view, position);
        if (view != null) {
            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                    getPaddingLeft() + getPaddingRight(), p.width);
            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                    getPaddingTop() + getPaddingBottom(), p.height);
            view.measure(childWidthSpec, childHeightSpec);
            measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
            recycler.recycleView(view);
        }
    }

}
like image 96
AbdulMomen عبدالمؤمن Avatar answered Nov 09 '22 21:11

AbdulMomen عبدالمؤمن