Modularize Manager class
Seperated out the code thats used more then once and created 2 seperate subclasses called Adapter and Recycler based on the method of 'lists'.
This commit is contained in:
parent
418898c33c
commit
baafdcbb95
|
|
@ -90,7 +90,7 @@ public class RecyclerViewAdapter extends RecyclerSwipeAdapter<RecyclerViewAdapte
|
|||
});
|
||||
viewHolder.textViewPos.setText((position + 1) + ".");
|
||||
viewHolder.textViewData.setText(item);
|
||||
mItemManger.bind(viewHolder.itemView, position);
|
||||
mItemManger.bindView(viewHolder.itemView, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import android.view.ViewGroup;
|
|||
import android.widget.ArrayAdapter;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemAdapterMangerImpl;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
|
@ -15,7 +16,7 @@ import java.util.List;
|
|||
|
||||
public abstract class ArraySwipeAdapter<T> extends ArrayAdapter implements SwipeItemMangerInterface,SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
private SwipeItemAdapterMangerImpl mItemManger = new SwipeItemAdapterMangerImpl(this);
|
||||
{}
|
||||
public ArraySwipeAdapter(Context context, int resource) {
|
||||
super(context, resource);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import android.view.ViewGroup;
|
|||
import android.widget.BaseAdapter;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemAdapterMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
|
@ -14,7 +15,7 @@ import java.util.List;
|
|||
|
||||
public abstract class BaseSwipeAdapter extends BaseAdapter implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
protected SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
protected SwipeItemAdapterMangerImpl mItemManger = new SwipeItemAdapterMangerImpl(this);
|
||||
|
||||
/**
|
||||
* return the {@link com.daimajia.swipe.SwipeLayout} resource id, int the view item.
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemAdapterMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
|
@ -16,7 +17,7 @@ import java.util.List;
|
|||
|
||||
public abstract class CursorSwipeAdapter extends CursorAdapter implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
private SwipeItemAdapterMangerImpl mItemManger = new SwipeItemAdapterMangerImpl(this);
|
||||
|
||||
protected CursorSwipeAdapter(Context context, Cursor c, boolean autoRequery) {
|
||||
super(context, c, autoRequery);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemAdapterMangerImpl;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
|
@ -16,7 +17,7 @@ import java.util.List;
|
|||
|
||||
public abstract class SimpleCursorSwipeAdapter extends SimpleCursorAdapter implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
private SwipeItemAdapterMangerImpl mItemManger = new SwipeItemAdapterMangerImpl(this);
|
||||
|
||||
protected SimpleCursorSwipeAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
|
||||
super(context, layout, c, from, to, flags);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
package com.daimajia.swipe.implments;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.BaseAdapter;
|
||||
|
||||
import com.daimajia.swipe.SimpleSwipeListener;
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
import com.daimajia.swipe.util.Attributes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* SwipeItemMangerImpl is a helper class to help all the adapters to maintain open status.
|
||||
*/
|
||||
public class SwipeItemAdapterMangerImpl extends SwipeItemMangerImpl{
|
||||
|
||||
protected BaseAdapter mAdapter;
|
||||
|
||||
public SwipeItemAdapterMangerImpl(BaseAdapter adapter) {
|
||||
super(adapter);
|
||||
this.mAdapter = adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(View target, int position) {
|
||||
int resId = getSwipeLayoutId(position);
|
||||
|
||||
OnLayoutListener onLayoutListener = new OnLayoutListener(position);
|
||||
SwipeLayout swipeLayout = (SwipeLayout) target.findViewById(resId);
|
||||
if (swipeLayout == null)
|
||||
throw new IllegalStateException("can not find SwipeLayout in target view");
|
||||
|
||||
SwipeMemory swipeMemory = new SwipeMemory(position);
|
||||
swipeLayout.addSwipeListener(swipeMemory);
|
||||
swipeLayout.addOnLayoutListener(onLayoutListener);
|
||||
swipeLayout.setTag(resId, new ValueBox(position, swipeMemory, onLayoutListener));
|
||||
|
||||
mShownLayouts.add(swipeLayout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateConvertView(View target, int position) {
|
||||
int resId = getSwipeLayoutId(position);
|
||||
|
||||
SwipeLayout swipeLayout = (SwipeLayout) target.findViewById(resId);
|
||||
if (swipeLayout == null)
|
||||
throw new IllegalStateException("can not find SwipeLayout in target view");
|
||||
|
||||
ValueBox valueBox = (ValueBox) swipeLayout.getTag(resId);
|
||||
valueBox.swipeMemory.setPosition(position);
|
||||
valueBox.onLayoutListener.setPosition(position);
|
||||
valueBox.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindView(View target, int position){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.daimajia.swipe.implments;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
import android.widget.BaseAdapter;
|
||||
|
||||
|
|
@ -18,7 +19,7 @@ import java.util.Set;
|
|||
/**
|
||||
* SwipeItemMangerImpl is a helper class to help all the adapters to maintain open status.
|
||||
*/
|
||||
public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
||||
public abstract class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
||||
|
||||
private Attributes.Mode mode = Attributes.Mode.Single;
|
||||
public final int INVALID_POSITION = -1;
|
||||
|
|
@ -28,7 +29,8 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
protected Set<Integer> mOpenPositions = new HashSet<Integer>();
|
||||
protected Set<SwipeLayout> mShownLayouts = new HashSet<SwipeLayout>();
|
||||
|
||||
protected BaseAdapter mAdapter;
|
||||
protected BaseAdapter mBaseAdapter;
|
||||
protected RecyclerView.Adapter mRecyclerAdapter;
|
||||
|
||||
public SwipeItemMangerImpl(BaseAdapter adapter) {
|
||||
if (adapter == null)
|
||||
|
|
@ -37,7 +39,17 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
if (!(adapter instanceof SwipeItemMangerInterface))
|
||||
throw new IllegalArgumentException("adapter should implement the SwipeAdapterInterface");
|
||||
|
||||
this.mAdapter = adapter;
|
||||
this.mBaseAdapter = adapter;
|
||||
}
|
||||
|
||||
public SwipeItemMangerImpl(RecyclerView.Adapter adapter) {
|
||||
if (adapter == null)
|
||||
throw new IllegalArgumentException("Adapter can not be null");
|
||||
|
||||
if (!(adapter instanceof SwipeItemMangerInterface))
|
||||
throw new IllegalArgumentException("adapter should implement the SwipeAdapterInterface");
|
||||
|
||||
this.mRecyclerAdapter = adapter;
|
||||
}
|
||||
|
||||
public Attributes.Mode getMode() {
|
||||
|
|
@ -51,37 +63,22 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
|
||||
public void initialize(View target, int position) {
|
||||
int resId = getSwipeLayoutId(position);
|
||||
/* initialize and updateConvertView used for AdapterManagerImpl */
|
||||
public abstract void initialize(View target, int position);
|
||||
|
||||
OnLayoutListener onLayoutListener = new OnLayoutListener(position);
|
||||
SwipeLayout swipeLayout = (SwipeLayout) target.findViewById(resId);
|
||||
if (swipeLayout == null)
|
||||
throw new IllegalStateException("can not find SwipeLayout in target view");
|
||||
public abstract void updateConvertView(View target, int position);
|
||||
|
||||
SwipeMemory swipeMemory = new SwipeMemory(position);
|
||||
swipeLayout.addSwipeListener(swipeMemory);
|
||||
swipeLayout.addOnLayoutListener(onLayoutListener);
|
||||
swipeLayout.setTag(resId, new ValueBox(position, swipeMemory, onLayoutListener));
|
||||
/* bindView used for RecyclerViewManagerImpl */
|
||||
public abstract void bindView(View target, int position);
|
||||
|
||||
mShownLayouts.add(swipeLayout);
|
||||
}
|
||||
|
||||
public void updateConvertView(View target, int position) {
|
||||
int resId = getSwipeLayoutId(position);
|
||||
|
||||
SwipeLayout swipeLayout = (SwipeLayout) target.findViewById(resId);
|
||||
if (swipeLayout == null)
|
||||
throw new IllegalStateException("can not find SwipeLayout in target view");
|
||||
|
||||
ValueBox valueBox = (ValueBox) swipeLayout.getTag(resId);
|
||||
valueBox.swipeMemory.setPosition(position);
|
||||
valueBox.onLayoutListener.setPosition(position);
|
||||
valueBox.position = position;
|
||||
}
|
||||
|
||||
private int getSwipeLayoutId(int position) {
|
||||
return ((SwipeAdapterInterface) (mAdapter)).getSwipeLayoutResourceId(position);
|
||||
public int getSwipeLayoutId(int position) {
|
||||
if (mBaseAdapter != null) {
|
||||
return ((SwipeAdapterInterface) (mBaseAdapter)).getSwipeLayoutResourceId(position);
|
||||
} else if (mRecyclerAdapter != null) {
|
||||
return ((SwipeAdapterInterface) (mRecyclerAdapter)).getSwipeLayoutResourceId(position);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -92,7 +89,11 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
} else {
|
||||
mOpenPosition = position;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
if (mBaseAdapter != null) {
|
||||
mBaseAdapter.notifyDataSetChanged();
|
||||
} else if (mRecyclerAdapter != null) {
|
||||
mRecyclerAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -103,7 +104,11 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
if (mOpenPosition == position)
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
if (mBaseAdapter != null) {
|
||||
mBaseAdapter.notifyDataSetChanged();
|
||||
} else if (mRecyclerAdapter != null) {
|
||||
mRecyclerAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -116,6 +121,11 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
|
||||
@Override
|
||||
public void closeAllItems() {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
mOpenPositions.clear();
|
||||
} else {
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
for (SwipeLayout s : mShownLayouts) {
|
||||
s.close();
|
||||
}
|
||||
|
|
@ -199,7 +209,6 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
} else {
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
mShownLayouts.remove(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -217,7 +226,6 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
closeAllExcept(layout);
|
||||
mOpenPosition = position;
|
||||
}
|
||||
mShownLayouts.add(layout);
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
|
|
|
|||
|
|
@ -16,42 +16,19 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* SwipeItemMangerImpl is a helper class to help all the adapters to maintain open status.
|
||||
* SwipeItemRecyclerMangerImpl is a helper class to help the RecyclerView to maintain open status.
|
||||
*/
|
||||
public class SwipeItemRecyclerMangerImpl implements SwipeItemMangerInterface {
|
||||
|
||||
private Attributes.Mode mode = Attributes.Mode.Single;
|
||||
public final int INVALID_POSITION = -1;
|
||||
|
||||
protected int mOpenPosition = INVALID_POSITION;
|
||||
|
||||
protected Set<Integer> mOpenPositions = new HashSet<Integer>();
|
||||
protected Set<SwipeLayout> mShownLayouts = new HashSet<SwipeLayout>();
|
||||
public class SwipeItemRecyclerMangerImpl extends SwipeItemMangerImpl{
|
||||
|
||||
protected RecyclerView.Adapter mAdapter;
|
||||
|
||||
public SwipeItemRecyclerMangerImpl(RecyclerView.Adapter adapter) {
|
||||
if (adapter == null)
|
||||
throw new IllegalArgumentException("Adapter can not be null");
|
||||
|
||||
if (!(adapter instanceof SwipeItemMangerInterface))
|
||||
throw new IllegalArgumentException("adapter should implement the SwipeAdapterInterface");
|
||||
|
||||
super(adapter);
|
||||
this.mAdapter = adapter;
|
||||
}
|
||||
|
||||
public Attributes.Mode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
public void setMode(Attributes.Mode mode) {
|
||||
this.mode = mode;
|
||||
mOpenPositions.clear();
|
||||
mShownLayouts.clear();
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
|
||||
public void bind(View target, int position) {
|
||||
@Override
|
||||
public void bindView(View target, int position) {
|
||||
int resId = getSwipeLayoutId(position);
|
||||
|
||||
OnLayoutListener onLayoutListener = new OnLayoutListener(position);
|
||||
|
|
@ -73,152 +50,14 @@ public class SwipeItemRecyclerMangerImpl implements SwipeItemMangerInterface {
|
|||
}
|
||||
}
|
||||
|
||||
private int getSwipeLayoutId(int position) {
|
||||
return ((SwipeAdapterInterface) (mAdapter)).getSwipeLayoutResourceId(position);
|
||||
@Override
|
||||
public void initialize(View target, int position) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
if (!mOpenPositions.contains(position))
|
||||
mOpenPositions.add(position);
|
||||
} else {
|
||||
mOpenPosition = position;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
public void updateConvertView(View target, int position) {
|
||||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
mOpenPositions.remove(position);
|
||||
} else {
|
||||
if (mOpenPosition == position)
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllExcept(SwipeLayout layout) {
|
||||
for (SwipeLayout s : mShownLayouts) {
|
||||
if (s != layout)
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllItems() {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
mOpenPositions.clear();
|
||||
} else {
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
for (SwipeLayout s : mShownLayouts) {
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeShownLayouts(SwipeLayout layout) {
|
||||
mShownLayouts.remove(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
return new ArrayList<Integer>(mOpenPositions);
|
||||
} else {
|
||||
return Arrays.asList(mOpenPosition);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwipeLayout> getOpenLayouts() {
|
||||
return new ArrayList<SwipeLayout>(mShownLayouts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen(int position) {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
return mOpenPositions.contains(position);
|
||||
} else {
|
||||
return mOpenPosition == position;
|
||||
}
|
||||
}
|
||||
|
||||
class ValueBox {
|
||||
OnLayoutListener onLayoutListener;
|
||||
SwipeMemory swipeMemory;
|
||||
int position;
|
||||
|
||||
ValueBox(int position, SwipeMemory swipeMemory, OnLayoutListener onLayoutListener) {
|
||||
this.swipeMemory = swipeMemory;
|
||||
this.onLayoutListener = onLayoutListener;
|
||||
this.position = position;
|
||||
}
|
||||
}
|
||||
|
||||
class OnLayoutListener implements SwipeLayout.OnLayout {
|
||||
|
||||
private int position;
|
||||
|
||||
OnLayoutListener(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLayout(SwipeLayout v) {
|
||||
if (isOpen(position)) {
|
||||
v.open(false, false);
|
||||
} else {
|
||||
v.close(false, false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SwipeMemory extends SimpleSwipeListener {
|
||||
|
||||
private int position;
|
||||
|
||||
SwipeMemory(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(SwipeLayout layout) {
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
mOpenPositions.remove(position);
|
||||
} else {
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartOpen(SwipeLayout layout) {
|
||||
if (mode == Attributes.Mode.Single) {
|
||||
closeAllExcept(layout);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(SwipeLayout layout) {
|
||||
if (mode == Attributes.Mode.Multiple)
|
||||
mOpenPositions.add(position);
|
||||
else {
|
||||
closeAllExcept(layout);
|
||||
mOpenPosition = position;
|
||||
}
|
||||
}
|
||||
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue