Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swipe Layout Manager #35

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,14 @@ public interface OnSwipeListener {

void onRightStickyEdge(SwipeLayout swipeLayout, boolean moveToRight);
}

public abstract static class SimpleSwipeListener implements OnSwipeListener {
@Override public void onBeginSwipe(SwipeLayout swipeLayout, boolean moveToRight) { }

@Override public void onSwipeClampReached(SwipeLayout swipeLayout, boolean moveToRight) { }

@Override public void onLeftStickyEdge(SwipeLayout swipeLayout, boolean moveToRight) { }

@Override public void onRightStickyEdge(SwipeLayout swipeLayout, boolean moveToRight) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ru.rambler.libs.swipe_layout;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class SwipeLayoutManager
{
private Map<String, WeakReference<SwipeLayout>> layouts = Collections.synchronizedMap(new HashMap<String, WeakReference<SwipeLayout>>());

private final Object syncObject = new Object();

private boolean openOnlyOne;

public SwipeLayoutManager()
{
this(false);
}

public SwipeLayoutManager(boolean openOnlyOne)
{
this.openOnlyOne = openOnlyOne;
}

public void bind(SwipeLayout swipeLayout, String id)
{
layouts.put(id, new WeakReference<>(swipeLayout));

swipeLayout.setOnSwipeListener(new SwipeLayout.SimpleSwipeListener() {
@Override
public void onBeginSwipe(SwipeLayout swipeLayout, boolean moveToRight)
{
if (openOnlyOne)
closeOthers(swipeLayout, true);
}
});
}

public void closeAll(boolean animate)
{
synchronized (syncObject)
{
for (WeakReference<SwipeLayout> layoutRef: layouts.values())
{
SwipeLayout layout = layoutRef.get();
if (layout != null)
reset(layout, animate);
}
}
}

public void closeLayout(SwipeLayout layout, boolean animate)
{
synchronized (syncObject)
{
reset(layout, animate);
}
}

public void closeLayout(String id, boolean animate)
{
synchronized (syncObject)
{
WeakReference<SwipeLayout> layoutRef = layouts.get(id);
if (layoutRef != null)
{
SwipeLayout layout = layoutRef.get();
if (layout != null)
reset(layout, animate);
}
}
}

public void closeOthers(SwipeLayout excludedLayout, boolean animate)
{
synchronized (syncObject)
{
for (WeakReference<SwipeLayout> layoutRef: layouts.values())
{
SwipeLayout layout = layoutRef.get();
if (layout != excludedLayout)
reset(layout, animate);
}
}
}

private void reset(SwipeLayout layoutToClose, boolean animate)
{
if (animate) layoutToClose.animateReset();
else layoutToClose.reset();
}

public void setOpenOnlyOne(boolean openOnlyOne)
{
this.openOnlyOne = openOnlyOne;
}

public boolean isOpenOnlyOne()
{
return openOnlyOne;
}
}
1 change: 1 addition & 0 deletions testapp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildToolsVersion '28.0.2'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.widget.Toast;

import ru.rambler.libs.swipe_layout.SwipeLayout;
import ru.rambler.libs.swipe_layout.SwipeLayoutManager;


public class MainActivity extends AppCompatActivity {

Expand All @@ -31,6 +33,8 @@ private static class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private final int COUNT = 30;
private final int[] itemsOffset = new int[COUNT];

private final SwipeLayoutManager swipeLayoutManager = new SwipeLayoutManager(true);

@Override
public int getItemViewType(int position) {
return position % 3;
Expand Down Expand Up @@ -103,8 +107,11 @@ public void onRightStickyEdge(SwipeLayout swipeLayout, boolean moveToRight) {

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textViewPos.setText("#" + (position + 1));
String uniqueId = "#" + (position + 1);
holder.textViewPos.setText(uniqueId);
holder.swipeLayout.setOffset(itemsOffset[position]);

swipeLayoutManager.bind(holder.swipeLayout, uniqueId);
}

@Override
Expand Down