@@ -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