Skip to content

Commit

Permalink
Merge pull request #50 from daisuke-nomura/master
Browse files Browse the repository at this point in the history
reverse layout
  • Loading branch information
lsjwzh committed Dec 27, 2015
2 parents 0f88312 + 0a6722f commit 23a8376
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
android:name=".MaterialDemoActivity"
android:theme="@style/Theme.DesignDemo">
</activity>
<activity
android:name=".ReverseSingleFlingPagerActivity" >
</activity>
<activity
android:name=".ReverseVerticalPagerActivity" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ void onClick() {
startActivity(new Intent(MainActivity.this, LoopPagerActivity.class));
}
});
mDemoListAdapter.add(new DemoItem("Reverse Single Fling Pager(like official ViewPager)") {
@Override
void onClick() {
startActivity(new Intent(MainActivity.this, ReverseSingleFlingPagerActivity.class));
}
});
mDemoListAdapter.add(new DemoItem("Reverse Vertical ViewPager Demo") {
@Override
void onClick() {
startActivity(new Intent(MainActivity.this, ReverseVerticalPagerActivity.class));
}
});
mDemoListAdapter.add(new DemoItem("3D effect Demo(TODO)") {
@Override
void onClick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.lsjwzh.widget.recyclerviewpagerdeomo;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager;

public class ReverseSingleFlingPagerActivity extends SingleFlingPagerActivity {
@Override
protected void initViewPager() {
mRecyclerView = (RecyclerViewPager)findViewById(R.id.viewpager);
LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
true);
mRecyclerView.setLayoutManager(layout);
mRecyclerView.setAdapter(new LayoutAdapter(this, mRecyclerView));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(true);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
// updateState(scrollState);
}

@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
// mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
int childCount = mRecyclerView.getChildCount();
int width = mRecyclerView.getChildAt(0).getWidth();
int padding = (mRecyclerView.getWidth() - width) / 2;
// mCountText.setText("Count: " + childCount);

for (int j = 0; j < childCount; j++) {
View v = recyclerView.getChildAt(j);
//往左 从 padding 到 -(v.getWidth()-padding) 的过程中,由大到小
float rate = 0;
;
if (v.getLeft() <= padding) {
if (v.getLeft() >= padding - v.getWidth()) {
rate = (padding - v.getLeft()) * 1f / v.getWidth();
} else {
rate = 1;
}
v.setScaleY(1 - rate * 0.1f);
v.setScaleX(1 - rate * 0.1f);

} else {
//往右 从 padding 到 recyclerView.getWidth()-padding 的过程中,由大到小
if (v.getLeft() <= recyclerView.getWidth() - padding) {
rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth();
}
v.setScaleY(0.9f + rate * 0.1f);
v.setScaleX(0.9f + rate * 0.1f);
}
}
}
});
mRecyclerView.addOnPageChangedListener(new RecyclerViewPager.OnPageChangedListener() {
@Override
public void OnPageChanged(int oldPosition, int newPosition) {
Log.d("test", "oldPosition:" + oldPosition + " newPosition:" + newPosition);
}
});

mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (mRecyclerView.getChildCount() < 3) {
if (mRecyclerView.getChildAt(1) != null) {
if (mRecyclerView.getCurrentPosition() == 0) {
View v1 = mRecyclerView.getChildAt(1);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
} else {
View v1 = mRecyclerView.getChildAt(0);
v1.setScaleY(0.9f);
v1.setScaleX(0.9f);
}
}
} else {
if (mRecyclerView.getChildAt(0) != null) {
View v0 = mRecyclerView.getChildAt(0);
v0.setScaleY(0.9f);
v0.setScaleX(0.9f);
}
if (mRecyclerView.getChildAt(2) != null) {
View v2 = mRecyclerView.getChildAt(2);
v2.setScaleY(0.9f);
v2.setScaleX(0.9f);
}
}

}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.lsjwzh.widget.recyclerviewpagerdeomo;

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

import com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager;

public class ReverseVerticalPagerActivity extends VerticalPagerActivity {
@Override
protected void initViewPager() {
mRecyclerView = (RecyclerViewPager) findViewById(R.id.viewpager);

LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
true);
mRecyclerView.setTriggerOffset(0.15f);
mRecyclerView.setFlingFactor(0.25f);
mRecyclerView.setLayoutManager(layout);
mRecyclerView.setAdapter(new LayoutAdapter(this, mRecyclerView));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(true);

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

}

@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
// mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
int childCount = mRecyclerView.getChildCount();
int width = mRecyclerView.getChildAt(0).getWidth();
int padding = (mRecyclerView.getWidth() - width) / 2;

for (int j = 0; j < childCount; j++) {
View v = recyclerView.getChildAt(j);
//往左 从 padding 到 -(v.getWidth()-padding) 的过程中,由大到小
float rate = 0;
if (v.getLeft() <= padding) {
if (v.getLeft() >= padding - v.getWidth()) {
rate = (padding - v.getLeft()) * 1f / v.getWidth();
} else {
rate = 1;
}
v.setScaleY(1 - rate * 0.1f);
} else {
//往右 从 padding 到 recyclerView.getWidth()-padding 的过程中,由大到小
if (v.getLeft() <= recyclerView.getWidth() - padding) {
rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth();
}
v.setScaleY(0.9f + rate * 0.1f);
}
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class RecyclerViewPager extends RecyclerView {
int mMinTopWhenDragging = Integer.MAX_VALUE;
private int mPositionOnTouchDown = -1;
private boolean mHasCalledOnPageChanged = true;
private boolean reverseLayout = false;

public RecyclerViewPager(Context context) {
this(context, null);
Expand Down Expand Up @@ -138,6 +140,15 @@ public RecyclerViewPagerAdapter getWrapperAdapter() {
return mViewPagerAdapter;
}

@Override
public void setLayoutManager(LayoutManager layout) {
super.setLayoutManager(layout);

if (layout instanceof LinearLayoutManager) {
reverseLayout = ((LinearLayoutManager) layout).getReverseLayout();
}
}

@Override
public boolean fling(int velocityX, int velocityY) {
boolean flinging = super.fling((int) (velocityX * mFlingFactor), (int) (velocityY * mFlingFactor));
Expand Down Expand Up @@ -216,6 +227,8 @@ public int getCurrentPosition() {
* adjust position before Touch event complete and fling action start.
*/
protected void adjustPositionX(int velocityX) {
if (reverseLayout) velocityX *= -1;

int childCount = getChildCount();
if (childCount > 0) {
int curPosition = ViewUtils.getCenterXChildPosition(this);
Expand All @@ -239,9 +252,11 @@ protected void adjustPositionX(int velocityX) {
View centerXChild = ViewUtils.getCenterXChild(this);
if (centerXChild != null) {
if (mTouchSpan > centerXChild.getWidth() * mTriggerOffset * mTriggerOffset && targetPosition != 0) {
targetPosition--;
if (!reverseLayout) targetPosition--;
else targetPosition++;
} else if (mTouchSpan < centerXChild.getWidth() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
targetPosition++;
if (!reverseLayout) targetPosition++;
else targetPosition --;
}
}
}
Expand Down Expand Up @@ -276,6 +291,8 @@ public void clearOnPageChangedListeners() {
* adjust position before Touch event complete and fling action start.
*/
protected void adjustPositionY(int velocityY) {
if (reverseLayout) velocityY *= -1;

int childCount = getChildCount();
if (childCount > 0) {
int curPosition = ViewUtils.getCenterYChildPosition(this);
Expand All @@ -296,9 +313,11 @@ protected void adjustPositionY(int velocityY) {
View centerYChild = ViewUtils.getCenterYChild(this);
if (centerYChild != null) {
if (mTouchSpan > centerYChild.getHeight() * mTriggerOffset && targetPosition != 0) {
targetPosition--;
if (!reverseLayout) targetPosition--;
else targetPosition++;
} else if (mTouchSpan < centerYChild.getHeight() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
targetPosition++;
if (!reverseLayout) targetPosition++;
else targetPosition--;
}
}
}
Expand Down Expand Up @@ -381,16 +400,20 @@ public void onScrollStateChanged(int state) {
int spanX = mCurView.getLeft() - mFisrtLeftWhenDragging;
// if user is tending to cancel paging action, don't perform position changing
if (spanX > mCurView.getWidth() * mTriggerOffset && mCurView.getLeft() >= mMaxLeftWhenDragging) {
targetPosition--;
if (!reverseLayout) targetPosition--;
else targetPosition ++;
} else if (spanX < mCurView.getWidth() * -mTriggerOffset && mCurView.getLeft() <= mMinLeftWhenDragging) {
targetPosition++;
if (!reverseLayout) targetPosition++;
else targetPosition--;
}
} else {
int spanY = mCurView.getTop() - mFirstTopWhenDragging;
if (spanY > mCurView.getHeight() * mTriggerOffset && mCurView.getTop() >= mMaxTopWhenDragging) {
targetPosition--;
if (!reverseLayout) targetPosition--;
else targetPosition++;
} else if (spanY < mCurView.getHeight() * -mTriggerOffset && mCurView.getTop() <= mMinTopWhenDragging) {
targetPosition++;
if (!reverseLayout) targetPosition++;
else targetPosition--;
}
}
}
Expand Down

0 comments on commit 23a8376

Please sign in to comment.