11
11
import android .view .View ;
12
12
import android .view .ViewConfiguration ;
13
13
import android .view .ViewGroup ;
14
- import android .view .ViewParent ;
15
14
import android .view .animation .AnticipateInterpolator ;
16
15
import android .view .animation .OvershootInterpolator ;
17
16
38
37
* 2016 10 22 add , 仿QQ,侧滑菜单展开时,点击除侧滑菜单之外的区域,关闭侧滑菜单。
39
38
* 8 2016 11 03 add,判断手指起始落点,如果距离属于滑动了,就屏蔽一切点击事件。
40
39
* 9 2016 11 04 fix 长按事件和侧滑的冲突。
40
+ * 10 2016 11 09 add,适配GridLayoutManager,将以第一个子Item(即ContentItem)的宽度为控件宽度。
41
41
* Created by zhangxutong .
42
42
* Date: 16/04/24
43
43
*/
@@ -49,7 +49,6 @@ public class SwipeMenuLayout extends ViewGroup {
49
49
private int mMaxVelocity ;//计算滑动速度用
50
50
private int mPointerId ;//多点触摸只算第一根手指的速度
51
51
private int mHeight ;//自己的高度
52
- private int mMaxWidth ;//父控件留给自己的最大的水平空间
53
52
/**
54
53
* 右侧菜单宽度总和(最大滑动距离)
55
54
*/
@@ -163,16 +162,9 @@ private void init(Context context) {
163
162
protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
164
163
//Log.d(TAG, "onMeasure() called with: " + "widthMeasureSpec = [" + widthMeasureSpec + "], heightMeasureSpec = [" + heightMeasureSpec + "]");
165
164
super .onMeasure (widthMeasureSpec , heightMeasureSpec );
166
- //add by zhangxutong 2016 10 22 for 最大宽度根据父控件计算出,如果没有父控件用屏幕宽度
167
- ViewParent parent = getParent ();
168
- if (parent != null && parent instanceof ViewGroup ) {
169
- ViewGroup viewGroup = (ViewGroup ) parent ;
170
- mMaxWidth = viewGroup .getMeasuredWidth () - viewGroup .getPaddingLeft () - viewGroup .getPaddingRight ();
171
- } else {
172
- mMaxWidth = getResources ().getDisplayMetrics ().widthPixels ;
173
- }
174
165
175
166
mRightMenuWidths = 0 ;//由于ViewHolder的复用机制,每次这里要手动恢复初始值
167
+ int contentWidth = 0 ;//2016 11 09 add,适配GridLayoutManager,将以第一个子Item(即ContentItem)的宽度为控件宽度
176
168
int childCount = getChildCount ();
177
169
178
170
//add by 2016 08 11 为了子View的高,可以matchParent(参考的FrameLayout 和LinearLayout的Horizontal)
@@ -191,10 +183,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
191
183
}
192
184
if (i > 0 ) {//第一个布局是Left item,从第二个开始才是RightMenu
193
185
mRightMenuWidths += childView .getMeasuredWidth ();
186
+ } else {
187
+ contentWidth = childView .getMeasuredWidth ();
194
188
}
195
189
}
196
190
}
197
- setMeasuredDimension (mMaxWidth , mHeight );//宽度取最大宽度
191
+ setMeasuredDimension (contentWidth , mHeight );//宽度取第一个Item(Content)的宽度
198
192
mLimit = mRightMenuWidths * 4 / 10 ;//滑动判断的临界值
199
193
//Log.d(TAG, "onMeasure() called with: " + "mRightMenuWidths = [" + mRightMenuWidths);
200
194
if (isNeedMeasureChildHeight ) {//如果子View的height有MatchParent属性的,设置子View高度
@@ -247,8 +241,8 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
247
241
View childView = getChildAt (i );
248
242
if (childView .getVisibility () != GONE ) {
249
243
if (i == 0 ) {//第一个子View是内容 宽度设置为全屏
250
- childView .layout (left , getPaddingTop (), left + mMaxWidth , getPaddingTop () + childView .getMeasuredHeight ());
251
- left = left + mMaxWidth ;
244
+ childView .layout (left , getPaddingTop (), left + childView . getMeasuredWidth () , getPaddingTop () + childView .getMeasuredHeight ());
245
+ left = left + childView . getMeasuredWidth () ;
252
246
} else {
253
247
if (isLeftSwipe ) {
254
248
childView .layout (left , getPaddingTop (), left + childView .getMeasuredWidth (), getPaddingTop () + childView .getMeasuredHeight ());
0 commit comments