Skip to content

Commit 783994b

Browse files
author
uis
committed
Merge branch 'dev' of https://github.com/luiing/StackViewLayout into dev
2 parents b9aec92 + 25d511e commit 783994b

6 files changed

Lines changed: 64 additions & 63 deletions

File tree

demo/src/main/java/com/uis/stackviewlayout/demo/activity/StackAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public int getItemViewType(int position) {
4444

4545
@Override
4646
public int getItemCount() {
47-
return 20;
47+
return 2;
4848
}
4949

5050
public static ArrayList<ItemEntity> initDataList(Context context) {

demo/src/main/res/layout/item_layout.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:background="@drawable/default_back"
6-
android:layout_height="match_parent"
7-
>
8-
9-
6+
android:layout_height="wrap_content">
107
<com.makeramen.roundedimageview.RoundedImageView
118
android:id="@+id/imageView"
129
android:layout_width="match_parent"
1310
android:layout_height="match_parent"
14-
android:scaleType="center"
11+
android:scaleType="centerCrop"
1512
app:riv_corner_radius="5dp" />
1613

1714
<ImageView

demo/src/main/res/layout/stack_left.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
xmlns:stack="http://schemas.android.com/apk/res-auto"
44
android:id="@+id/stacklayout"
55
android:layout_width="match_parent"
6-
android:layout_height="150dp"
7-
stack:stackAspectRatio="0"
8-
stack:stackPaddingX="10dp"
6+
android:layout_height="wrap_content"
7+
stack:stackAspectRatio="2"
8+
stack:stackPaddingX="15dp"
99
stack:stackOffsetX="2dp"
10-
stack:stackPaddingY="10dp"
11-
stack:stackOffsetY="2dp"
10+
stack:stackPaddingY="20dp"
11+
stack:stackOffsetY="5dp"
1212
stack:stackEdge="10dp"
1313
stack:stackSize = "3"
14-
stack:stackModel="left">
14+
stack:stackModel="right">
1515
</com.uis.stackviewlayout.StackViewLayout>

demo/src/main/res/layout/stack_right.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
android:id="@+id/stacklayout"
55
android:layout_width="match_parent"
66
android:layout_height="150dp"
7+
stack:stackSpace="10dp"
78
stack:stackEdge="5dp"
89
stack:stackSize="3">
910

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
5+
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
9+
<item name="android:windowTranslucentNavigation">true</item>
10+
<item name="android:windowTranslucentStatus">true</item>
11+
</style>
12+
</resources>

stacklayout/src/main/java/com/uis/stackviewlayout/StackViewLayout.java

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class StackViewLayout extends ViewGroup{
5050
private int mDelay = 3000;
5151
private int childWidthMeasure,childHeightMeasure;
5252
private boolean mIsDragged = false;
53-
private List<Integer> padxArray = new ArrayList<>(stackSize);
53+
private List<Integer> xSpace = new ArrayList<>(stackSize);
54+
private List<Integer> ySpace = new ArrayList<>(stackSize);
5455

5556
public StackViewLayout(Context context) {
5657
this(context, null);
@@ -74,6 +75,15 @@ public StackViewLayout(Context context, AttributeSet attrs, int defStyleAttr) {
7475
offsetY = (int)type.getDimension(R.styleable.StackViewLayout_stackOffsetY,offsetY);
7576
type.recycle();
7677

78+
boolean isLeft = MODEL_LEFT == stackModel;
79+
for(int i=0,size=stackSize;i<size;i++){
80+
int revert = size-1-i;
81+
int dx = isLeft ? getSeriesSum(paddingX-revert*offsetX,offsetX,i):getSeriesSum(paddingX,offsetX,revert);
82+
int dy = getSeriesSum(paddingY,offsetY,revert);
83+
xSpace.add(dx);
84+
ySpace.add(dy);
85+
}
86+
7787
ViewConfiguration configuration = ViewConfiguration.get(getContext());
7888
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
7989
mTouchSlop = configuration.getScaledTouchSlop();
@@ -101,74 +111,51 @@ public int getItemCount() {
101111
@Override
102112
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
103113
int width = getDefaultSize(0, widthMeasureSpec);
104-
int height = getDefaultSize(0,heightMeasureSpec);
114+
int height = getDefaultSize(width/2,heightMeasureSpec);
105115
childWidthMeasure = width-2*edge-getSeriesSum(paddingX,offsetX,getOffsetXSize()-1)-getPaddingLeft()-getPaddingRight();
106116
if(aspectRatio > 0){
107117
height = (int)(1f*childWidthMeasure/aspectRatio)+getPaddingTop()+getPaddingBottom();
108118
}
109119
childHeightMeasure = height;
110-
111120
setMeasuredDimension(width,height);
112121
}
113122

114123
@Override
115124
protected void onLayout(boolean changed, int l, int t, int r, int b) {
116-
addChildView();
125+
log("onLayout "+changed+",l="+l);
126+
int size = getOffsetXSize();
127+
for (int i = getChildCount(); i<size+2 && size>0; i++) {
128+
int cnt = adapter.getItemCount();
129+
int position = (current+size-i+cnt)%cnt;
130+
int viewType = adapter.getItemViewType(position);
131+
View child = adapter.onCreateView(this, viewType);
132+
addView(child);
133+
adapter.onBindView(child,position);
134+
adapter.onPageSelected(current);
135+
}
117136
layoutChildView();
118137
}
119138

120139
private void layoutChildView(){
121-
boolean isLeft = MODEL_LEFT == stackModel;
122140
int count = getChildCount();
123141
for(int i=0;i<count;i++){
124142
int left=getPaddingLeft(),right,top=getPaddingTop(),bottom;
125143
View child = getChildAt(i);
126-
127-
// int index = count-1-i;//2,1,0
128-
// int size = MODEL_LEFT == stackModel ? i:index;
129-
// int start = MODEL_LEFT == stackModel ? paddingX-(index)*offsetX:paddingX;
130-
131-
if(0 == i){
132-
int index = count - 3;
133-
left += edge + getSeriesSum(isLeft ? paddingX-index*offsetX:paddingX, offsetX, isLeft ? i:index);
134-
right = left + childWidthMeasure;
135-
top += getSeriesSum(paddingY, offsetY, index);
136-
bottom = childHeightMeasure - top;
137-
}else if(count-1 == i){
138-
left = -childWidthMeasure;
139-
right = 0;
140-
bottom = childHeightMeasure - top;
144+
if(i==count-1){
145+
boolean isLeft = MODEL_LEFT == stackModel;
146+
left += isLeft ? getMeasuredWidth():-childWidthMeasure;
141147
}else {
142-
int index = count - 2 - i;
143-
int size = isLeft ? i-1 : index;
144-
int start = isLeft ? paddingX - index* offsetX : paddingX;
145-
left += edge + getSeriesSum(start, offsetX, size);
146-
right = left + childWidthMeasure;
147-
top += getSeriesSum(paddingY, offsetY, index);
148-
bottom = childHeightMeasure - top;
148+
int index = Math.max(0, i - 1);
149+
left += edge + xSpace.get(index);
150+
top += ySpace.get(index);
149151
}
152+
bottom = childHeightMeasure - top;
153+
right = left + childWidthMeasure;
150154
setChildMeasureDimension(child,right-left,bottom-top);
151155
child.layout(left,top,right,bottom);
152156
}
153157
}
154158

155-
private void addChildView(){
156-
int size = getOffsetXSize();
157-
int cnt = adapter.getItemCount();
158-
for (int i = getChildCount(); i < size+2; i++) {
159-
int position = (current+size-i+cnt)%cnt;
160-
int viewType = adapter.getItemViewType(position);
161-
View child = adapter.onCreateView(this, viewType);
162-
addView(child);
163-
adapter.onBindView(child,position);
164-
adapter.onPageSelected(current);
165-
}
166-
}
167-
168-
private void addChildPoleView(){
169-
170-
}
171-
172159
private void setChildMeasureDimension(View child,int w,int h){
173160
child.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));
174161
}
@@ -185,8 +172,8 @@ private int getSeriesSum(int start,int ratio,int size){
185172
@Override
186173
public void computeScroll() {
187174
if(!mScroller.isFinished() && mScroller.computeScrollOffset()){
188-
int dy = mScroller.getCurrY();
189-
scrollTo(0,dy);
175+
int dx = mScroller.getCurrX();
176+
scrollDx(dx);
190177
ViewCompat.postInvalidateOnAnimation(this);
191178
}else{
192179
endScroll();
@@ -197,21 +184,25 @@ private void scrollDx(int dx){
197184
int count = getChildCount();
198185
int i = count-2;
199186
View child = getChildAt(i);
200-
int size = MODEL_LEFT == stackModel ? i-1:0;
201-
int start = paddingX;
202-
int left = edge+getSeriesSum(start,offsetX,size)+getPaddingLeft()+dx;
187+
188+
int left = getPaddingLeft()+edge+xSpace.get(i-1)+dx;
203189
int right = left + childWidthMeasure;
204190
int top = getPaddingTop();
205191
int bottom = childHeightMeasure-top;
206192
child.layout(left,top,right,bottom);
207193
}
208194

209195
private void scrollVelocity(int velocity){
196+
int count = getChildCount();
197+
int i = count-2;
210198

199+
View child = getChildAt(i);
200+
int dx = getPaddingLeft()+edge+xSpace.get(i-1)-child.getLeft();
201+
startScroll(-dx,dx);
211202
}
212203

213-
private void startScroll(){
214-
mScroller.startScroll(getScrollX(),getScrollY(),0,0,mDuration);
204+
private void startScroll(int begin,int dx){
205+
mScroller.startScroll(begin,0,dx,0,mDuration);
215206
ViewCompat.postInvalidateOnAnimation(this);
216207
}
217208

0 commit comments

Comments
 (0)