Skip to content

Commit 9e965d7

Browse files
committed
a simpler and better way to mange open items
1 parent bbb5c08 commit 9e965d7

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Diff for: library/src/main/java/com/daimajia/swipe/SwipeAdapter.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public static enum Mode {
1414
};
1515

1616
private Mode mode = Mode.Single;
17-
1817
public final int INVALID_POSITION = -1;
18+
1919
private Set<Integer> mOpenPositions = new HashSet<Integer>();
2020
private int mOpenPosition = INVALID_POSITION;
21-
private SwipeLayout mPrevious;
21+
private Set<SwipeLayout> mShownLayouts = new HashSet<SwipeLayout>();
2222

2323
/**
2424
* return the {@link com.daimajia.swipe.SwipeLayout} resource id, int the view item.
@@ -60,6 +60,7 @@ public final View getView(int position, View convertView, ViewGroup parent) {
6060
swipeLayout.addSwipeListener(swipeMemory);
6161
swipeLayout.addOnLayoutListener(onLayoutListener);
6262
swipeLayout.setTag(swipeResourceId, new ValueBox(position, swipeMemory, onLayoutListener));
63+
mShownLayouts.add(swipeLayout);
6364
}
6465
}else{
6566
swipeLayout = (SwipeLayout)v.findViewById(swipeResourceId);
@@ -121,6 +122,18 @@ public void closeItem(int position) {
121122
notifyDataSetChanged();
122123
}
123124

125+
126+
public void closeAllItems(){
127+
closeAllExcept(null);
128+
}
129+
130+
public void closeAllExcept(SwipeLayout layout){
131+
for(SwipeLayout s : mShownLayouts){
132+
if(s != layout)
133+
s.close();
134+
}
135+
}
136+
124137
class ValueBox {
125138
OnLayoutListener onLayoutListener;
126139
SwipeMemory swipeMemory;
@@ -174,32 +187,26 @@ class SwipeMemory extends SimpleSwipeListener{
174187

175188
@Override
176189
public void onClose(SwipeLayout layout) {
177-
if(mode == Mode.Multiple)
190+
if(mode == Mode.Multiple){
178191
mOpenPositions.remove(position);
179-
else{
180-
if (position == mOpenPosition) {
181-
mOpenPosition = INVALID_POSITION;
182-
mPrevious = null;
183-
}
184192
}
185193
}
186194

187195
@Override
188196
public void onStartOpen(SwipeLayout layout) {
189197
if(mode == Mode.Single) {
190-
if(mOpenPosition != position){
191-
if(mPrevious != null)
192-
mPrevious.close();
193-
}
194-
mOpenPosition = position;
195-
mPrevious = layout;
198+
closeAllExcept(layout);
196199
}
197200
}
198201

199202
@Override
200203
public void onOpen(SwipeLayout layout) {
201-
if(mode == Mode.Multiple)
204+
if (mode == Mode.Multiple)
202205
mOpenPositions.add(position);
206+
else {
207+
closeAllExcept(layout);
208+
mOpenPosition = position;
209+
}
203210
}
204211

205212
public void setPosition(int position){

0 commit comments

Comments
 (0)