Skip to content Skip to sidebar Skip to footer

Nestedscrollview + Coodinatorlayout Scrollby() Scrollto() Methods Do Nothing

I have a NestedScrollView being used with CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout with parallax effect similar to this tutorial I need to scroll the content prog

Solution 1:

How do I scroll like this programmatically?

For that scrolling behavior you need collapse or expand CollapsingToolbarLayout no need to scroll your the NestedScrollView

Here is the sample code for that

Try this Make some below changes in your Layout

<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns: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:id="@+id/rootView"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"><com.google.android.material.appbar.AppBarLayoutandroid:id="@+id/app_bar"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/transparent"android:fitsSystemWindows="true"><com.google.android.material.appbar.CollapsingToolbarLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"app:expandedTitleMarginEnd="64dp"app:expandedTitleMarginStart="48dp"app:layout_scrollFlags="scroll|exitUntilCollapsed"><ImageViewandroid:id="@+id/iv_image"android:layout_width="match_parent"android:layout_height="250dp"android:adjustViewBounds="true"android:scaleType="fitXY"android:src="@drawable/goku"app:layout_collapseMode="parallax"app:layout_collapseParallaxMultiplier="0.7" /><androidx.appcompat.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"app:layout_collapseMode="pin"app:contentInsetLeft="0dp"app:contentInsetStart="0dp"app:contentInsetStartWithNavigation="0dp"app:titleTextAppearance="@style/AppTheme.Toolbar.Title"app:popupTheme="@style/AppTheme.PopupOverlay" /></com.google.android.material.appbar.CollapsingToolbarLayout></com.google.android.material.appbar.AppBarLayout><com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@color/Boxcolordiabled"app:layout_anchor="@id/app_bar"app:layout_anchorGravity="bottom|end"app:srcCompat="@drawable/ic_favorite" /><androidx.core.widget.NestedScrollViewandroid:id="@+id/nsv_form"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/holo_blue_light"app:layout_behavior="@string/appbar_scrolling_view_behavior"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:animateLayoutChanges="true"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><Buttonandroid:id="@+id/btnColl"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="Expand " /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"android:text="@string/demo" /></LinearLayout></androidx.core.widget.NestedScrollView></androidx.coordinatorlayout.widget.CoordinatorLayout>

Activity code

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import neel.com.bottomappbar.R;

publicclassMainActivityextendsAppCompatActivity {

    Toolbar toolbar;
    AppBarLayout app_bar;
    Button btnColl;
    FloatingActionButton fab;
    CoordinatorLayout rootView;

    @OverrideprotectedvoidonCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Stack Demo");

        app_bar = findViewById(R.id.app_bar);
        btnColl = findViewById(R.id.btnColl);
        fab = findViewById(R.id.fab);
        rootView = findViewById(R.id.rootView);


        fab.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {

                Toast.makeText(MainActivity.this, "Collapse FAB Clicked", Toast.LENGTH_SHORT).show();

                CoordinatorLayout.LayoutParamsparams= (CoordinatorLayout.LayoutParams) app_bar.getLayoutParams();
                final AppBarLayout.Behaviorbehavior= (AppBarLayout.Behavior) params.getBehavior();

                if (behavior != null) {
                    ValueAnimatorvalueAnimator= ValueAnimator.ofInt();
                    valueAnimator.setInterpolator(newDecelerateInterpolator());

                    valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
                        @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
                            behavior.setTopAndBottomOffset((Integer) animation.getAnimatedValue());
                            app_bar.requestLayout();
                        }
                    });

                    valueAnimator.setIntValues(0, -900);
                    valueAnimator.setDuration(1000);
                    valueAnimator.start();
                }
            }
        });

        btnColl.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {

                Toast.makeText(MainActivity.this, "Expand btnColl Clicked", Toast.LENGTH_SHORT).show();

                CoordinatorLayout.LayoutParamsparams= (CoordinatorLayout.LayoutParams) app_bar.getLayoutParams();
                final AppBarLayout.Behaviorbehavior= (AppBarLayout.Behavior) params.getBehavior();
                if (behavior != null) {

                    ValueAnimatorvalueAnimator= ValueAnimator.ofInt();
                    valueAnimator.setInterpolator(newDecelerateInterpolator());

                    valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
                        @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
                            behavior.setTopAndBottomOffset((Integer) animation.getAnimatedValue());
                            app_bar.requestLayout();
                        }
                    });

                    valueAnimator.setIntValues(-900, 0);
                    valueAnimator.setDuration(400);
                    valueAnimator.start();
                }


            }
        });
    }


}

OUTPUT

https://www.youtube.com/watch?v=nZY1zPxjRt0

Solution 2:

Scrolls (scrollBy()/scrollTo()/smoothScrollTo()/smoothScrollBy()) needs to be called from UI thread.

In Kotlin, you can use

Handler().post {
    nsv_form.scrollBy(0, 300)
}

Post a Comment for "Nestedscrollview + Coodinatorlayout Scrollby() Scrollto() Methods Do Nothing"