Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsjlzx committed Dec 1, 2016
1 parent f29f782 commit 502fcc8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void setLoadMoreEnabled(boolean enabled) {
mLoadMoreEnabled = enabled;
if (!enabled) {
if (mFootView instanceof LoadingFooter) {
mWrapAdapter.removeFooterView(mFootView);
mWrapAdapter.removeFooterView();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public void addFooterView(View view) {
if (view == null) {
throw new RuntimeException("footer is null");
}
if (getFooterViewsCount() > 0) {
removeFooterView(getFooterView());
}

removeFooterView();
mFooterViews.add(view);
}

Expand Down Expand Up @@ -113,14 +112,22 @@ public ArrayList<View> getHeaderViews() {
return mHeaderViews;
}

public void removeHeaderView(View view) {
mHeaderViews.remove(view);
this.notifyDataSetChanged();
public void removeHeaderView() {
if (getHeaderViewsCount() > 0) {
View headerView = getHeaderView();
mHeaderViews.remove(headerView);
this.notifyDataSetChanged();
}

}

public void removeFooterView(View view) {
mFooterViews.remove(view);
this.notifyDataSetChanged();
public void removeFooterView() {
if (getFooterViewsCount() > 0) {
View footerView = getFooterView();
mFooterViews.remove(footerView);
this.notifyDataSetChanged();
}

}

public int getHeaderViewsCount() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,98 +1,17 @@
package com.github.jdsjlzx.util;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

/**
* RecyclerView设置Header/Footer所用到的工具类
* @author Lzx
* @created 2016/9/30 10:36
* @deprecated
*
*/
public class RecyclerViewUtils {

/**
* 设置HeaderView
*
* @param recyclerView
* @param view
*/
@Deprecated
public static void setHeaderView(RecyclerView recyclerView, View view) {
RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

if (outerAdapter == null || !(outerAdapter instanceof LRecyclerViewAdapter)) {
return;
}

LRecyclerViewAdapter headerAndFooterAdapter = (LRecyclerViewAdapter) outerAdapter;
/*if (headerAndFooterAdapter.getHeaderViewsCount() == 0) {
headerAndFooterAdapter.addHeaderView(view);
}*/
headerAndFooterAdapter.addHeaderView(view);
}

/**
* 设置FooterView
*
* @param recyclerView
* @param view
*/
@Deprecated
public static void setFooterView(RecyclerView recyclerView, View view) {
RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

if (outerAdapter == null || !(outerAdapter instanceof LRecyclerViewAdapter)) {
return;
}

LRecyclerViewAdapter lRecyclerViewAdapter = (LRecyclerViewAdapter) outerAdapter;
if (lRecyclerViewAdapter.getFooterViewsCount() > 0) {
lRecyclerViewAdapter.removeFooterView(lRecyclerViewAdapter.getFooterView());
}
lRecyclerViewAdapter.addFooterView(view);
}

/**
* 移除FooterView
*
* @param recyclerView
*/
public static void removeFooterView(RecyclerView recyclerView) {

RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

if (outerAdapter != null && outerAdapter instanceof LRecyclerViewAdapter) {

int footerViewCounter = ((LRecyclerViewAdapter) outerAdapter).getFooterViewsCount();
if (footerViewCounter > 0) {
View footerView = ((LRecyclerViewAdapter) outerAdapter).getFooterView();
((LRecyclerViewAdapter) outerAdapter).removeFooterView(footerView);
}
}
}

/**
* 移除HeaderView
*
* @param recyclerView
*/
public static void removeHeaderView(RecyclerView recyclerView) {

RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();

if (outerAdapter != null && outerAdapter instanceof LRecyclerViewAdapter) {

int headerViewCounter = ((LRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
if (headerViewCounter > 0) {
View headerView = ((LRecyclerViewAdapter) outerAdapter).getHeaderView();
((LRecyclerViewAdapter) outerAdapter).removeHeaderView(headerView);
}
}
}

/**
* 请使用本方法替代RecyclerView.ViewHolder的getLayoutPosition()方法
*
Expand All @@ -113,23 +32,4 @@ public static int getLayoutPosition(RecyclerView recyclerView, RecyclerView.View
return holder.getLayoutPosition();
}

/**
* 请使用本方法替代RecyclerView.ViewHolder的getAdapterPosition()方法
*
* @param recyclerView
* @param holder
* @return
*/
public static int getAdapterPosition(RecyclerView recyclerView, RecyclerView.ViewHolder holder) {
RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
if (outerAdapter != null && outerAdapter instanceof LRecyclerViewAdapter) {

int headerViewCounter = ((LRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
if (headerViewCounter > 0) {
return holder.getAdapterPosition() - (headerViewCounter + 1);
}
}

return holder.getAdapterPosition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.lzx.demo.bean.ItemModel;
import com.lzx.demo.util.AppToast;
import com.lzx.demo.util.NetworkUtils;
import com.lzx.demo.view.SampleHeader;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,11 +78,14 @@ public void onCreate(Bundle savedInstanceState) {
mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);

mLRecyclerViewAdapter.addHeaderView(new SampleHeader(this));
//add a HeaderView
final View header = LayoutInflater.from(this).inflate(R.layout.sample_header,(ViewGroup)findViewById(android.R.id.content), false);
mLRecyclerViewAdapter.addHeaderView(header);

mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {

RecyclerViewStateUtils.setFooterViewState(mRecyclerView,LoadingFooter.State.Normal);
mDataAdapter.clear();
mLRecyclerViewAdapter.notifyDataSetChanged();//fix bug:crapped or attached views may not be recycled. isScrap:false isAttached:true
Expand Down
81 changes: 63 additions & 18 deletions app/src/main/java/com/lzx/demo/view/SwipeMenuView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lzx.demo.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
Expand All @@ -9,7 +11,7 @@
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.OvershootInterpolator;

public class SwipeMenuView extends ViewGroup {
Expand All @@ -29,6 +31,7 @@ public class SwipeMenuView extends ViewGroup {
* 滑动判定临界值(右侧菜单宽度的40%) 手指抬起时,超过了展开,没超过收起menu
*/
private int mLimit;
private View mContentView;
//private Scroller mScroller;//以前item的滑动动画靠它做,现在用属性动画做
//上一次的xy
private PointF mLastP = new PointF();
Expand All @@ -49,6 +52,8 @@ public class SwipeMenuView extends ViewGroup {
//20160929add 左滑右滑的开关
private boolean isLeftSwipe = false;

private boolean isExpand;//代表当前是否是展开状态

public SwipeMenuView(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -113,9 +118,12 @@ private void init(Context context) {

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Log.d(TAG, "onMeasure() called with: " + "widthMeasureSpec = [" + widthMeasureSpec + "], heightMeasureSpec = [" + heightMeasureSpec + "]");
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

setClickable(true);//令自己可点击,从而获取触摸事件

mRightMenuWidths = 0;//由于ViewHolder的复用机制,每次这里要手动恢复初始值
int contentWidth = 0;//适配GridLayoutManager,将以第一个子Item(即ContentItem)的宽度为控件宽度
int childCount = getChildCount();

//add by 2016 08 11 为了子View的高,可以matchParent(参考的FrameLayout 和LinearLayout的Horizontal)
Expand All @@ -125,19 +133,24 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != GONE) {
//measureChild(childView, widthMeasureSpec, heightMeasureSpec);
measureChildWithMargins(childView, widthMeasureSpec, 0, heightMeasureSpec, 0);
//后续计划加入上滑、下滑,则将不再支持Item的margin
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
//measureChildWithMargins(childView, widthMeasureSpec, 0, heightMeasureSpec, 0);
final MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
mHeight = Math.max(mHeight, childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
mHeight = Math.max(mHeight, childView.getMeasuredHeight());
if (measureMatchParentChildren && lp.height == LayoutParams.MATCH_PARENT) {
isNeedMeasureChildHeight = true;
}
if (i > 0) {//第一个布局是Left item,从第二个开始才是RightMenu
mRightMenuWidths += childView.getMeasuredWidth();
} else {
mContentView = childView;
contentWidth = childView.getMeasuredWidth();
}
}
}
setMeasuredDimension(mScreenW, mHeight);//宽度取屏幕宽度
setMeasuredDimension(getPaddingLeft() + getPaddingRight() + contentWidth,
mHeight + getPaddingTop() + getPaddingBottom());//宽度取第一个Item(Content)的宽度
mLimit = mRightMenuWidths * 4 / 10;//滑动判断的临界值
//Log.d(TAG, "onMeasure() called with: " + "mRightMenuWidths = [" + mRightMenuWidths);
if (isNeedMeasureChildHeight) {//如果子View的height有MatchParent属性的,设置子View高度
Expand Down Expand Up @@ -182,16 +195,15 @@ private void forceUniformHeight(int count, int widthMeasureSpec) {

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//LogUtils.d(TAG, "onLayout() called with: " + "changed = [" + changed + "], l = [" + l + "], t = [" + t + "], r = [" + r + "], b = [" + b + "]");
int childCount = getChildCount();
int left = l;
int right = 0;
int left = 0 + getPaddingLeft();
int right = 0 + getPaddingLeft();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != GONE) {
if (i == 0) {//第一个子View是内容 宽度设置为全屏
childView.layout(left, getPaddingTop(), left + mScreenW, getPaddingTop() + childView.getMeasuredHeight());
left = left + mScreenW;
childView.layout(left, getPaddingTop(), left + childView.getMeasuredWidth(), getPaddingTop() + childView.getMeasuredHeight());
left = left + childView.getMeasuredWidth();
} else {
if (isLeftSwipe) {
childView.layout(left, getPaddingTop(), left + childView.getMeasuredWidth(), getPaddingTop() + childView.getMeasuredHeight());
Expand All @@ -204,7 +216,6 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
}
//Log.d(TAG, "onLayout() called with: " + "maxScrollGap = [" + maxScrollGap + "], l = [" + l + "], t = [" + t + "], r = [" + r + "], b = [" + b + "]");
}

@Override
Expand Down Expand Up @@ -335,6 +346,11 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getX() < getWidth() - getScrollX()) {
return true;//true表示拦截
}
}else {
float gap = mLastP.x - ev.getRawX();
if(gap<0 && getScaleX() == 1.0) {
return true;
}
}
} else {
if (-getScrollX() > mScaleTouchSlop) {
Expand All @@ -361,8 +377,17 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
private ValueAnimator mExpandAnim, mCloseAnim;

public void smoothExpand() {
/*mScroller.startScroll(getScrollX(), 0, mRightMenuWidths - getScrollX(), 0);
invalidate();*/
//展开就加入ViewCache:
mViewCache = SwipeMenuView.this;

//2016 11 13 add 侧滑菜单展开,屏蔽content长按
if (null != mContentView) {
mContentView.setLongClickable(false);
}

if (mCloseAnim != null && mCloseAnim.isRunning()) {
mCloseAnim.cancel();
}
mExpandAnim = ValueAnimator.ofInt(getScrollX(), isLeftSwipe ? mRightMenuWidths : -mRightMenuWidths);
mExpandAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
Expand All @@ -371,25 +396,45 @@ public void onAnimationUpdate(ValueAnimator animation) {
}
});
mExpandAnim.setInterpolator(new OvershootInterpolator());
mExpandAnim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
isExpand = true;
}
});
mExpandAnim.setDuration(300).start();
}

/**
* 平滑关闭
*/
public void smoothClose() {
/* mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0);
invalidate();*/
mViewCache = null;

//2016 11 13 add 侧滑菜单展开,屏蔽content长按
if (null != mContentView) {
mContentView.setLongClickable(true);
}

if (mExpandAnim != null && mExpandAnim.isRunning()) {
mExpandAnim.cancel();
}
mCloseAnim = ValueAnimator.ofInt(getScrollX(), 0);
mCloseAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
scrollTo((Integer) animation.getAnimatedValue(), 0);
}
});
mCloseAnim.setInterpolator(new AnticipateInterpolator());
mCloseAnim.setInterpolator(new AccelerateInterpolator());
mCloseAnim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
isExpand = false;

}
});
mCloseAnim.setDuration(300).start();
//LogUtils.d(TAG, "smoothClose() called with:getScrollX() " + getScrollX());
}


Expand Down

0 comments on commit 502fcc8

Please sign in to comment.