Skip to content

Commit b71b511

Browse files
author
luobl
committed
修复4.3以下版本不能滑动的问题
1 parent 3d79b77 commit b71b511

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
3d卡片效果
55
=======
66

7+
- 测试版本4.1.2,4.2.2,4.4.2可用
8+
79
##使用
810

911

@@ -22,6 +24,6 @@
2224
##问题
2325

2426
- `CardView`高度设置为`wrap_content`卡片会显示不全。
25-
- `CardView`设置`padding``margin`,旋转动画会有被截断的效果。最后一张卡片距离顶部的距离时在CardView内部指定的,为固定值。
26-
- ……
27+
- `CardView`设置`padding``margin`,旋转动画会有被截断的效果。最后一张卡片距离顶部的距离是在CardView内部指定的,为固定值。
28+
- 测试2.3.2有严重bug(估计4.0版本之前都有此bug),不可用。
2729

src/com/chiemy/cardview/TestFragment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public class TestFragment extends Fragment{
1919
private TextView tv;
2020
private View root;
21+
private View view;
2122

2223
@Override
2324
public View onCreateView(LayoutInflater inflater,
@@ -29,6 +30,7 @@ public View onCreateView(LayoutInflater inflater,
2930

3031
private void initUI(final View root) {
3132
root.setClickable(true);
33+
root.setOnClickListener(null);
3234
tv = (TextView) root.findViewById(R.id.textView);
3335
root.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
3436
@Override
@@ -41,17 +43,19 @@ public void onClick(View v) {
4143
public void onAnimationEnd(Animator animation) {
4244
root.clearAnimation();
4345
root.setVisibility(View.INVISIBLE);
46+
view.setClickable(true);
4447
}
4548
});
4649
}
4750
});
4851
}
4952

5053
public void show(final View view,Bundle bundle){
54+
this.view = view;
5155
String text = bundle.getString("text");
5256
tv.setText(text);
5357
view.setClickable(false);
54-
view.setRotationY(0);
58+
ViewHelper.setRotationY(view, 0);
5559
ViewHelper.setRotationY(root, -90);
5660
root.setVisibility(View.VISIBLE);
5761

@@ -65,7 +69,6 @@ public void show(final View view,Bundle bundle){
6569
@Override
6670
public void onAnimationEnd(Animator animation) {
6771
ViewHelper.setRotationY(view, 0);
68-
view.setClickable(true);
6972
}
7073
});
7174
}

src/com/chiemy/cardview/Utils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.chiemy.cardview;
22

33
import android.content.Context;
4+
import android.graphics.Rect;
45
import android.util.DisplayMetrics;
6+
import android.view.View;
57

68
public class Utils {
79

@@ -36,5 +38,5 @@ public static float convertDpToPixel(Context context, float dp) {
3638
float px = (float) (dp * (metrics.densityDpi / 160f));
3739
return px;
3840
}
39-
41+
4042
}

src/com/chiemy/cardview/view/CardView.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.widget.FrameLayout;
1414
import android.widget.ListAdapter;
1515

16+
import com.chiemy.cardview.Utils;
1617
import com.nineoldandroids.animation.Animator;
1718
import com.nineoldandroids.animation.AnimatorListenerAdapter;
1819
import com.nineoldandroids.view.ViewHelper;
@@ -25,7 +26,7 @@
2526
public class CardView extends FrameLayout{
2627
private static final int ITEM_SPACE = 40;
2728
private static final int DEF_MAX_VISIBLE = 4;
28-
private static final int PADDING_TOP = 300;
29+
private static final int PADDING_TOP = 20;
2930

3031
private int mMaxVisible = DEF_MAX_VISIBLE;
3132
private int itemSpace = ITEM_SPACE;
@@ -58,6 +59,7 @@ public CardView(Context context) {
5859
}
5960

6061
private void init() {
62+
topRect = new Rect();
6163
ViewConfiguration con = ViewConfiguration.get(getContext());
6264
mTouchSlop = con.getScaledTouchSlop();
6365
}
@@ -93,7 +95,6 @@ public void setAdapter(ListAdapter adapter) {
9395
ensureFull();
9496
}
9597

96-
9798
public void setOnCardClickListener(OnCardClickListener listener) {
9899
mListener = listener;
99100
}
@@ -108,18 +109,17 @@ && getChildCount() < mMaxVisible) {
108109
viewHolder.put(index, view);
109110

110111
//添加剩余的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);
116113
ViewHelper.setScaleX(view, ((mMaxVisible - index - 1)/(float)mMaxVisible)*0.2f + 0.8f);
117-
118114
int topMargin = (mMaxVisible - index - 1)*itemSpace + PADDING_TOP;
119115
ViewHelper.setTranslationY(view, topMargin);
120116
ViewHelper.setAlpha(view, mNextAdapterPosition == 0 ? 1 : 0.5f);
121117

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);
123123

124124
mNextAdapterPosition += 1;
125125
}
@@ -144,6 +144,7 @@ public boolean onTouchEvent(MotionEvent event) {
144144
case MotionEvent.ACTION_MOVE:
145145
if(!remove){
146146
if(goDown()){
147+
downY = -1;
147148
remove = true;
148149
}
149150
}
@@ -157,10 +158,8 @@ public boolean onTouchEvent(MotionEvent event) {
157158
*/
158159
private boolean goDown() {
159160
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);
164163
//如果按下的位置不在顶部视图上,则不移动
165164
if(!topRect.contains((int)downX, (int)downY)){
166165
return false;
@@ -237,6 +236,14 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
237236
return super.onInterceptTouchEvent(ev);
238237
}
239238

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+
240247
private final DataSetObserver mDataSetObserver = new DataSetObserver() {
241248
@Override
242249
public void onChanged() {
@@ -253,7 +260,6 @@ public void onInvalidated() {
253260
@Override
254261
public void onClick(View v) {
255262
if(mListener != null){
256-
System.out.println(">>cardClick:" + topPosition);
257263
mListener.onCardClick(v, topPosition);
258264
}
259265
}

0 commit comments

Comments
 (0)