add swipe adapter utils
This commit is contained in:
parent
677c318ab9
commit
0b485a1e35
|
|
@ -0,0 +1,99 @@
|
|||
package com.daimajia.swipe.adapters;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ArraySwipeAdapter<T> extends ArrayAdapter implements SwipeItemMangerInterface,SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
{}
|
||||
public ArraySwipeAdapter(Context context, int resource) {
|
||||
super(context, resource);
|
||||
}
|
||||
|
||||
public ArraySwipeAdapter(Context context, int resource, int textViewResourceId) {
|
||||
super(context, resource, textViewResourceId);
|
||||
}
|
||||
|
||||
public ArraySwipeAdapter(Context context, int resource, T[] objects) {
|
||||
super(context, resource, objects);
|
||||
}
|
||||
|
||||
public ArraySwipeAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
|
||||
super(context, resource, textViewResourceId, objects);
|
||||
}
|
||||
|
||||
public ArraySwipeAdapter(Context context, int resource, List<T> objects) {
|
||||
super(context, resource, objects);
|
||||
}
|
||||
|
||||
public ArraySwipeAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
|
||||
super(context, resource, textViewResourceId, objects);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
boolean convertViewIsNull = convertView == null;
|
||||
View v = super.getView(position, convertView, parent);
|
||||
if(convertViewIsNull){
|
||||
mItemManger.initialize(v, position);
|
||||
}else{
|
||||
mItemManger.updateConvertView(v, position);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
mItemManger.openItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
mItemManger.closeItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllExcept(SwipeLayout layout) {
|
||||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwipeLayout> getOpenLayouts() {
|
||||
return mItemManger.getOpenLayouts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeShownLayouts(SwipeLayout layout) {
|
||||
mItemManger.removeShownLayouts(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen(int position) {
|
||||
return mItemManger.isOpen(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SwipeItemMangerImpl.Mode getMode() {
|
||||
return mItemManger.getMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMode(SwipeItemMangerImpl.Mode mode) {
|
||||
mItemManger.setMode(mode);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package com.daimajia.swipe.adapters;
|
||||
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class BaseSwipeAdapter extends BaseAdapter implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
protected SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
|
||||
/**
|
||||
* return the {@link com.daimajia.swipe.SwipeLayout} resource id, int the view item.
|
||||
* @param position
|
||||
* @return
|
||||
*/
|
||||
public abstract int getSwipeLayoutResourceId(int position);
|
||||
|
||||
/**
|
||||
* generate a new view item.
|
||||
* Never bind SwipeListener or fill values here, every item has a chance to fill value or bind
|
||||
* listeners in fillValues.
|
||||
* to fill it in {@code fillValues} method.
|
||||
* @param position
|
||||
* @param parent
|
||||
* @return
|
||||
*/
|
||||
public abstract View generateView(int position, ViewGroup parent);
|
||||
|
||||
/**
|
||||
* fill values or bind listeners to the view.
|
||||
* @param position
|
||||
* @param convertView
|
||||
*/
|
||||
public abstract void fillValues(int position, View convertView);
|
||||
|
||||
|
||||
@Override
|
||||
public final View getView(int position, View convertView, ViewGroup parent) {
|
||||
View v = convertView;
|
||||
if(v == null){
|
||||
v = generateView(position, parent);
|
||||
mItemManger.initialize(v, position);
|
||||
}else{
|
||||
mItemManger.updateConvertView(v, position);
|
||||
}
|
||||
fillValues(position, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
mItemManger.openItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
mItemManger.closeItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllExcept(SwipeLayout layout) {
|
||||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwipeLayout> getOpenLayouts() {
|
||||
return mItemManger.getOpenLayouts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeShownLayouts(SwipeLayout layout) {
|
||||
mItemManger.removeShownLayouts(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen(int position) {
|
||||
return mItemManger.isOpen(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SwipeItemMangerImpl.Mode getMode() {
|
||||
return mItemManger.getMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMode(SwipeItemMangerImpl.Mode mode) {
|
||||
mItemManger.setMode(mode);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
package com.daimajia.swipe.adapters;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.support.v4.widget.CursorAdapter;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CursorSwipeAdapter extends CursorAdapter implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
|
||||
protected CursorSwipeAdapter(Context context, Cursor c, boolean autoRequery) {
|
||||
super(context, c, autoRequery);
|
||||
}
|
||||
|
||||
protected CursorSwipeAdapter(Context context, Cursor c, int flags) {
|
||||
super(context, c, flags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
boolean convertViewIsNull = convertView == null;
|
||||
View v = super.getView(position, convertView, parent);
|
||||
if(convertViewIsNull){
|
||||
mItemManger.initialize(v, position);
|
||||
}else{
|
||||
mItemManger.updateConvertView(v, position);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
mItemManger.openItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
mItemManger.closeItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllExcept(SwipeLayout layout) {
|
||||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwipeLayout> getOpenLayouts() {
|
||||
return mItemManger.getOpenLayouts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeShownLayouts(SwipeLayout layout) {
|
||||
mItemManger.removeShownLayouts(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen(int position) {
|
||||
return mItemManger.isOpen(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SwipeItemMangerImpl.Mode getMode() {
|
||||
return mItemManger.getMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMode(SwipeItemMangerImpl.Mode mode) {
|
||||
mItemManger.setMode(mode);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package com.daimajia.swipe.adapters;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.SimpleCursorAdapter;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SimpleCursorSwipeAdapter extends SimpleCursorAdapter implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);
|
||||
|
||||
@TargetApi(11)
|
||||
protected SimpleCursorSwipeAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
|
||||
super(context, layout, c, from, to, flags);
|
||||
}
|
||||
|
||||
protected SimpleCursorSwipeAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
|
||||
super(context, layout, c, from, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
boolean convertViewIsNull = convertView == null;
|
||||
View v = super.getView(position, convertView, parent);
|
||||
if(convertViewIsNull){
|
||||
mItemManger.initialize(v, position);
|
||||
}else{
|
||||
mItemManger.updateConvertView(v, position);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
mItemManger.openItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
mItemManger.closeItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllExcept(SwipeLayout layout) {
|
||||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwipeLayout> getOpenLayouts() {
|
||||
return mItemManger.getOpenLayouts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeShownLayouts(SwipeLayout layout) {
|
||||
mItemManger.removeShownLayouts(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen(int position) {
|
||||
return mItemManger.isOpen(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SwipeItemMangerImpl.Mode getMode() {
|
||||
return mItemManger.getMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMode(SwipeItemMangerImpl.Mode mode) {
|
||||
mItemManger.setMode(mode);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,222 @@
|
|||
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 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 SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
||||
|
||||
private Mode mode = 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>();
|
||||
|
||||
protected BaseAdapter mAdapter;
|
||||
|
||||
public SwipeItemMangerImpl(BaseAdapter 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.mAdapter = adapter;
|
||||
}
|
||||
|
||||
public enum Mode{
|
||||
Single, Multiple
|
||||
};
|
||||
|
||||
public Mode getMode(){
|
||||
return mode;
|
||||
}
|
||||
|
||||
public void setMode(Mode mode){
|
||||
this.mode = mode;
|
||||
mOpenPositions.clear();
|
||||
mShownLayouts.clear();
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
if(mode == Mode.Multiple){
|
||||
if(!mOpenPositions.contains(position))
|
||||
mOpenPositions.add(position);
|
||||
}else{
|
||||
mOpenPosition = position;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
if(mode == 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 removeShownLayouts(SwipeLayout layout) {
|
||||
mShownLayouts.remove(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
if(mode == 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 == 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 == Mode.Multiple){
|
||||
mOpenPositions.remove(position);
|
||||
}else{
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartOpen(SwipeLayout layout) {
|
||||
if(mode == Mode.Single) {
|
||||
closeAllExcept(layout);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(SwipeLayout layout) {
|
||||
if (mode == Mode.Multiple)
|
||||
mOpenPositions.add(position);
|
||||
else {
|
||||
closeAllExcept(layout);
|
||||
mOpenPosition = position;
|
||||
}
|
||||
}
|
||||
|
||||
public void setPosition(int position){
|
||||
this.position = position;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.daimajia.swipe.interfaces;
|
||||
|
||||
public interface SwipeAdapterInterface {
|
||||
public int getSwipeLayoutResourceId(int position);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.daimajia.swipe.interfaces;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SwipeItemMangerInterface {
|
||||
|
||||
public void openItem(int position);
|
||||
|
||||
public void closeItem(int position);
|
||||
|
||||
public void closeAllExcept(SwipeLayout layout);
|
||||
|
||||
public List<Integer> getOpenItems();
|
||||
|
||||
public List<SwipeLayout> getOpenLayouts();
|
||||
|
||||
public void removeShownLayouts(SwipeLayout layout);
|
||||
|
||||
public boolean isOpen(int position);
|
||||
|
||||
public SwipeItemMangerImpl.Mode getMode();
|
||||
|
||||
public void setMode(SwipeItemMangerImpl.Mode mode);
|
||||
}
|
||||
Loading…
Reference in New Issue