13
13
import android .widget .FrameLayout ;
14
14
import android .widget .ListAdapter ;
15
15
16
+ import com .chiemy .cardview .Utils ;
16
17
import com .nineoldandroids .animation .Animator ;
17
18
import com .nineoldandroids .animation .AnimatorListenerAdapter ;
18
19
import com .nineoldandroids .view .ViewHelper ;
25
26
public class CardView extends FrameLayout {
26
27
private static final int ITEM_SPACE = 40 ;
27
28
private static final int DEF_MAX_VISIBLE = 4 ;
28
- private static final int PADDING_TOP = 300 ;
29
+ private static final int PADDING_TOP = 20 ;
29
30
30
31
private int mMaxVisible = DEF_MAX_VISIBLE ;
31
32
private int itemSpace = ITEM_SPACE ;
@@ -58,6 +59,7 @@ public CardView(Context context) {
58
59
}
59
60
60
61
private void init () {
62
+ topRect = new Rect ();
61
63
ViewConfiguration con = ViewConfiguration .get (getContext ());
62
64
mTouchSlop = con .getScaledTouchSlop ();
63
65
}
@@ -93,7 +95,6 @@ public void setAdapter(ListAdapter adapter) {
93
95
ensureFull ();
94
96
}
95
97
96
-
97
98
public void setOnCardClickListener (OnCardClickListener listener ) {
98
99
mListener = listener ;
99
100
}
@@ -108,18 +109,17 @@ && getChildCount() < mMaxVisible) {
108
109
viewHolder .put (index , view );
109
110
110
111
//添加剩余的View时,始终处在最后
111
- if (mNextAdapterPosition >= mMaxVisible ){
112
- index = mMaxVisible - 1 ;
113
- }
114
- LayoutParams params = new LayoutParams (LayoutParams .MATCH_PARENT ,LayoutParams .WRAP_CONTENT );
115
- view .setLayoutParams (params );
112
+ index = Math .min (mNextAdapterPosition , mMaxVisible - 1 );
116
113
ViewHelper .setScaleX (view , ((mMaxVisible - index - 1 )/(float )mMaxVisible )*0.2f + 0.8f );
117
-
118
114
int topMargin = (mMaxVisible - index - 1 )*itemSpace + PADDING_TOP ;
119
115
ViewHelper .setTranslationY (view , topMargin );
120
116
ViewHelper .setAlpha (view , mNextAdapterPosition == 0 ? 1 : 0.5f );
121
117
122
- addViewInLayout (view ,0 , params );
118
+ LayoutParams params = (LayoutParams ) view .getLayoutParams ();
119
+ if (params == null ){
120
+ params = new LayoutParams (LayoutParams .MATCH_PARENT , LayoutParams .WRAP_CONTENT );
121
+ }
122
+ addViewInLayout (view ,0 , params , true );
123
123
124
124
mNextAdapterPosition += 1 ;
125
125
}
@@ -144,6 +144,7 @@ public boolean onTouchEvent(MotionEvent event) {
144
144
case MotionEvent .ACTION_MOVE :
145
145
if (!remove ){
146
146
if (goDown ()){
147
+ downY = -1 ;
147
148
remove = true ;
148
149
}
149
150
}
@@ -157,10 +158,8 @@ public boolean onTouchEvent(MotionEvent event) {
157
158
*/
158
159
private boolean goDown () {
159
160
final View topView = getChildAt (getChildCount () - 1 );
160
- if (topRect == null ){
161
- topRect = new Rect ();
162
- topView .getHitRect (topRect );
163
- }
161
+ //topView.getHitRect(topRect); 在4.3以前有bug,用以下方法代替
162
+ topRect = getHitRect (topRect , topView );
164
163
//如果按下的位置不在顶部视图上,则不移动
165
164
if (!topRect .contains ((int )downX , (int )downY )){
166
165
return false ;
@@ -237,6 +236,14 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
237
236
return super .onInterceptTouchEvent (ev );
238
237
}
239
238
239
+ public static Rect getHitRect (Rect rect ,View child ){
240
+ rect .left = child .getLeft ();
241
+ rect .right = child .getRight ();
242
+ rect .top = (int ) (child .getTop () + ViewHelper .getTranslationY (child ));
243
+ rect .bottom = (int ) (child .getBottom () + ViewHelper .getTranslationY (child ));
244
+ return rect ;
245
+ }
246
+
240
247
private final DataSetObserver mDataSetObserver = new DataSetObserver () {
241
248
@ Override
242
249
public void onChanged () {
@@ -253,7 +260,6 @@ public void onInvalidated() {
253
260
@ Override
254
261
public void onClick (View v ) {
255
262
if (mListener != null ){
256
- System .out .println (">>cardClick:" + topPosition );
257
263
mListener .onCardClick (v , topPosition );
258
264
}
259
265
}
0 commit comments