11package com .daimajia .swipe .implments ;
22
3+ import android .support .v7 .widget .RecyclerView ;
34import android .view .View ;
45import android .widget .BaseAdapter ;
56
1819/**
1920 * SwipeItemMangerImpl is a helper class to help all the adapters to maintain open status.
2021 */
21- public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
22+ public abstract class SwipeItemMangerImpl implements SwipeItemMangerInterface {
2223
2324 private Attributes .Mode mode = Attributes .Mode .Single ;
2425 public final int INVALID_POSITION = -1 ;
@@ -28,7 +29,8 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
2829 protected Set <Integer > mOpenPositions = new HashSet <Integer >();
2930 protected Set <SwipeLayout > mShownLayouts = new HashSet <SwipeLayout >();
3031
31- protected BaseAdapter mAdapter ;
32+ protected BaseAdapter mBaseAdapter ;
33+ protected RecyclerView .Adapter mRecyclerAdapter ;
3234
3335 public SwipeItemMangerImpl (BaseAdapter adapter ) {
3436 if (adapter == null )
@@ -37,7 +39,17 @@ public SwipeItemMangerImpl(BaseAdapter adapter) {
3739 if (!(adapter instanceof SwipeItemMangerInterface ))
3840 throw new IllegalArgumentException ("adapter should implement the SwipeAdapterInterface" );
3941
40- this .mAdapter = adapter ;
42+ this .mBaseAdapter = adapter ;
43+ }
44+
45+ public SwipeItemMangerImpl (RecyclerView .Adapter adapter ) {
46+ if (adapter == null )
47+ throw new IllegalArgumentException ("Adapter can not be null" );
48+
49+ if (!(adapter instanceof SwipeItemMangerInterface ))
50+ throw new IllegalArgumentException ("adapter should implement the SwipeAdapterInterface" );
51+
52+ this .mRecyclerAdapter = adapter ;
4153 }
4254
4355 public Attributes .Mode getMode () {
@@ -51,37 +63,22 @@ public void setMode(Attributes.Mode mode) {
5163 mOpenPosition = INVALID_POSITION ;
5264 }
5365
54- public void initialize (View target , int position ) {
55- int resId = getSwipeLayoutId (position );
56-
57- OnLayoutListener onLayoutListener = new OnLayoutListener (position );
58- SwipeLayout swipeLayout = (SwipeLayout ) target .findViewById (resId );
59- if (swipeLayout == null )
60- throw new IllegalStateException ("can not find SwipeLayout in target view" );
66+ /* initialize and updateConvertView used for AdapterManagerImpl */
67+ public abstract void initialize (View target , int position );
6168
62- SwipeMemory swipeMemory = new SwipeMemory (position );
63- swipeLayout .addSwipeListener (swipeMemory );
64- swipeLayout .addOnLayoutListener (onLayoutListener );
65- swipeLayout .setTag (resId , new ValueBox (position , swipeMemory , onLayoutListener ));
66-
67- mShownLayouts .add (swipeLayout );
68- }
69+ public abstract void updateConvertView (View target , int position );
6970
70- public void updateConvertView ( View target , int position ) {
71- int resId = getSwipeLayoutId ( position );
71+ /* bindView used for RecyclerViewManagerImpl */
72+ public abstract void bindView ( View target , int position );
7273
73- SwipeLayout swipeLayout = (SwipeLayout ) target .findViewById (resId );
74- if (swipeLayout == null )
75- throw new IllegalStateException ("can not find SwipeLayout in target view" );
76-
77- ValueBox valueBox = (ValueBox ) swipeLayout .getTag (resId );
78- valueBox .swipeMemory .setPosition (position );
79- valueBox .onLayoutListener .setPosition (position );
80- valueBox .position = position ;
81- }
82-
83- private int getSwipeLayoutId (int position ) {
84- return ((SwipeAdapterInterface ) (mAdapter )).getSwipeLayoutResourceId (position );
74+ public int getSwipeLayoutId (int position ) {
75+ if (mBaseAdapter != null ) {
76+ return ((SwipeAdapterInterface ) (mBaseAdapter )).getSwipeLayoutResourceId (position );
77+ } else if (mRecyclerAdapter != null ) {
78+ return ((SwipeAdapterInterface ) (mRecyclerAdapter )).getSwipeLayoutResourceId (position );
79+ } else {
80+ return -1 ;
81+ }
8582 }
8683
8784 @ Override
@@ -92,7 +89,11 @@ public void openItem(int position) {
9289 } else {
9390 mOpenPosition = position ;
9491 }
95- mAdapter .notifyDataSetChanged ();
92+ if (mBaseAdapter != null ) {
93+ mBaseAdapter .notifyDataSetChanged ();
94+ } else if (mRecyclerAdapter != null ) {
95+ mRecyclerAdapter .notifyDataSetChanged ();
96+ }
9697 }
9798
9899 @ Override
@@ -103,7 +104,11 @@ public void closeItem(int position) {
103104 if (mOpenPosition == position )
104105 mOpenPosition = INVALID_POSITION ;
105106 }
106- mAdapter .notifyDataSetChanged ();
107+ if (mBaseAdapter != null ) {
108+ mBaseAdapter .notifyDataSetChanged ();
109+ } else if (mRecyclerAdapter != null ) {
110+ mRecyclerAdapter .notifyDataSetChanged ();
111+ }
107112 }
108113
109114 @ Override
@@ -116,6 +121,11 @@ public void closeAllExcept(SwipeLayout layout) {
116121
117122 @ Override
118123 public void closeAllItems () {
124+ if (mode == Attributes .Mode .Multiple ) {
125+ mOpenPositions .clear ();
126+ } else {
127+ mOpenPosition = INVALID_POSITION ;
128+ }
119129 for (SwipeLayout s : mShownLayouts ) {
120130 s .close ();
121131 }
@@ -199,7 +209,6 @@ public void onClose(SwipeLayout layout) {
199209 } else {
200210 mOpenPosition = INVALID_POSITION ;
201211 }
202- mShownLayouts .remove (layout );
203212 }
204213
205214 @ Override
@@ -217,7 +226,6 @@ public void onOpen(SwipeLayout layout) {
217226 closeAllExcept (layout );
218227 mOpenPosition = position ;
219228 }
220- mShownLayouts .add (layout );
221229 }
222230
223231 public void setPosition (int position ) {
0 commit comments