Skip to content

Commit

Permalink
feat:support ItemDecorations
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjwzh committed Jan 24, 2016
1 parent c0caee8 commit 3211f78
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void initViewPager() {
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(true);

mRecyclerView.addItemDecoration(new SpacesItemDecoration(50, mRecyclerView.getAdapter().getItemCount()));
mRecyclerView.addOnPageChangedListener(new RecyclerViewPager.OnPageChangedListener() {
@Override
public void OnPageChanged(int oldPosition, int newPosition) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lsjwzh.widget.recyclerviewpagerdeomo;

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

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private final int mColumnCount;
private final int mSpace;

public SpacesItemDecoration(int space, int columnCount) {
this.mSpace = space;
this.mColumnCount = columnCount;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.bottom = mSpace;
// Add top margin only for the first item to avoid double mSpace between items
if (parent.getChildLayoutPosition(view) == mColumnCount - 1) {
outRect.right = 0;
} else {
outRect.right = mSpace / 2;
}
if (parent.getChildLayoutPosition(view) == 0) {
outRect.left = 0;
} else {
outRect.left = mSpace / 2;
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_cheese_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent" />
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
Expand Down Expand Up @@ -173,7 +175,54 @@ public void smoothScrollToPosition(int position) {
Log.d("@", "smoothScrollToPosition:" + position);
}
mSmoothScrollTargetPosition = position;
super.smoothScrollToPosition(position);
if (getLayoutManager() != null && getLayoutManager() instanceof LinearLayoutManager) {
// exclude item decoration
LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(getContext()) {
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
if (getLayoutManager() == null) {
return null;
}
return ((LinearLayoutManager) getLayoutManager())
.computeScrollVectorForPosition(targetPosition);
}

@Override
protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
if (getLayoutManager() == null) {
return;
}
int dx = calculateDxToMakeVisible(targetView,
getHorizontalSnapPreference());
int dy = calculateDyToMakeVisible(targetView,
getVerticalSnapPreference());
if (dx > 0) {
dx = dx - getLayoutManager()
.getLeftDecorationWidth(targetView);
} else {
dx = dx + getLayoutManager()
.getRightDecorationWidth(targetView);
}
if (dy > 0) {
dy = dy - getLayoutManager()
.getTopDecorationHeight(targetView);
} else {
dy = dy + getLayoutManager()
.getBottomDecorationHeight(targetView);
}
final int distance = (int) Math.sqrt(dx * dx + dy * dy);
final int time = calculateTimeForDeceleration(distance);
if (time > 0) {
action.update(-dx, -dy, time, mDecelerateInterpolator);
}
}
};
linearSmoothScroller.setTargetPosition(position);
getLayoutManager().startSmoothScroll(linearSmoothScroller);
} else {
super.smoothScrollToPosition(position);
}
}

@Override
Expand Down Expand Up @@ -256,7 +305,7 @@ protected void adjustPositionX(int velocityX) {
else targetPosition++;
} else if (mTouchSpan < centerXChild.getWidth() * -mTriggerOffset && targetPosition != mViewPagerAdapter.getItemCount() - 1) {
if (!reverseLayout) targetPosition++;
else targetPosition --;
else targetPosition--;
}
}
}
Expand Down Expand Up @@ -401,7 +450,7 @@ public void onScrollStateChanged(int state) {
// if user is tending to cancel paging action, don't perform position changing
if (spanX > mCurView.getWidth() * mTriggerOffset && mCurView.getLeft() >= mMaxLeftWhenDragging) {
if (!reverseLayout) targetPosition--;
else targetPosition ++;
else targetPosition++;
} else if (spanX < mCurView.getWidth() * -mTriggerOffset && mCurView.getLeft() <= mMinLeftWhenDragging) {
if (!reverseLayout) targetPosition++;
else targetPosition--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

public class TabLayoutSupport {

Expand Down Expand Up @@ -76,9 +77,12 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
final int pagerWidth = recyclerView.getWidth()
- recyclerView.getPaddingLeft()
- recyclerView.getPaddingRight();
int centerChildPosition = viewPager.getChildAdapterPosition(ViewUtils.getCenterXChild
(viewPager));
float offset = mPagerLeftBeforeScroll - ViewUtils.getCenterXChild(viewPager).getLeft()
final View centerXChild = ViewUtils.getCenterXChild(viewPager);
if (centerXChild == null) {
return;
}
int centerChildPosition = viewPager.getChildAdapterPosition(centerXChild);
float offset = mPagerLeftBeforeScroll - centerXChild.getLeft()
+ pagerWidth * (centerChildPosition - mPositionBeforeScroll);
final float positionOffset = offset * 1f / pagerWidth;
if (tabLayout != null) {
Expand Down

0 comments on commit 3211f78

Please sign in to comment.