Setup Clicks, Deletion, Animation
Removed 2-way-view in favor of more simplistic items. Based on research most people have used the ViewHolder for onItemClick listener support, sometimes I've seen a ViewHolder implement View.OnClickListener, however that didn't work for our example so our root view has an itemclicklistener set. On top of that, basic animations were added via a top starred github library. The delete is hooked up to actually remove the item from the list. However it seems glitchy in comparison. It should be noted that it will be left up to the user to determine UI/UX aspects such as removal. The Inbox App by Google uses a swipe to remove items, where the Gmail App by Google uses a swipe to 'queue' an item for dismisal, but not dismissing it until the user makes another interaction such as scrolling or clicking another email. A 'closeAllItems()' method was added in our Managers, as well as a minor fix that was needed by adding 'shown layouts' correctly in our SwipeListener.
This commit is contained in:
parent
7ea40c5978
commit
418898c33c
|
|
@ -1,7 +1,7 @@
|
|||
apply plugin: 'com.android.application'
|
||||
|
||||
repositories {
|
||||
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
|
||||
jcenter()
|
||||
}
|
||||
|
||||
android {
|
||||
|
|
@ -29,6 +29,6 @@ dependencies {
|
|||
compile 'com.daimajia.easing:library:1.0.0@aar'
|
||||
compile 'com.daimajia.androidanimations:library:1.1.2@aar'
|
||||
compile 'com.nineoldandroids:library:2.4.0'
|
||||
compile 'org.lucasr.twowayview:core:1.0.0-SNAPSHOT@aar'
|
||||
compile 'org.lucasr.twowayview:layouts:1.0.0-SNAPSHOT@aar'
|
||||
// This dude gave a shoutout to you (daimajia) on his github page:
|
||||
compile 'jp.wasabeef:recyclerview-animators:1.0.3@aar'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.daimajia.swipedemo">
|
||||
|
||||
<uses-sdk tools:overrideLibrary="org.lucasr.twowayview, org.lucasr.twowayview.widget" />
|
||||
<uses-sdk tools:overrideLibrary="org.lucasr.twowayview, org.lucasr.twowayview.widget, jp.wasabeef.recyclerview" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
|
|
|
|||
|
|
@ -4,41 +4,39 @@ import android.app.ActionBar;
|
|||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.daimajia.swipe.util.Attributes;
|
||||
import com.daimajia.swipedemo.adapter.RecyclerViewAdapter;
|
||||
import com.daimajia.swipedemo.adapter.RecyclerViewAdvancedAdapter;
|
||||
import com.daimajia.swipedemo.adapter.util.RecyclerItemClickListener;
|
||||
|
||||
import org.lucasr.twowayview.ItemClickSupport;
|
||||
import org.lucasr.twowayview.TwoWayLayoutManager;
|
||||
import org.lucasr.twowayview.widget.DividerItemDecoration;
|
||||
import org.lucasr.twowayview.widget.ListLayoutManager;
|
||||
import com.daimajia.swipedemo.adapter.util.DividerItemDecoration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import jp.wasabeef.recyclerview.animators.FadeInLeftAnimator;
|
||||
|
||||
public class RecyclerViewExample extends Activity {
|
||||
|
||||
/**
|
||||
* RecyclerView: The new recycler view replaces the list view. Its more modular and therefore we
|
||||
* must implement some of the functionality ourselves and attach it to our recyclerview.
|
||||
* <p/>
|
||||
* 1) Position items on the screen: This is done with LayoutManagers
|
||||
* 2) Animate & Decorate views: This is done with ItemAnimators & ItemDecorators
|
||||
* 3) Handle any touch events apart from scrolling: This is now done in our adapter's ViewHolder
|
||||
*/
|
||||
|
||||
private RecyclerView recyclerView;
|
||||
private RecyclerView.Adapter mAdapter;
|
||||
// RecyclerView.LayoutManager: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.LayoutManager.html
|
||||
// Our LayoutManager uses: https://github.com/lucasr/twoway-view to help with decoration and can be used for a more advanced config as well.
|
||||
// Read http://lucasr.org/2014/07/31/the-new-twowayview/ for a better understanding
|
||||
private RecyclerView.LayoutManager mLayoutManager;
|
||||
|
||||
private Context mContext = this;
|
||||
private ArrayList<String> mDataSet;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
|
@ -51,46 +49,22 @@ public class RecyclerViewExample extends Activity {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sample data.
|
||||
*/
|
||||
// Layout Managers:
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
// Item Decorator:
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.divider)));
|
||||
recyclerView.setItemAnimator(new FadeInLeftAnimator());
|
||||
|
||||
// Adapter:
|
||||
String[] adapterData = new String[]{"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};
|
||||
|
||||
// Uses a ListLayout manager from TwoWayView Lib:
|
||||
mLayoutManager = new ListLayoutManager(this, TwoWayLayoutManager.Orientation.VERTICAL);
|
||||
recyclerView.setLayoutManager(mLayoutManager);
|
||||
final Drawable divider = getResources().getDrawable(R.drawable.divider);
|
||||
recyclerView.addItemDecoration(new DividerItemDecoration(divider));
|
||||
|
||||
mDataSet = new ArrayList<String>(Arrays.asList(adapterData));
|
||||
mAdapter = new RecyclerViewAdapter(this, mDataSet);
|
||||
|
||||
((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
|
||||
recyclerView.setAdapter(mAdapter);
|
||||
|
||||
/* Listeners */
|
||||
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(View view, int position) {
|
||||
Toast.makeText(mContext, "Clicked:" + position, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}));
|
||||
recyclerView.setOnScrollListener(onScrollListener);
|
||||
|
||||
|
||||
// TODO: Item Selection Support for RecyclerView
|
||||
// recyclerView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
// @Override
|
||||
// public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
// Log.e("ListView", "onItemSelected:" + position);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onNothingSelected(AdapterView<?> parent) {
|
||||
// Log.e("ListView", "onNothingSelected:");
|
||||
// }
|
||||
// });
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -106,6 +80,7 @@ public class RecyclerViewExample extends Activity {
|
|||
@Override
|
||||
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
// Could hide open views here if you wanted. //
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package com.daimajia.swipedemo.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
|
@ -17,57 +20,76 @@ import com.daimajia.swipedemo.R;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RecyclerViewAdapter extends RecyclerSwipeAdapter {
|
||||
public class RecyclerViewAdapter extends RecyclerSwipeAdapter<RecyclerViewAdapter.SimpleViewHolder> {
|
||||
|
||||
public static class SimpleViewHolder extends ViewHolder{
|
||||
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
|
||||
SwipeLayout swipeLayout;
|
||||
TextView textViewPos;
|
||||
TextView textViewData;
|
||||
Button buttonDelete;
|
||||
|
||||
public SimpleViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
|
||||
textViewPos = (TextView) itemView.findViewById(R.id.position);
|
||||
textViewData = (TextView) itemView.findViewById(R.id.text_data);
|
||||
buttonDelete = (Button) itemView.findViewById(R.id.delete);
|
||||
|
||||
itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.d(getClass().getSimpleName(), "onItemSelected: " + textViewData.getText().toString());
|
||||
Toast.makeText(view.getContext(), "onItemSelected: " + textViewData.getText().toString(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private Context mContext;
|
||||
private ArrayList<String> mDataset;
|
||||
|
||||
protected SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
|
||||
//protected SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
|
||||
|
||||
public RecyclerViewAdapter(Context context, ArrayList<String> objects) {
|
||||
super(context, objects);
|
||||
this.mContext = context;
|
||||
this.mDataset = objects;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder createRecyclerViewHolder(ViewGroup parent, int viewType) {
|
||||
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
|
||||
return new SimpleViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindRecyclerViewHolder(ViewHolder viewHolder, final int position) {
|
||||
SimpleViewHolder simpleViewHolder = (SimpleViewHolder) viewHolder;
|
||||
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
|
||||
String item = mDataset.get(position);
|
||||
simpleViewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
|
||||
simpleViewHolder.swipeLayout.addSwipeListener(new SimpleSwipeListener() {
|
||||
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
|
||||
viewHolder.swipeLayout.addSwipeListener(new SimpleSwipeListener() {
|
||||
@Override
|
||||
public void onOpen(SwipeLayout layout) {
|
||||
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
|
||||
}
|
||||
});
|
||||
simpleViewHolder.swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
|
||||
viewHolder.swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
|
||||
@Override
|
||||
public void onDoubleClick(SwipeLayout layout, boolean surface) {
|
||||
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
simpleViewHolder.textViewPos.setText((position + 1) + ".");
|
||||
simpleViewHolder.textViewData.setText(item);
|
||||
viewHolder.buttonDelete.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
mItemManger.removeShownLayouts(viewHolder.swipeLayout);
|
||||
mDataset.remove(position);
|
||||
notifyItemRemoved(position);
|
||||
notifyItemRangeChanged(position, mDataset.size());
|
||||
mItemManger.closeAllItems();
|
||||
Toast.makeText(view.getContext(), "Deleted " + viewHolder.textViewData.getText().toString() + "!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
viewHolder.textViewPos.setText((position + 1) + ".");
|
||||
viewHolder.textViewData.setText(item);
|
||||
mItemManger.bind(viewHolder.itemView, position);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,135 +0,0 @@
|
|||
package com.daimajia.swipedemo.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.daimajia.androidanimations.library.Techniques;
|
||||
import com.daimajia.androidanimations.library.YoYo;
|
||||
import com.daimajia.swipe.SimpleSwipeListener;
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
import com.daimajia.swipe.implments.SwipeItemRecyclerMangerImpl;
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
|
||||
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
|
||||
import com.daimajia.swipe.util.Attributes;
|
||||
import com.daimajia.swipedemo.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RecyclerViewAdvancedAdapter extends RecyclerView.Adapter<RecyclerViewAdvancedAdapter.ViewHolder> implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
SwipeLayout swipeLayout;
|
||||
TextView textViewPos;
|
||||
TextView textViewData;
|
||||
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
|
||||
textViewPos = (TextView) itemView.findViewById(R.id.position);
|
||||
textViewData = (TextView) itemView.findViewById(R.id.text_data);
|
||||
}
|
||||
}
|
||||
|
||||
private Context mContext;
|
||||
private ArrayList<String> mDataset;
|
||||
|
||||
protected SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
|
||||
|
||||
|
||||
public RecyclerViewAdvancedAdapter(Context context, ArrayList<String> objects) {
|
||||
this.mContext = context;
|
||||
this.mDataset = objects;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
|
||||
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
|
||||
viewHolder.swipeLayout.addSwipeListener(new SimpleSwipeListener() {
|
||||
@Override
|
||||
public void onOpen(SwipeLayout layout) {
|
||||
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
|
||||
}
|
||||
});
|
||||
viewHolder.swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
|
||||
@Override
|
||||
public void onDoubleClick(SwipeLayout layout, boolean surface) {
|
||||
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
viewHolder.textViewPos.setText((position + 1) + ".");
|
||||
viewHolder.textViewData.setText(mDataset.get(position));
|
||||
mItemManger.bind(viewHolder.itemView, position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mDataset.size();
|
||||
}
|
||||
|
||||
@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 Attributes.Mode getMode() {
|
||||
return mItemManger.getMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMode(Attributes.Mode mode) {
|
||||
mItemManger.setMode(mode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSwipeLayoutResourceId(int position) {
|
||||
return R.id.swipe;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
package com.daimajia.swipedemo.adapter.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
|
||||
|
||||
private Drawable mDivider;
|
||||
private boolean mShowFirstDivider = false;
|
||||
private boolean mShowLastDivider = false;
|
||||
|
||||
|
||||
public DividerItemDecoration(Context context, AttributeSet attrs) {
|
||||
final TypedArray a = context
|
||||
.obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider});
|
||||
mDivider = a.getDrawable(0);
|
||||
a.recycle();
|
||||
}
|
||||
|
||||
public DividerItemDecoration(Context context, AttributeSet attrs, boolean showFirstDivider,
|
||||
boolean showLastDivider) {
|
||||
this(context, attrs);
|
||||
mShowFirstDivider = showFirstDivider;
|
||||
mShowLastDivider = showLastDivider;
|
||||
}
|
||||
|
||||
public DividerItemDecoration(Drawable divider) {
|
||||
mDivider = divider;
|
||||
}
|
||||
|
||||
public DividerItemDecoration(Drawable divider, boolean showFirstDivider,
|
||||
boolean showLastDivider) {
|
||||
this(divider);
|
||||
mShowFirstDivider = showFirstDivider;
|
||||
mShowLastDivider = showLastDivider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
|
||||
RecyclerView.State state) {
|
||||
super.getItemOffsets(outRect, view, parent, state);
|
||||
if (mDivider == null) {
|
||||
return;
|
||||
}
|
||||
if (parent.getChildPosition(view) < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
|
||||
outRect.top = mDivider.getIntrinsicHeight();
|
||||
} else {
|
||||
outRect.left = mDivider.getIntrinsicWidth();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
|
||||
if (mDivider == null) {
|
||||
super.onDrawOver(c, parent, state);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialization needed to avoid compiler warning
|
||||
int left = 0, right = 0, top = 0, bottom = 0, size;
|
||||
int orientation = getOrientation(parent);
|
||||
int childCount = parent.getChildCount();
|
||||
|
||||
if (orientation == LinearLayoutManager.VERTICAL) {
|
||||
size = mDivider.getIntrinsicHeight();
|
||||
left = parent.getPaddingLeft();
|
||||
right = parent.getWidth() - parent.getPaddingRight();
|
||||
} else { //horizontal
|
||||
size = mDivider.getIntrinsicWidth();
|
||||
top = parent.getPaddingTop();
|
||||
bottom = parent.getHeight() - parent.getPaddingBottom();
|
||||
}
|
||||
|
||||
for (int i = mShowFirstDivider ? 0 : 1; i < childCount; i++) {
|
||||
View child = parent.getChildAt(i);
|
||||
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
|
||||
if (orientation == LinearLayoutManager.VERTICAL) {
|
||||
top = child.getTop() - params.topMargin;
|
||||
bottom = top + size;
|
||||
} else { //horizontal
|
||||
left = child.getLeft() - params.leftMargin;
|
||||
right = left + size;
|
||||
}
|
||||
mDivider.setBounds(left, top, right, bottom);
|
||||
mDivider.draw(c);
|
||||
}
|
||||
|
||||
// show last divider
|
||||
if (mShowLastDivider && childCount > 0) {
|
||||
View child = parent.getChildAt(childCount - 1);
|
||||
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
if (orientation == LinearLayoutManager.VERTICAL) {
|
||||
top = child.getBottom() + params.bottomMargin;
|
||||
bottom = top + size;
|
||||
} else { // horizontal
|
||||
left = child.getRight() + params.rightMargin;
|
||||
right = left + size;
|
||||
}
|
||||
mDivider.setBounds(left, top, right, bottom);
|
||||
mDivider.draw(c);
|
||||
}
|
||||
}
|
||||
|
||||
private int getOrientation(RecyclerView parent) {
|
||||
if (parent.getLayoutManager() instanceof LinearLayoutManager) {
|
||||
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
|
||||
return layoutManager.getOrientation();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"DividerItemDecoration can only be used with a LinearLayoutManager.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -58,8 +58,7 @@
|
|||
android:id="@+id/text_data"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:tag="Hover"
|
||||
android:text="Do not, for one repulse, forgo the purpose that you resolved to effort. " />
|
||||
android:tag="Hover"/>
|
||||
</LinearLayout>
|
||||
</com.daimajia.swipe.SwipeLayout>
|
||||
</LinearLayout>
|
||||
|
|
|
|||
|
|
@ -68,6 +68,11 @@ public abstract class ArraySwipeAdapter<T> extends ArrayAdapter implements Swipe
|
|||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllItems() {
|
||||
mItemManger.closeAllItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
|
|
|
|||
|
|
@ -70,6 +70,11 @@ public abstract class BaseSwipeAdapter extends BaseAdapter implements SwipeItemM
|
|||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllItems() {
|
||||
mItemManger.closeAllItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package com.daimajia.swipe.adapters;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.daimajia.swipe.SwipeLayout;
|
||||
|
|
@ -11,53 +9,17 @@ 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.List;
|
||||
|
||||
public abstract class RecyclerSwipeAdapter extends RecyclerView.Adapter<RecyclerSwipeAdapter.ViewHolder> implements SwipeItemMangerInterface,SwipeAdapterInterface {
|
||||
public abstract class RecyclerSwipeAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements SwipeItemMangerInterface, SwipeAdapterInterface {
|
||||
|
||||
private SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
|
||||
|
||||
/**
|
||||
* This must be over-ridden
|
||||
* Containes the view of the swiped item in the recycler.view
|
||||
*/
|
||||
public static abstract class ViewHolder extends RecyclerView.ViewHolder {
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
};
|
||||
|
||||
private Context mContext;
|
||||
private ArrayList mDataset;
|
||||
|
||||
public RecyclerSwipeAdapter(Context context, ArrayList objects) {
|
||||
this.mContext = context;
|
||||
this.mDataset = objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param parent
|
||||
* @param viewType
|
||||
* @return View
|
||||
*/
|
||||
public abstract ViewHolder createRecyclerViewHolder(ViewGroup parent, int viewType);
|
||||
public SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
return createRecyclerViewHolder(parent, viewType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param viewHolder
|
||||
* @param position
|
||||
*/
|
||||
public abstract void bindRecyclerViewHolder(ViewHolder viewHolder, int position);
|
||||
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
|
||||
bindRecyclerViewHolder(viewHolder, position);
|
||||
}
|
||||
public abstract void onBindViewHolder(VH viewHolder, final int position);
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
|
|
@ -74,6 +36,11 @@ public abstract class RecyclerSwipeAdapter extends RecyclerView.Adapter<Recycler
|
|||
mItemManger.closeAllExcept(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllItems() {
|
||||
mItemManger.closeAllItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
return mItemManger.getOpenItems();
|
||||
|
|
|
|||
|
|
@ -31,20 +31,20 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
protected BaseAdapter mAdapter;
|
||||
|
||||
public SwipeItemMangerImpl(BaseAdapter adapter) {
|
||||
if(adapter == null)
|
||||
if (adapter == null)
|
||||
throw new IllegalArgumentException("Adapter can not be null");
|
||||
|
||||
if(!(adapter instanceof SwipeItemMangerInterface))
|
||||
if (!(adapter instanceof SwipeItemMangerInterface))
|
||||
throw new IllegalArgumentException("adapter should implement the SwipeAdapterInterface");
|
||||
|
||||
this.mAdapter = adapter;
|
||||
}
|
||||
|
||||
public Attributes.Mode getMode(){
|
||||
public Attributes.Mode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
public void setMode(Attributes.Mode mode){
|
||||
public void setMode(Attributes.Mode mode) {
|
||||
this.mode = mode;
|
||||
mOpenPositions.clear();
|
||||
mShownLayouts.clear();
|
||||
|
|
@ -55,8 +55,8 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
int resId = getSwipeLayoutId(position);
|
||||
|
||||
OnLayoutListener onLayoutListener = new OnLayoutListener(position);
|
||||
SwipeLayout swipeLayout = (SwipeLayout)target.findViewById(resId);
|
||||
if(swipeLayout == null)
|
||||
SwipeLayout swipeLayout = (SwipeLayout) target.findViewById(resId);
|
||||
if (swipeLayout == null)
|
||||
throw new IllegalStateException("can not find SwipeLayout in target view");
|
||||
|
||||
SwipeMemory swipeMemory = new SwipeMemory(position);
|
||||
|
|
@ -70,26 +70,26 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
public void updateConvertView(View target, int position) {
|
||||
int resId = getSwipeLayoutId(position);
|
||||
|
||||
SwipeLayout swipeLayout = (SwipeLayout)target.findViewById(resId);
|
||||
if(swipeLayout == null)
|
||||
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 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);
|
||||
private int getSwipeLayoutId(int position) {
|
||||
return ((SwipeAdapterInterface) (mAdapter)).getSwipeLayoutResourceId(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openItem(int position) {
|
||||
if(mode == Attributes.Mode.Multiple){
|
||||
if(!mOpenPositions.contains(position))
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
if (!mOpenPositions.contains(position))
|
||||
mOpenPositions.add(position);
|
||||
}else{
|
||||
} else {
|
||||
mOpenPosition = position;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
|
|
@ -97,10 +97,10 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
|
||||
@Override
|
||||
public void closeItem(int position) {
|
||||
if(mode == Attributes.Mode.Multiple){
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
mOpenPositions.remove(position);
|
||||
}else{
|
||||
if(mOpenPosition == position)
|
||||
} else {
|
||||
if (mOpenPosition == position)
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
|
|
@ -108,12 +108,19 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
|
||||
@Override
|
||||
public void closeAllExcept(SwipeLayout layout) {
|
||||
for(SwipeLayout s : mShownLayouts){
|
||||
if(s != layout)
|
||||
for (SwipeLayout s : mShownLayouts) {
|
||||
if (s != layout)
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeAllItems() {
|
||||
for (SwipeLayout s : mShownLayouts) {
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeShownLayouts(SwipeLayout layout) {
|
||||
mShownLayouts.remove(layout);
|
||||
|
|
@ -121,9 +128,9 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
|
||||
@Override
|
||||
public List<Integer> getOpenItems() {
|
||||
if(mode == Attributes.Mode.Multiple){
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
return new ArrayList<Integer>(mOpenPositions);
|
||||
}else{
|
||||
} else {
|
||||
return Arrays.asList(mOpenPosition);
|
||||
}
|
||||
}
|
||||
|
|
@ -135,9 +142,9 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
|
||||
@Override
|
||||
public boolean isOpen(int position) {
|
||||
if(mode == Attributes.Mode.Multiple){
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
return mOpenPositions.contains(position);
|
||||
}else{
|
||||
} else {
|
||||
return mOpenPosition == position;
|
||||
}
|
||||
}
|
||||
|
|
@ -154,7 +161,7 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
}
|
||||
}
|
||||
|
||||
class OnLayoutListener implements SwipeLayout.OnLayout{
|
||||
class OnLayoutListener implements SwipeLayout.OnLayout {
|
||||
|
||||
private int position;
|
||||
|
||||
|
|
@ -162,15 +169,15 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
this.position = position;
|
||||
}
|
||||
|
||||
public void setPosition(int position){
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLayout(SwipeLayout v) {
|
||||
if(isOpen(position)){
|
||||
if (isOpen(position)) {
|
||||
v.open(false, false);
|
||||
}else{
|
||||
} else {
|
||||
v.close(false, false);
|
||||
}
|
||||
}
|
||||
|
|
@ -187,16 +194,17 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
|
||||
@Override
|
||||
public void onClose(SwipeLayout layout) {
|
||||
if(mode == Attributes.Mode.Multiple){
|
||||
if (mode == Attributes.Mode.Multiple) {
|
||||
mOpenPositions.remove(position);
|
||||
}else{
|
||||
} else {
|
||||
mOpenPosition = INVALID_POSITION;
|
||||
}
|
||||
mShownLayouts.remove(layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartOpen(SwipeLayout layout) {
|
||||
if(mode == Attributes.Mode.Single) {
|
||||
if (mode == Attributes.Mode.Single) {
|
||||
closeAllExcept(layout);
|
||||
}
|
||||
}
|
||||
|
|
@ -209,9 +217,10 @@ public class SwipeItemMangerImpl implements SwipeItemMangerInterface {
|
|||
closeAllExcept(layout);
|
||||
mOpenPosition = position;
|
||||
}
|
||||
mShownLayouts.add(layout);
|
||||
}
|
||||
|
||||
public void setPosition(int position){
|
||||
public void setPosition(int position) {
|
||||
this.position = position;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,6 +107,18 @@ public class SwipeItemRecyclerMangerImpl implements SwipeItemMangerInterface {
|
|||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ public interface SwipeItemMangerInterface {
|
|||
public void closeItem(int position);
|
||||
|
||||
public void closeAllExcept(SwipeLayout layout);
|
||||
|
||||
public void closeAllItems();
|
||||
|
||||
public List<Integer> getOpenItems();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue