(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.
This commit is contained in:
John Shelley 2014-12-19 10:32:59 -06:00
parent a5a2e2c9ea
commit 1f75c11e6a
6 changed files with 262 additions and 7 deletions

View File

@ -16,6 +16,7 @@ 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 org.lucasr.twowayview.ItemClickSupport;
@ -61,8 +62,8 @@ public class RecyclerViewExample extends Activity {
recyclerView.addItemDecoration(new DividerItemDecoration(divider));
mDataSet = new ArrayList<String>(Arrays.asList(adapterData));
mAdapter = new RecyclerViewAdvancedAdapter(this, mDataSet);
((RecyclerViewAdvancedAdapter) mAdapter).setMode(Attributes.Mode.Single);
mAdapter = new RecyclerViewAdapter(this, mDataSet);
((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
recyclerView.setAdapter(mAdapter);
/* Listeners */

View File

@ -0,0 +1,83 @@
package com.daimajia.swipedemo.adapter;
import android.content.Context;
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.adapters.RecyclerSwipeAdapter;
import com.daimajia.swipe.implments.SwipeItemRecyclerMangerImpl;
import com.daimajia.swipedemo.R;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerSwipeAdapter {
public static class SimpleViewHolder extends ViewHolder {
SwipeLayout swipeLayout;
TextView textViewPos;
TextView textViewData;
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);
}
}
private Context mContext;
private ArrayList<String> mDataset;
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) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void bindRecyclerViewHolder(ViewHolder viewHolder, int position) {
SimpleViewHolder simpleViewHolder = (SimpleViewHolder) viewHolder;
String item = mDataset.get(position);
simpleViewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
simpleViewHolder.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() {
@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);
mItemManger.bind(viewHolder.itemView, position);
}
@Override
public int getItemCount() {
return mDataset.size();
}
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
}

View File

@ -18,7 +18,7 @@
android:shape="rectangle">
<solid android:color="#cccccc"/>
<size android:width="2dp"
android:height="2dp" />
<size android:width="1dp"
android:height="1dp" />
</shape>

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
swipe:horizontalSwipeOffset="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#FF5534"
android:gravity="center"
android:tag="Bottom3"
android:weightSum="10">
<ImageView
android:id="@+id/trash"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="@drawable/trash" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Delete Item?"
android:textColor="#fff"
android:textSize="17sp" />
<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="4"
android:background="#ffffff"
android:text="Yes,Delete"
android:textColor="#FF5534" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_selector"
android:elevation="5dp"
android:padding="10dp">
<TextView
android:id="@+id/position"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
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. " />
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
</LinearLayout>

View File

@ -23,6 +23,6 @@ VERSION_CODE=18
GROUP=com.daimajia.swipelayout
ANDROID_BUILD_MIN_SDK_VERSION=8
ANDROID_BUILD_TARGET_SDK_VERSION=20
ANDROID_BUILD_SDK_VERSION=20
ANDROID_BUILD_TOOLS_VERSION=20.0.0
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.0.0

View File

@ -0,0 +1,106 @@
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;
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 java.util.ArrayList;
import java.util.List;
public abstract class RecyclerSwipeAdapter extends RecyclerView.Adapter<RecyclerSwipeAdapter.ViewHolder> 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);
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return createRecyclerViewHolder(parent, viewType);
}
/**
* @param viewHolder
* @param position
*/
public abstract void bindRecyclerViewHolder(ViewHolder viewHolder, int position);
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
bindRecyclerViewHolder(viewHolder, position);
}
@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);
}
}