15b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar/*
25b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
35b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar *
45b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
55b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * you may not use this file except in compliance with the License.
65b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * You may obtain a copy of the License at
75b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar *
85b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
95b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar *
105b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Unless required by applicable law or agreed to in writing, software
115b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
125b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * See the License for the specific language governing permissions and
145b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * limitations under the License.
155b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar */
165b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
175b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarpackage com.example.android.supportv7.widget.touch;
185b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
195b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.app.Activity;
205b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.graphics.Canvas;
215b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.os.Bundle;
225b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.view.LayoutInflater;
235b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.view.View;
245b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.view.ViewGroup;
255b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.widget.Button;
265b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport android.widget.TextView;
275b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
28def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.annotation.NonNull;
29def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.annotation.Nullable;
30def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.cardview.widget.CardView;
31def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.recyclerview.widget.ItemTouchHelper;
32def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.recyclerview.widget.LinearLayoutManager;
33def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.recyclerview.widget.RecyclerView;
34def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas
358a11e6829c522aa1efcc903afa4c01d337082eabChris Craikimport com.example.android.supportv7.Cheeses;
368a11e6829c522aa1efcc903afa4c01d337082eabChris Craikimport com.example.android.supportv7.R;
378a11e6829c522aa1efcc903afa4c01d337082eabChris Craikimport com.example.android.supportv7.widget.util.ConfigToggle;
388a11e6829c522aa1efcc903afa4c01d337082eabChris Craikimport com.example.android.supportv7.widget.util.ConfigViewHolder;
398a11e6829c522aa1efcc903afa4c01d337082eabChris Craik
405b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport java.util.ArrayList;
415b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport java.util.Arrays;
425b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarimport java.util.List;
435b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
445b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar/**
455b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar * Sample activity from which ItemTouchHelper demo activities inherit.
465b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar */
475b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyarabstract public class ItemTouchHelperActivity extends Activity {
485b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
495b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public RecyclerView mRecyclerView;
505b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
515b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchAdapter mAdapter;
525b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
535b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchHelper mItemTouchHelper;
545b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
555b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchHelper.Callback mCallback;
565b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
575b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    private ConfigToggle[] mConfigToggles;
585b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
595b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    @Override
605b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public void onCreate(Bundle savedInstanceState) {
615b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        super.onCreate(savedInstanceState);
625b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        setContentView(R.layout.activity_item_touch);
63fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mRecyclerView = findViewById(R.id.recycler_view);
645b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mRecyclerView.setHasFixedSize(true);
655b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mAdapter = createAdapter();
665b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mRecyclerView.setAdapter(mAdapter);
675b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
685b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mItemTouchHelper = createItemTouchHelper();
695b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mItemTouchHelper.attachToRecyclerView(mRecyclerView);
705b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        initToggles();
715b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
725b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
735b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    private void initToggles() {
745b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mConfigToggles = createConfigToggles();
75fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        RecyclerView configView = findViewById(R.id.config_recycler_view);
765b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        configView.setAdapter(new RecyclerView.Adapter<ConfigViewHolder>() {
775b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
785b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            public ConfigViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
795b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                return new ConfigViewHolder(LayoutInflater.from(parent.getContext())
805b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                        .inflate(R.layout.config_view_toggle, parent, false));
815b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
825b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
835b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
845b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            public void onBindViewHolder(ConfigViewHolder holder, int position) {
855b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                ConfigToggle toggle = mConfigToggles[position];
865b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                holder.bind(toggle);
875b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
885b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
895b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
905b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            public int getItemCount() {
915b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                return mConfigToggles.length;
925b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
935b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        });
945b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        configView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
955b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                false));
965b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        configView.setHasFixedSize(true);
975b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
985b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
995b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    abstract ConfigToggle[] createConfigToggles();
1005b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1015b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchHelper createItemTouchHelper() {
1025b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        mCallback = createCallback();
1035b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return new ItemTouchHelper(mCallback);
1045b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
1055b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1065b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchHelper.Callback createCallback() {
1075b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return new ItemTouchHelper.Callback() {
1085b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1098a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public int getMovementFlags(@NonNull RecyclerView recyclerView,
1108a11e6829c522aa1efcc903afa4c01d337082eabChris Craik                    @NonNull RecyclerView.ViewHolder viewHolder) {
1115b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                return ItemTouchHelperActivity.this.getMovementFlags(recyclerView, viewHolder);
1125b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1135b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1145b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1158a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public boolean onMove(@NonNull RecyclerView recyclerView,
1168a11e6829c522aa1efcc903afa4c01d337082eabChris Craik                    @NonNull RecyclerView.ViewHolder viewHolder,
1178a11e6829c522aa1efcc903afa4c01d337082eabChris Craik                    @NonNull RecyclerView.ViewHolder target) {
1185b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                mAdapter.move(viewHolder.getAdapterPosition(), target.getAdapterPosition());
1195b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                return true;
1205b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1215b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1225b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1238a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
1245b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                mAdapter.delete(viewHolder.getAdapterPosition());
1255b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1265b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1275b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1288a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder,
1298a11e6829c522aa1efcc903afa4c01d337082eabChris Craik                    int actionState) {
1305b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                super.onSelectedChanged(viewHolder, actionState);
1315b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                ItemTouchHelperActivity.this.onSelectedChanged(viewHolder, actionState);
1325b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1335b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1345b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1358a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
1368a11e6829c522aa1efcc903afa4c01d337082eabChris Craik                    @NonNull RecyclerView.ViewHolder viewHolder,
1375b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                    float dX, float dY, int actionState, boolean isCurrentlyActive) {
1385b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                if (ItemTouchHelperActivity.this.onChildDraw(c, recyclerView, viewHolder,
1395b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                        dX, dY, actionState, isCurrentlyActive)) {
1405b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                    return;
1415b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                }
1425b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState,
1435b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                        isCurrentlyActive);
1445b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1455b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1465b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1478a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView,
1485b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                    RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
1495b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                    boolean isCurrentlyActive) {
1505b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                if (ItemTouchHelperActivity.this.onChildDrawOver(c, recyclerView, viewHolder,
1515b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                        dX, dY, actionState, isCurrentlyActive)) {
1525b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                    return;
1535b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                }
1545b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState,
1555b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                        isCurrentlyActive);
1565b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1575b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1585b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1595b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            public boolean isLongPressDragEnabled() {
1605b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                return ItemTouchHelperActivity.this.isLongPressDragEnabled();
1615b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1625b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1635b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1645b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            public boolean isItemViewSwipeEnabled() {
1655b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                return ItemTouchHelperActivity.this.isPointerSwipeEnabled();
1665b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1675b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1685b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            @Override
1698a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public void clearView(@NonNull RecyclerView recyclerView,
1708a11e6829c522aa1efcc903afa4c01d337082eabChris Craik                    @NonNull RecyclerView.ViewHolder viewHolder) {
1715b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                super.clearView(recyclerView, viewHolder);
1725b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                ItemTouchHelperActivity.this.clearView(viewHolder);
1735b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            }
1745b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        };
1755b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
1765b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1775b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    /**
1785b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar     * @return True if we should NOT call parent
1795b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar     */
1805b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public boolean onChildDraw(Canvas c, RecyclerView recyclerView,
1815b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            RecyclerView.ViewHolder viewHolder,
1825b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            float dX, float dY, int actionState, boolean isCurrentlyActive) {
1835b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return false;
1845b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
1855b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1865b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    /**
1875b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar     * @return True if we should NOT call parent
1885b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar     */
1895b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public boolean onChildDrawOver(Canvas c, RecyclerView recyclerView,
1905b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState,
1915b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            boolean isCurrentlyActive) {
1925b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return false;
1935b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
1945b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1955b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public void clearView(RecyclerView.ViewHolder viewHolder) {
1965b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1975b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
1985b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
1995b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
2005b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2015b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2025b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2035b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public boolean isLongPressDragEnabled() {
2045b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return true;
2055b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2065b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2075b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public boolean isPointerSwipeEnabled() {
2085b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return true;
2095b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2105b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2115b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchViewHolder onCreateViewHolder(ViewGroup parent) {
2128a11e6829c522aa1efcc903afa4c01d337082eabChris Craik        return new ItemTouchViewHolder(
2135b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                LayoutInflater.from(parent.getContext())
2145b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar                        .inflate(R.layout.touch_item, parent, false));
2155b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2165b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2175b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    abstract public int getMovementFlags(RecyclerView recyclerView,
2185b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            RecyclerView.ViewHolder viewHolder);
2195b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2205b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public ItemTouchAdapter createAdapter() {
2215b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        return new ItemTouchAdapter();
2225b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2235b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2245b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public void onBind(ItemTouchViewHolder viewHolder) {
2255b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2265b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2275b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2285b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public void onCreateViewHolder(ItemTouchViewHolder viewHolder) {
2295b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2305b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2315b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2325b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public class ItemTouchViewHolder extends RecyclerView.ViewHolder {
2335b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2345b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public final TextView textView;
2355b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2365b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public final Button actionButton;
2375b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2385b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public final CardView cardView;
2395b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2405b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public final View overlay;
2415b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2425b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public ItemTouchViewHolder(View itemView) {
2435b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            super(itemView);
2445b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            cardView = (CardView) itemView;
2455b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            textView = (TextView) itemView.findViewById(R.id.text_view);
2465b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            actionButton = (Button) itemView.findViewById(R.id.action_button);
2475b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            overlay = itemView.findViewById(R.id.overlay);
2485b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2495b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2505b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2515b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    public class ItemTouchAdapter extends RecyclerView.Adapter<ItemTouchViewHolder> {
2525b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2535b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        private List<String> mItems = new ArrayList<String>();
2545b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2555b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public ItemTouchAdapter() {
2565b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            mItems.addAll(Arrays.asList(Cheeses.sCheeseStrings));
2575b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2585b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2595b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        @Override
2605b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public ItemTouchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
2615b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            return ItemTouchHelperActivity.this.onCreateViewHolder(parent);
2625b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2635b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2645b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        @Override
2655b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public void onBindViewHolder(ItemTouchViewHolder holder, int position) {
2665b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            holder.textView.setText(mItems.get(position));
2675b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            onBind(holder);
2685b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2695b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2705b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public void delete(int position) {
2715b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            mItems.remove(position);
2725b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            notifyItemRemoved(position);
2735b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2745b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2755b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public void move(int from, int to) {
2765b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            String prev = mItems.remove(from);
2775b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            mItems.add(to > from ? to - 1 : to, prev);
2785b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            notifyItemMoved(from, to);
2795b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2805b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2815b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        @Override
2825b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        public int getItemCount() {
2835b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar            return mItems.size();
2845b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar        }
2855b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar    }
2865b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2875b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar
2885b611d45d8d84bd604e6ce855f9520499ff89b68Yigit Boyar}
289