|
| 1 | +package co.paulburke.android.itemtouchhelperdemo; |
| 2 | + |
| 3 | +import android.graphics.Color; |
| 4 | +import android.support.v7.widget.RecyclerView; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.TextView; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperAdapter; |
| 15 | +import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperViewHolder; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Paul Burke (ipaulpro) |
| 19 | + */ |
| 20 | +public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder> |
| 21 | + implements ItemTouchHelperAdapter { |
| 22 | + |
| 23 | + private static final String[] STRINGS = new String[]{ |
| 24 | + "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" |
| 25 | + }; |
| 26 | + |
| 27 | + private final List<String> mItems = new ArrayList<>(); |
| 28 | + |
| 29 | + public RecyclerListAdapter() { |
| 30 | + mItems.addAll(Arrays.asList(STRINGS)); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 35 | + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false); |
| 36 | + ItemViewHolder itemViewHolder = new ItemViewHolder(view); |
| 37 | + return itemViewHolder; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onBindViewHolder(final ItemViewHolder holder, int position) { |
| 42 | + holder.textView.setText(mItems.get(position)); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onItemDismiss(int position) { |
| 47 | + mItems.remove(position); |
| 48 | + notifyItemRemoved(position); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onItemMove(int fromPosition, int toPosition) { |
| 53 | + String prev = mItems.remove(fromPosition); |
| 54 | + mItems.add(toPosition > fromPosition ? toPosition - 1 : toPosition, prev); |
| 55 | + notifyItemMoved(fromPosition, toPosition); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public int getItemCount() { |
| 60 | + return mItems.size(); |
| 61 | + } |
| 62 | + |
| 63 | + public static class ItemViewHolder extends RecyclerView.ViewHolder implements |
| 64 | + ItemTouchHelperViewHolder { |
| 65 | + |
| 66 | + public final TextView textView; |
| 67 | + |
| 68 | + public ItemViewHolder(View itemView) { |
| 69 | + super(itemView); |
| 70 | + textView = (TextView) itemView; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onItemSelected() { |
| 75 | + itemView.setBackgroundColor(Color.LTGRAY); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onItemClear() { |
| 80 | + itemView.setBackgroundColor(0); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments