Skip to content

Commit

Permalink
Created natural scroll handler
Browse files Browse the repository at this point in the history
  • Loading branch information
danylovolokh committed Dec 8, 2015
1 parent a817294 commit 26debd2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 56 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/com/volokh/danylo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
int screenWidth = getActivity().getResources().getDisplayMetrics().widthPixels;
int screenHeight = getActivity().getResources().getDisplayMetrics().heightPixels;

int circleRadius = (int) (screenWidth * 1.5);

int xOrigin = -screenWidth;
int yOrigin = 0;
// int circleRadius = screenHeight*2 + screenWidth/2;
// int circleRadius = (int) (screenWidth * 1.5);
//
// int xOrigin = -screenHeight*2 + screenWidth/4;
// int xOrigin = -screenWidth;
// int yOrigin = 0;
int circleRadius = screenHeight*2 + screenWidth/2;

int xOrigin = -screenHeight*2 + screenWidth/4;
int yOrigin = 0;
mRecyclerView = (DebugRecyclerView) rootView.findViewById(R.id.recycler_view);
mRecyclerView.setParameters(circleRadius, xOrigin, yOrigin);

Expand All @@ -118,7 +118,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
xOrigin,
yOrigin,
mRecyclerView,
IScrollHandler.Strategy.PIXEL_PERFECT);
IScrollHandler.Strategy.NATURAL);

mRecyclerView.setLayoutManager(mLondonEyeLayoutManager);//new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.volokh.danylo.layoutmanager.scroller;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.volokh.danylo.layoutmanager.circle_helper.quadrant_helper.QuadrantHelper;
import com.volokh.danylo.layoutmanager.layouter.Layouter;
Expand All @@ -10,14 +10,20 @@
*
* This scroll handler scrolls every view by the offset that user scrolled with his finger.
*/
public class NaturalScrollHandler implements IScrollHandler {
public class NaturalScrollHandler extends ScrollHandler {

public NaturalScrollHandler(ScrollHandlerCallback callback, QuadrantHelper quadrantHelper, Layouter layouter) {
private final ScrollHandlerCallback mCallback;

public NaturalScrollHandler(ScrollHandlerCallback callback, QuadrantHelper quadrantHelper, Layouter layouter) {
super(callback, quadrantHelper, layouter);
mCallback = callback;
}

@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler) {
return 0;
protected void scrollViews(View firstView, int delta) {
for (int indexOfView = 0; indexOfView < mCallback.getChildCount(); indexOfView++) {
View view = mCallback.getChildAt(indexOfView);
scrollSingleViewVerticallyBy(view, delta);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,28 @@ public class PixelPerfectScrollHandler extends ScrollHandler {
}

/**
* 1a. Shifts first view by "dy"
* 1b. Shifts all other views relatively to first view.
* 1. Shifts first view by "dy"
* 2. Shifts all other views relatively to first view.
*/
@Override
protected void scrollViews(View firstView, int delta) {
/**1a. */
Point firstViewNewCenter = scrollFirstViewVerticallyBy(firstView, delta);
/**1. */
Point firstViewNewCenter = scrollSingleViewVerticallyBy(firstView, delta);

ViewData previousViewData = new ViewData(
firstView.getTop(),
firstView.getBottom(),
firstView.getLeft(),
firstView.getRight(),
firstViewNewCenter);
/**1b. */

/**2. */
for (int indexOfView = 1; indexOfView < mCallback.getChildCount(); indexOfView++) {
View view = mCallback.getChildAt(indexOfView);
scrollSingleView(previousViewData, view);
}
}

/**
* This method calculates new position of first view and returns new center point of first view
*/
private Point scrollFirstViewVerticallyBy(View view, int indexOffset) {
if (SHOW_LOGS) Log.v(TAG, ">> scrollFirstViewVerticallyBy, indexOffset " + indexOffset);

int viewCenterX = view.getRight() - view.getWidth() / 2;
int viewCenterY = view.getTop() + view.getHeight() / 2;
SCROLL_HELPER_POINT.update(viewCenterX, viewCenterY);

int centerPointIndex = mQuadrantHelper.getViewCenterPointIndex(SCROLL_HELPER_POINT);

int newCenterPointIndex = mQuadrantHelper.getNewCenterPointIndex(centerPointIndex + indexOffset);

Point newCenterPoint = mQuadrantHelper.getViewCenterPoint(newCenterPointIndex);
if (SHOW_LOGS) Log.v(TAG, "scrollFirstViewVerticallyBy, viewCenterY " + viewCenterY);

int dx = newCenterPoint.getX() - viewCenterX;
int dy = newCenterPoint.getY() - viewCenterY;

view.offsetTopAndBottom(dy);
view.offsetLeftAndRight(dx);

return newCenterPoint;
}

private void scrollSingleView(ViewData previousViewData, View view) {
if (SHOW_LOGS) Log.v(TAG, "scrollSingleView, previousViewData " + previousViewData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.volokh.danylo.Config;
import com.volokh.danylo.layoutmanager.ViewData;
import com.volokh.danylo.layoutmanager.circle_helper.point.Point;
import com.volokh.danylo.layoutmanager.circle_helper.point.UpdatablePoint;
import com.volokh.danylo.layoutmanager.circle_helper.quadrant_helper.QuadrantHelper;
import com.volokh.danylo.layoutmanager.layouter.Layouter;
Expand Down Expand Up @@ -65,6 +66,32 @@ public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler) {
return -delta;
}

/**
* This method calculates new position of single view and returns new center point of first view
*/
protected Point scrollSingleViewVerticallyBy(View view, int indexOffset) {
if (SHOW_LOGS) Log.v(TAG, ">> scrollSingleViewVerticallyBy, indexOffset " + indexOffset);

int viewCenterX = view.getRight() - view.getWidth() / 2;
int viewCenterY = view.getTop() + view.getHeight() / 2;
SCROLL_HELPER_POINT.update(viewCenterX, viewCenterY);

int centerPointIndex = mQuadrantHelper.getViewCenterPointIndex(SCROLL_HELPER_POINT);

int newCenterPointIndex = mQuadrantHelper.getNewCenterPointIndex(centerPointIndex + indexOffset);

Point newCenterPoint = mQuadrantHelper.getViewCenterPoint(newCenterPointIndex);
if (SHOW_LOGS) Log.v(TAG, "scrollSingleViewVerticallyBy, viewCenterY " + viewCenterY);

int dx = newCenterPoint.getX() - viewCenterX;
int dy = newCenterPoint.getY() - viewCenterY;

view.offsetTopAndBottom(dy);
view.offsetLeftAndRight(dx);

return newCenterPoint;
}

/**
* This method recycles views:
* If views was scrolled down then it recycles top if needed and add views from the bottom
Expand Down
35 changes: 22 additions & 13 deletions app/src/main/res/layout/capsule_layout.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<com.volokh.danylo.Capsule xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center"
>
android:padding="50dp">

<TextView
android:id="@+id/capsule_name"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
/>
<com.volokh.danylo.Capsule xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
android:gravity="center">

</com.volokh.danylo.Capsule>
<TextView
android:id="@+id/capsule_name"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
/>

</com.volokh.danylo.Capsule>

</LinearLayout>

0 comments on commit 26debd2

Please sign in to comment.