Skip to content

Commit 1f75c11

Browse files
committed
(recyclerview.adapter) Update adapter. See details ->
RecyclerViewAdapter in the demo now extends from RecyclerSwipeAdapter from the library. This greatly simplifies things for the users. They still must implement their own ViewHolder and extend from the abstract one inside the SwipeAdapter. See the demo for recyclerview for a better understanding of how it works. Also increased the build and compile versions to 21. Added elevation to the top view for 5.0 devices. See the recyclerview on a device running android 5.0+ to see the difference from the edge. This can be replicated with a shadow drawable gradient with lower devices. Maybe someone else can add this feature.
1 parent a5a2e2c commit 1f75c11

File tree

6 files changed

+262
-7
lines changed

6 files changed

+262
-7
lines changed

demo/src/main/java/com/daimajia/swipedemo/RecyclerViewExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.widget.Toast;
1717

1818
import com.daimajia.swipe.util.Attributes;
19+
import com.daimajia.swipedemo.adapter.RecyclerViewAdapter;
1920
import com.daimajia.swipedemo.adapter.RecyclerViewAdvancedAdapter;
2021

2122
import org.lucasr.twowayview.ItemClickSupport;
@@ -61,8 +62,8 @@ protected void onCreate(Bundle savedInstanceState) {
6162
recyclerView.addItemDecoration(new DividerItemDecoration(divider));
6263

6364
mDataSet = new ArrayList<String>(Arrays.asList(adapterData));
64-
mAdapter = new RecyclerViewAdvancedAdapter(this, mDataSet);
65-
((RecyclerViewAdvancedAdapter) mAdapter).setMode(Attributes.Mode.Single);
65+
mAdapter = new RecyclerViewAdapter(this, mDataSet);
66+
((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
6667
recyclerView.setAdapter(mAdapter);
6768

6869
/* Listeners */
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.daimajia.swipedemo.adapter;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.TextView;
8+
import android.widget.Toast;
9+
10+
import com.daimajia.androidanimations.library.Techniques;
11+
import com.daimajia.androidanimations.library.YoYo;
12+
import com.daimajia.swipe.SimpleSwipeListener;
13+
import com.daimajia.swipe.SwipeLayout;
14+
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;
15+
import com.daimajia.swipe.implments.SwipeItemRecyclerMangerImpl;
16+
import com.daimajia.swipedemo.R;
17+
18+
import java.util.ArrayList;
19+
20+
public class RecyclerViewAdapter extends RecyclerSwipeAdapter {
21+
22+
public static class SimpleViewHolder extends ViewHolder {
23+
SwipeLayout swipeLayout;
24+
TextView textViewPos;
25+
TextView textViewData;
26+
27+
public SimpleViewHolder(View itemView) {
28+
super(itemView);
29+
swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
30+
textViewPos = (TextView) itemView.findViewById(R.id.position);
31+
textViewData = (TextView) itemView.findViewById(R.id.text_data);
32+
}
33+
}
34+
35+
private Context mContext;
36+
private ArrayList<String> mDataset;
37+
38+
protected SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
39+
40+
public RecyclerViewAdapter(Context context, ArrayList<String> objects) {
41+
super(context, objects);
42+
this.mContext = context;
43+
this.mDataset = objects;
44+
}
45+
46+
@Override
47+
public ViewHolder createRecyclerViewHolder(ViewGroup parent, int viewType) {
48+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
49+
return new SimpleViewHolder(view);
50+
}
51+
52+
@Override
53+
public void bindRecyclerViewHolder(ViewHolder viewHolder, int position) {
54+
SimpleViewHolder simpleViewHolder = (SimpleViewHolder) viewHolder;
55+
String item = mDataset.get(position);
56+
simpleViewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
57+
simpleViewHolder.swipeLayout.addSwipeListener(new SimpleSwipeListener() {
58+
@Override
59+
public void onOpen(SwipeLayout layout) {
60+
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
61+
}
62+
});
63+
simpleViewHolder.swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
64+
@Override
65+
public void onDoubleClick(SwipeLayout layout, boolean surface) {
66+
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
67+
}
68+
});
69+
simpleViewHolder.textViewPos.setText((position + 1) + ".");
70+
simpleViewHolder.textViewData.setText(item);
71+
mItemManger.bind(viewHolder.itemView, position);
72+
}
73+
74+
@Override
75+
public int getItemCount() {
76+
return mDataset.size();
77+
}
78+
79+
@Override
80+
public int getSwipeLayoutResourceId(int position) {
81+
return R.id.swipe;
82+
}
83+
}

demo/src/main/res/drawable/divider.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
android:shape="rectangle">
1919

2020
<solid android:color="#cccccc"/>
21-
<size android:width="2dp"
22-
android:height="2dp" />
21+
<size android:width="1dp"
22+
android:height="1dp" />
2323

2424
</shape>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto"
7+
android:id="@+id/swipe"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
swipe:horizontalSwipeOffset="0dp">
11+
12+
<LinearLayout
13+
android:layout_width="match_parent"
14+
android:layout_height="80dp"
15+
android:background="#FF5534"
16+
android:gravity="center"
17+
android:tag="Bottom3"
18+
android:weightSum="10">
19+
20+
<ImageView
21+
android:id="@+id/trash"
22+
android:layout_width="27dp"
23+
android:layout_height="30dp"
24+
android:layout_weight="1"
25+
android:src="@drawable/trash" />
26+
27+
<TextView
28+
android:layout_width="0dp"
29+
android:layout_height="wrap_content"
30+
android:layout_weight="5"
31+
android:text="Delete Item?"
32+
android:textColor="#fff"
33+
android:textSize="17sp" />
34+
35+
<Button
36+
android:id="@+id/delete"
37+
android:layout_width="0dp"
38+
android:layout_height="40dp"
39+
android:layout_weight="4"
40+
android:background="#ffffff"
41+
android:text="Yes,Delete"
42+
android:textColor="#FF5534" />
43+
</LinearLayout>
44+
45+
<LinearLayout
46+
android:layout_width="match_parent"
47+
android:layout_height="match_parent"
48+
android:background="@drawable/item_selector"
49+
android:elevation="5dp"
50+
android:padding="10dp">
51+
52+
<TextView
53+
android:id="@+id/position"
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content" />
56+
57+
<TextView
58+
android:id="@+id/text_data"
59+
android:layout_width="match_parent"
60+
android:layout_height="match_parent"
61+
android:tag="Hover"
62+
android:text="Do not, for one repulse, forgo the purpose that you resolved to effort. " />
63+
</LinearLayout>
64+
</com.daimajia.swipe.SwipeLayout>
65+
</LinearLayout>

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ VERSION_CODE=18
2323
GROUP=com.daimajia.swipelayout
2424

2525
ANDROID_BUILD_MIN_SDK_VERSION=8
26-
ANDROID_BUILD_TARGET_SDK_VERSION=20
27-
ANDROID_BUILD_SDK_VERSION=20
28-
ANDROID_BUILD_TOOLS_VERSION=20.0.0
26+
ANDROID_BUILD_TARGET_SDK_VERSION=21
27+
ANDROID_BUILD_SDK_VERSION=21
28+
ANDROID_BUILD_TOOLS_VERSION=21.0.0
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.daimajia.swipe.adapters;
2+
3+
import android.content.Context;
4+
import android.support.v7.widget.RecyclerView;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
8+
import com.daimajia.swipe.SwipeLayout;
9+
import com.daimajia.swipe.implments.SwipeItemRecyclerMangerImpl;
10+
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
11+
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;
12+
import com.daimajia.swipe.util.Attributes;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
public abstract class RecyclerSwipeAdapter extends RecyclerView.Adapter<RecyclerSwipeAdapter.ViewHolder> implements SwipeItemMangerInterface,SwipeAdapterInterface {
18+
19+
private SwipeItemRecyclerMangerImpl mItemManger = new SwipeItemRecyclerMangerImpl(this);
20+
21+
/**
22+
* This must be over-ridden
23+
* Containes the view of the swiped item in the recycler.view
24+
*/
25+
public static abstract class ViewHolder extends RecyclerView.ViewHolder {
26+
public ViewHolder(View itemView) {
27+
super(itemView);
28+
}
29+
};
30+
31+
private Context mContext;
32+
private ArrayList mDataset;
33+
34+
public RecyclerSwipeAdapter(Context context, ArrayList objects) {
35+
this.mContext = context;
36+
this.mDataset = objects;
37+
}
38+
39+
/**
40+
* @param parent
41+
* @param viewType
42+
* @return View
43+
*/
44+
public abstract ViewHolder createRecyclerViewHolder(ViewGroup parent, int viewType);
45+
46+
@Override
47+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
48+
return createRecyclerViewHolder(parent, viewType);
49+
}
50+
51+
/**
52+
* @param viewHolder
53+
* @param position
54+
*/
55+
public abstract void bindRecyclerViewHolder(ViewHolder viewHolder, int position);
56+
57+
@Override
58+
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
59+
bindRecyclerViewHolder(viewHolder, position);
60+
}
61+
62+
@Override
63+
public void openItem(int position) {
64+
mItemManger.openItem(position);
65+
}
66+
67+
@Override
68+
public void closeItem(int position) {
69+
mItemManger.closeItem(position);
70+
}
71+
72+
@Override
73+
public void closeAllExcept(SwipeLayout layout) {
74+
mItemManger.closeAllExcept(layout);
75+
}
76+
77+
@Override
78+
public List<Integer> getOpenItems() {
79+
return mItemManger.getOpenItems();
80+
}
81+
82+
@Override
83+
public List<SwipeLayout> getOpenLayouts() {
84+
return mItemManger.getOpenLayouts();
85+
}
86+
87+
@Override
88+
public void removeShownLayouts(SwipeLayout layout) {
89+
mItemManger.removeShownLayouts(layout);
90+
}
91+
92+
@Override
93+
public boolean isOpen(int position) {
94+
return mItemManger.isOpen(position);
95+
}
96+
97+
@Override
98+
public Attributes.Mode getMode() {
99+
return mItemManger.getMode();
100+
}
101+
102+
@Override
103+
public void setMode(Attributes.Mode mode) {
104+
mItemManger.setMode(mode);
105+
}
106+
}

0 commit comments

Comments
 (0)