-
Notifications
You must be signed in to change notification settings - Fork 2
/
DynamicPageTransformer.java
103 lines (82 loc) · 3.7 KB
/
DynamicPageTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.mogujie.socialsdk.feed.adapter;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import com.mogujie.lifetag.TagShowLayout;
import java.util.List;
public class DynamicPageTransformer implements ViewPager.PageTransformer {
private ViewPager mPager;
private List<Integer> mHeightList;
private IndexMultiImageAdapter mAdapter;
private int pagerWidth;
public DynamicPageTransformer(ViewPager viewPager, List<Integer> heightList, int pagerWidth) {
mPager = viewPager;
mHeightList = heightList;
setDefaultHeight();
this.pagerWidth = pagerWidth;
mAdapter = (IndexMultiImageAdapter) mPager.getAdapter();
}
public void setDefaultHeight() {
ViewGroup.LayoutParams params = mPager.getLayoutParams();
if (mHeightList != null && mHeightList.size() > 0)
params.height = mHeightList.get(0);
mPager.setLayoutParams(params);
}
private int getHeight(int position) {
return position >= 0 && position < mHeightList.size() ? mHeightList.get(position) : 0;
}
float lastPosition = -1;
int base = 0;
final float valve = 0.5f;
final int LEFT_SLIDING = -1;
final int RIGHT_SLIDING = 1;
final int IDLE = 0;
int sliding_state = IDLE;
@Override
public void transformPage(View page, float position) {
if (position >= -1 && position <= 0) {
int curr = mPager.getCurrentItem();
int gap = 0;
if (getHeight(curr) > 0) {
if (lastPosition != 0 && lastPosition != -1 && Math.abs(lastPosition - position) < 0.5f) {
if (lastPosition > position) { //左滑 <- 右移
if (sliding_state == RIGHT_SLIDING)
base = base - 1;
if (getHeight(base + 1) != 0 && getHeight(base) != 0)
gap = getHeight(base + 1) - getHeight(base);
sliding_state = LEFT_SLIDING;
} else if (lastPosition < position) {//右滑 -> 左移
if (sliding_state == LEFT_SLIDING)
base = base + 1;
if (getHeight(base - 1) != 0 && getHeight(base) != 0)
gap = getHeight(base - 1) - getHeight(base);
sliding_state = RIGHT_SLIDING;
}
}
}
ViewGroup.LayoutParams params = mPager.getLayoutParams();
if (position == 0 || position == -1 || Math.abs(lastPosition - position) > valve) {
base = curr;
params.height = getHeight(base);
sliding_state = IDLE;
} else if (gap != 0) {
if (Math.abs(lastPosition - position) < valve) {
if (lastPosition < position) {
params.height = (int) ((1 + position) * gap + getHeight(base));
} else if (lastPosition > position) {
params.height = (int) ((-position) * gap + getHeight(base));
} else
lastPosition = 0;
}
}
lastPosition = position;
mPager.setLayoutParams(params);
if (mPager.getCurrentItem() >= 0 && mPager.getCurrentItem() < mHeightList.size() && mPager.getCurrentItem() < mAdapter.mViewArray.size()) {
TagShowLayout itemView = mAdapter.mViewArray.get(mPager.getCurrentItem());
Rect rect = new Rect(0, 0, pagerWidth, mHeightList.get(mPager.getCurrentItem()));
itemView.setBoundary(rect);
}
}
}
}