Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FooFragment extends Fragment {
- private RecyclerView mRecyclerView;
- private Product[] mProducts1;
- private SQLiteDatabaseHelper mDatabase;
- public FooFragment() {
- super();
- }
- @Override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- mDatabase = SQLiteDatabaseHelper.getsInstance(getContext());
- setDataForAdapter();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- final View view = inflater.inflate(R.layout.fragment_foo, container, false);
- mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_foo_fragment);
- setUpRecyclerView(inflater.getContext());
- return inflater.inflate(R.layout.fragment_foo, container, false);
- }
- private void setUpRecyclerView(Context context) {
- final ProductAdapter adapter = new ProductAdapter(mProducts1, context.getApplicationContext());
- LinearLayoutManager manager = new LinearLayoutManager(getContext());
- manager.setOrientation(LinearLayoutManager.VERTICAL);
- mRecyclerView.setLayoutManager(manager);
- mRecyclerView.setAdapter(adapter);
- adapter.notifyDataSetChanged();
- setAnimationDecorator();
- setUpItemTouchHelper(context);
- mRecyclerView.post(new Runnable() {
- @Override
- public void run() {
- mRecyclerView.setAdapter(adapter);
- adapter.notifyDataSetChanged();
- }
- });
- }
- private void setDataForAdapter() {
- if (mDatabase.getAllProducts().isEmpty()) {
- Product[] products2 = Product.products;
- for (Product product : products2)
- mDatabase.addProduct(product);
- }
- List<Product> products = mDatabase.getAllProducts();
- mProducts1 = new Product[products.size()];
- for (Product product : products)
- mProducts1[products.indexOf(product)] = product;
- }
- private void setUpItemTouchHelper(final Context context) {
- ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
- Drawable background;
- Drawable deleteMark;
- int deleteMarkMargin;
- boolean isInitialised;
- private void initialise() {
- background = new ColorDrawable(Color.RED);
- deleteMark = ContextCompat.getDrawable(context, R.drawable.delete_24dp);
- deleteMark.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
- deleteMarkMargin = (int) context.getResources().getDimension(R.dimen.delete_margin) + 20;
- isInitialised = true;
- }
- @Override
- public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
- int position = viewHolder.getAdapterPosition();
- ProductAdapter productAdapter = (ProductAdapter) recyclerView.getAdapter();
- if (productAdapter.isPendingRemoval(position))
- return 0;
- return super.getSwipeDirs(recyclerView, viewHolder);
- }
- @Override
- public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isActiveNow) {
- View itemView = viewHolder.itemView;
- if (viewHolder.getAdapterPosition() == -1)
- return;
- if (!isInitialised)
- initialise();
- background.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
- background.draw(canvas);
- int itemHeight = itemView.getBottom() - itemView.getTop();
- int intrinsicWidth = deleteMark.getIntrinsicWidth();
- int intrinsicHeight = deleteMark.getIntrinsicWidth();
- int deleteMarkLeft = itemView.getRight() - intrinsicWidth - deleteMarkMargin;
- int deleteMarkRight = itemView.getRight() - deleteMarkMargin;
- int deleteMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
- int deleteMarkBottom = deleteMarkTop + intrinsicHeight;
- deleteMark.setBounds(deleteMarkLeft, deleteMarkTop, deleteMarkRight, deleteMarkBottom);
- deleteMark.draw(canvas);
- super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isActiveNow);
- }
- @Override
- public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
- return false;
- }
- @Override
- public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
- int position = viewHolder.getAdapterPosition();
- ProductAdapter productAdapter = (ProductAdapter) mRecyclerView.getAdapter();
- productAdapter.pendingRemoval(position);
- }
- };
- ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleCallback);
- mItemTouchHelper.attachToRecyclerView(mRecyclerView);
- }
- private void setAnimationDecorator() {
- mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
- Drawable background;
- boolean isInitialised;
- private void initialise() {
- background = new ColorDrawable(Color.RED);
- isInitialised = true;
- }
- @Override
- public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
- if (!isInitialised)
- initialise();
- if (parent.getItemAnimator().isRunning()) {
- View firstViewUp = null;
- View lastViewDown = null;
- int left = 0;
- int right = parent.getWidth();
- int top = 0;
- int bottom = 0;
- int childCount = parent.getLayoutManager().getChildCount();
- for (int i = 0; i < childCount; i++) {
- View child = parent.getLayoutManager().getChildAt(i);
- if (child.getTranslationY() < 0)
- lastViewDown = child;
- else if (child.getTranslationY() > 0)
- if (firstViewUp == null)
- firstViewUp = child;
- }
- if (lastViewDown != null && firstViewUp != null) {
- top = lastViewDown.getBottom() + (int) lastViewDown.getTranslationY();
- bottom = firstViewUp.getTop() + (int) firstViewUp.getTranslationY();
- } else if (lastViewDown != null) {
- top = lastViewDown.getBottom() + (int) lastViewDown.getTranslationY();
- bottom = lastViewDown.getBottom();
- } else if (firstViewUp != null) {
- top = firstViewUp.getTop();
- bottom = firstViewUp.getTop() + (int) firstViewUp.getTranslationY();
- }
- background.setBounds(left, top, right, bottom);
- background.draw(canvas);
- }
- super.onDraw(canvas, parent, state);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement