Skip to content

Commit

Permalink
解决了卡片滑动过程中小概率会显示上一张卡片的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
huxq17 committed May 27, 2016
1 parent 192e1bd commit b5296c2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
8 changes: 0 additions & 8 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.huxq17.example.R;
import com.huxq17.example.bean.ContentBean;
import com.huxq17.swipecardsview.BaseCardAdapter;
import com.huxq17.swipecardsview.LogUtil;
import com.squareup.picasso.Picasso;

import java.util.List;
Expand Down Expand Up @@ -44,7 +43,6 @@ public void onBindData(int position, View cardview) {
if (datas == null || datas.size() == 0) {
return;
}
LogUtil.d("onBindData position="+position);
ImageView imageView = (ImageView) cardview.findViewById(R.id.iv_meizi);
ContentBean meizi = datas.get(position);
String url = meizi.getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.View;
import android.view.ViewGroup;

import com.andbase.tractor.utils.LogUtils;
import com.huxq17.example.MainActivity;
import com.huxq17.example.R;
import com.huxq17.example.adapter.MeiziAdapter;
Expand Down Expand Up @@ -80,7 +81,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onShow(int index) {
curIndex = index;
// LogUtils.i("test showing index = " + index);
LogUtils.i("test showing index = " + index);
}

@Override
Expand Down
33 changes: 22 additions & 11 deletions library/src/main/java/com/huxq17/swipecardsview/SwipeCardsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class SwipeCardsView extends LinearLayout {
private boolean isTouching = false;
private int tempShowingIndex = -1;
private int cardVisibleCount = 3;
/**
* 卡片是否在移动的标记,如果在移动中则不执行onLayout中的layout操作
*/
private boolean mScrolling = false;

public SwipeCardsView(Context context) {
this(context, null);
Expand Down Expand Up @@ -132,10 +136,10 @@ private void bindCardData(int position, View cardview) {
*/
public void notifyDatasetChanged(int index) {
if (canResetView()) {
LogUtil.d("test notifyDatasetChanged canResetView="+index);
LogUtil.d("test notifyDatasetChanged canResetView=" + index);
refreshUI(index);
} else {
LogUtil.d("test notifyDatasetChanged can not Reset View="+index);
LogUtil.d("test notifyDatasetChanged can not Reset View=" + index);
mWaitRefresh = true;
tempShowingIndex = index;
}
Expand Down Expand Up @@ -282,11 +286,13 @@ public boolean dispatchTouchEventSupper(MotionEvent e) {
}

private void releaseTopView(float xvel, float yvel) {
mScrolling = true;
View topView = getTopView();
LogUtil.i("test start releaseTopView topView="+topView);
LogUtil.i("test start releaseTopView topView=" + topView);
if (topView != null) {
onTopViewReleased(topView, xvel, yvel);
} LogUtil.i("test end releaseTopView topView="+topView);
}
LogUtil.i("test end releaseTopView topView=" + topView);

}

Expand Down Expand Up @@ -332,6 +338,7 @@ private View getTopView() {
public void startScrollTopView(int finalLeft, int finalTop, int duration, SlideType flyType) {
View topView = getTopView();
if (topView == null) {
mScrolling = false;
return;
}
if (finalLeft != initLeft) {
Expand All @@ -344,6 +351,8 @@ public void startScrollTopView(int finalLeft, int finalTop, int duration, SlideT
if (dx != 0 || dy != 0) {
mScroller.startScroll(topView.getLeft(), topView.getTop(), dx, dy, duration);
ViewCompat.postInvalidateOnAnimation(this);
} else {
mScrolling = false;
}
if (flyType != SlideType.NONE && mCardsSlideListener != null) {
mCardsSlideListener.onCardVanish(showingIndex, flyType);
Expand Down Expand Up @@ -405,7 +414,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (hasTouchTopView||mScroller.computeScrollOffset()) {
if (hasTouchTopView || mScrolling) {
return;
}
int size = viewList.size();
Expand All @@ -421,7 +430,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
initTop = viewList.get(0).getTop();
mCardWidth = viewList.get(0).getMeasuredWidth();
View topView = getTopView();
LogUtil.i("test onLayout initLeft="+initLeft+"; topView="+topView);
LogUtil.i("test onLayout initLeft=" + initLeft + "; topView=" + topView);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Expand Down Expand Up @@ -479,7 +488,6 @@ private void layoutChild(View child, int index) {

@Override
public void computeScroll() {
LogUtil.d("test computeScroll mScroller.computeScrollOffset()="+mScroller.computeScrollOffset());
if (mScroller.computeScrollOffset()) {
View topView = getTopView();
if (topView == null) {
Expand All @@ -494,6 +502,7 @@ public void computeScroll() {
}
ViewCompat.postInvalidateOnAnimation(this);
} else {
mScrolling = false;
onAnimalStop();
}
}
Expand All @@ -513,6 +522,7 @@ private boolean canResetView() {
*/
private void resetViewGroup() {
if (releasedViewList.size() == 0) {
mScrolling = false;
if (mWaitRefresh) {
mWaitRefresh = false;
refreshUI(tempShowingIndex);
Expand All @@ -529,24 +539,25 @@ private void resetViewGroup() {
} else {
View changedView = releasedViewList.get(0);
if (changedView.getLeft() == initLeft) {
LogUtil.i("test changedView="+changedView+";changedView.getLeft() == initLeft left="+initLeft+";scalex="+changedView.getScaleX());
releasedViewList.remove(0);
mScrolling = false;
return;
}
viewList.remove(changedView);
viewList.add(changedView);
mScrolling = false;
int viewSize = viewList.size();
removeViewInLayout(changedView);
addViewInLayout(changedView, 0, changedView.getLayoutParams(), true);
requestLayout();
// removeView(changedView);
// addView(changedView,0);
viewList.remove(changedView);
viewList.add(changedView);
if (mWaitRefresh) {
mWaitRefresh = false;
int index = ++tempShowingIndex;
refreshUI(index);
} else {
int newIndex = showingIndex + viewSize ;
int newIndex = showingIndex + viewSize;
if (newIndex < mCount) {
bindCardData(newIndex, changedView);
} else {
Expand Down

0 comments on commit b5296c2

Please sign in to comment.