Skip to content

Commit 021d629

Browse files
author
zhangxutong
committed
2016 11 09 add,适配GridLayoutManager,将以第一个子Item(即ContentItem)的宽度为控件宽度。
1 parent 95beb8d commit 021d629

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Step 2. Add the dependency
3636

3737

3838
###更新点###
39+
2016 11 09 更新:
40+
1 适配GridLayoutManager,将以第一个子Item(即ContentItem)的宽度为控件宽度。
41+
2 使用时,如果需要撑满布局,切记第一个子Item(Content),宽度要是match_parent.
42+
3943
2016 11 04 更新:
4044
1 优化了长按事件和侧滑事件的关系,尽量的参考QQ。
4145

app/src/main/java/mcxtzhang/swipedelmenu/FullDemo/FullDelDemoActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.os.Bundle;
5+
import android.support.v7.widget.GridLayoutManager;
56
import android.support.v7.widget.LinearLayoutManager;
67
import android.support.v7.widget.RecyclerView;
78
import android.view.MotionEvent;
@@ -63,7 +64,7 @@ public void onTop(int pos) {
6364
}
6465
});
6566
mRv.setAdapter(mAdapter);
66-
mRv.setLayoutManager(mLayoutManager = new LinearLayoutManager(this));
67+
mRv.setLayoutManager(mLayoutManager = new GridLayoutManager(this,2));
6768

6869
//6 2016 10 21 add , 增加viewChache 的 get()方法,
6970
// 可以用在:当点击外部空白处时,关闭正在展开的侧滑菜单。我个人觉得意义不大,

app/src/main/res/layout/item_cst_swipe.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<TextView
1010
android:id="@+id/content"
11-
android:layout_width="wrap_content"
11+
android:layout_width="match_parent"
1212
android:layout_height="match_parent"
1313
android:background="?android:attr/selectableItemBackground"
1414
android:gravity="center"

swipemenulib/src/main/java/com/mcxtzhang/swipemenulib/SwipeMenuLayout.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import android.view.View;
1212
import android.view.ViewConfiguration;
1313
import android.view.ViewGroup;
14-
import android.view.ViewParent;
1514
import android.view.animation.AnticipateInterpolator;
1615
import android.view.animation.OvershootInterpolator;
1716

@@ -38,6 +37,7 @@
3837
* 2016 10 22 add , 仿QQ,侧滑菜单展开时,点击除侧滑菜单之外的区域,关闭侧滑菜单。
3938
* 8 2016 11 03 add,判断手指起始落点,如果距离属于滑动了,就屏蔽一切点击事件。
4039
* 9 2016 11 04 fix 长按事件和侧滑的冲突。
40+
* 10 2016 11 09 add,适配GridLayoutManager,将以第一个子Item(即ContentItem)的宽度为控件宽度。
4141
* Created by zhangxutong .
4242
* Date: 16/04/24
4343
*/
@@ -49,7 +49,6 @@ public class SwipeMenuLayout extends ViewGroup {
4949
private int mMaxVelocity;//计算滑动速度用
5050
private int mPointerId;//多点触摸只算第一根手指的速度
5151
private int mHeight;//自己的高度
52-
private int mMaxWidth;//父控件留给自己的最大的水平空间
5352
/**
5453
* 右侧菜单宽度总和(最大滑动距离)
5554
*/
@@ -163,16 +162,9 @@ private void init(Context context) {
163162
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
164163
//Log.d(TAG, "onMeasure() called with: " + "widthMeasureSpec = [" + widthMeasureSpec + "], heightMeasureSpec = [" + heightMeasureSpec + "]");
165164
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-
}
174165

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

178170
//add by 2016 08 11 为了子View的高,可以matchParent(参考的FrameLayout 和LinearLayout的Horizontal)
@@ -191,10 +183,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
191183
}
192184
if (i > 0) {//第一个布局是Left item,从第二个开始才是RightMenu
193185
mRightMenuWidths += childView.getMeasuredWidth();
186+
} else {
187+
contentWidth = childView.getMeasuredWidth();
194188
}
195189
}
196190
}
197-
setMeasuredDimension(mMaxWidth, mHeight);//宽度取最大宽度
191+
setMeasuredDimension(contentWidth, mHeight);//宽度取第一个Item(Content)的宽度
198192
mLimit = mRightMenuWidths * 4 / 10;//滑动判断的临界值
199193
//Log.d(TAG, "onMeasure() called with: " + "mRightMenuWidths = [" + mRightMenuWidths);
200194
if (isNeedMeasureChildHeight) {//如果子View的height有MatchParent属性的,设置子View高度
@@ -247,8 +241,8 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
247241
View childView = getChildAt(i);
248242
if (childView.getVisibility() != GONE) {
249243
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();
252246
} else {
253247
if (isLeftSwipe) {
254248
childView.layout(left, getPaddingTop(), left + childView.getMeasuredWidth(), getPaddingTop() + childView.getMeasuredHeight());

0 commit comments

Comments
 (0)