ashlok malik
ashlok malik

Reputation: 65

Vertical Parallax scrolling in android

I want to achieve parallax scrolling in android without using external library.Is there anyway to achieve this same as in google play store. Thanks

Upvotes: 1

Views: 1024

Answers (2)

Shally pathak
Shally pathak

Reputation: 79

For me the following method worked out .

In your android layout mention the below code:`

     <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:fitsSystemWindows="true">

      <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
      >

      <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:background="@color/white"
        >


        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >
    //place the widgets that you want to collapse .....(here i have used an 
       ImageView)

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <ImageView

                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:id="@+id/imgViewFirst"
                android:src="@drawable/firstImage"
                android:scaleType="fitXY"/>


            </LinearLayout>

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

      //place the widgets that you want to pin after scrolling up.....

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/lightGrey"/>


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

   //place the widgets you want to show after you scrolled upwards...(here i have 
       used an ImageView)

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/imgViewSecond"
            android:src="@drawable/secondImage"
            android:scaleType="fitXY"/>

    </android.support.v4.widget.NestedScrollView>

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


    </LinearLayout>`

Upvotes: 0

Jaysaw
Jaysaw

Reputation: 31

To implement that in a RecyclerView:

public class MyScrollListener extends RecyclerView.OnScrollListener {

    private int totalScrollDistance = 0;
    private View parallax;

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        totalScrollDistance += dy;
        if (parallax == null) {
            parallax = recyclerView.getChildAt(0);
        }
        parallax.setTranslationY(totalScrollDistance/2);
    }
}

Setup:

mRecyclerView.setOnScrollListener(new MyScrollListener());

To implement that in a ScrollView, you have to implement a OnScrollListener:

public class MyScrollView extends ScrollView {

    private int lastScrollY;

    private OnScrollListener listener;

    public void setOnScrollListener(OnScrollListener listener) {
        this.listener = listener;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int scrollY = getScrollY();
            if (lastScrollY != scrollY) {
                lastScrollY = scrollY;
                // update when lastScrollY != scrollY and keep sending message till lastScrollY == scrollY
                handler.sendMessageDelayed(handler.obtainMessage(), 5);
            }
            if (listener != null) {
                listener.onScroll(scrollY);
            }
        }
    };

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        lastScrollY = getScrollY();
        if (listener != null) {
            listener.onScroll(lastScrollY);
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            //might still scrolling after touching, use a handler to handle it
            handler.sendMessageDelayed(handler.obtainMessage(), 5);
        }
        return super.onTouchEvent(ev);
    }    

    public interface OnScrollListener {
        void onScroll(int dy);
    }
}

Then do what we did in OnScrollListener of RecyclerView:

scrollView = (MyScrollView) findViewById(R.id.my_scrollview);
    final View parallax = ((ViewGoup) scrollView.getChildAt(0)).getChildAt(0);
    scrollView.setOnScrollListener(new MyScrollView.OnScrollListener() {
        @Override
        public void onScroll(int dy) {
            parallax.setTranslationY(dy/2);
        }
    });

Upvotes: 3

Related Questions