Created
January 30, 2015 20:56
-
-
Save djandreski/a5f895e00a3fac23b4ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.jadi.timetracker.components; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import com.daimajia.swipe.SimpleSwipeListener; | |
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.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
/** | |
* Created by Dragan Jandreski on 20.01.2015. | |
*/ | |
public class RecyclerSwipeItemMangerImpl<VH extends RecyclerView.ViewHolder> | |
implements SwipeItemMangerInterface { | |
private SwipeItemMangerImpl.Mode mode = SwipeItemMangerImpl.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 RecyclerView.Adapter<VH> mAdapter; | |
public RecyclerSwipeItemMangerImpl(RecyclerView.Adapter<VH> 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; | |
} | |
@Override | |
public SwipeItemMangerImpl.Mode getMode() { | |
return mode; | |
} | |
@Override | |
public void setMode(SwipeItemMangerImpl.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 == SwipeItemMangerImpl.Mode.Multiple){ | |
if(!mOpenPositions.contains(position)) | |
mOpenPositions.add(position); | |
}else{ | |
mOpenPosition = position; | |
} | |
mAdapter.notifyDataSetChanged(); | |
} | |
/** | |
* Not in the standard implementation, I needed it to implement the smooth opening when item is clicked | |
* @param target The item view it is being clicked on | |
* @param position The Position of the item view | |
*/ | |
public void openItem(View target, int position) { | |
if(mode == SwipeItemMangerImpl.Mode.Multiple){ | |
if(!mOpenPositions.contains(position)) | |
mOpenPositions.add(position); | |
}else{ | |
mOpenPosition = position; | |
int resId = getSwipeLayoutId(position); | |
SwipeLayout swipeLayout = (SwipeLayout)target.findViewById(resId); | |
if(swipeLayout == null) | |
throw new IllegalStateException("can not find SwipeLayout in target view"); | |
swipeLayout.open(); | |
} | |
} | |
@Override | |
public void closeItem(int position) { | |
if(mode == SwipeItemMangerImpl.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 == SwipeItemMangerImpl.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 == SwipeItemMangerImpl.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 == SwipeItemMangerImpl.Mode.Multiple){ | |
mOpenPositions.remove(position); | |
}else{ | |
mOpenPosition = INVALID_POSITION; | |
} | |
} | |
@Override | |
public void onStartOpen(SwipeLayout layout) { | |
if(mode == SwipeItemMangerImpl.Mode.Single) { | |
closeAllExcept(layout); | |
} | |
} | |
@Override | |
public void onOpen(SwipeLayout layout) { | |
if (mode == SwipeItemMangerImpl.Mode.Multiple) | |
mOpenPositions.add(position); | |
else { | |
closeAllExcept(layout); | |
mOpenPosition = position; | |
} | |
} | |
public void setPosition(int position){ | |
this.position = position; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment