Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. public class FooFragment extends Fragment {
  2. private RecyclerView mRecyclerView;
  3. private Product[] mProducts1;
  4. private SQLiteDatabaseHelper mDatabase;
  5.  
  6.  
  7. public FooFragment() {
  8. super();
  9. }
  10.  
  11. @Override
  12. public void onCreate(Bundle savedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. mDatabase = SQLiteDatabaseHelper.getsInstance(getContext());
  15. setDataForAdapter();
  16. }
  17.  
  18.  
  19. @Override
  20. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  21. Bundle savedInstanceState) {
  22. final View view = inflater.inflate(R.layout.fragment_foo, container, false);
  23. mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_foo_fragment);
  24. setUpRecyclerView(inflater.getContext());
  25. return inflater.inflate(R.layout.fragment_foo, container, false);
  26. }
  27.  
  28.  
  29. private void setUpRecyclerView(Context context) {
  30. final ProductAdapter adapter = new ProductAdapter(mProducts1, context.getApplicationContext());
  31. LinearLayoutManager manager = new LinearLayoutManager(getContext());
  32. manager.setOrientation(LinearLayoutManager.VERTICAL);
  33. mRecyclerView.setLayoutManager(manager);
  34. mRecyclerView.setAdapter(adapter);
  35. adapter.notifyDataSetChanged();
  36. setAnimationDecorator();
  37. setUpItemTouchHelper(context);
  38. mRecyclerView.post(new Runnable() {
  39. @Override
  40. public void run() {
  41. mRecyclerView.setAdapter(adapter);
  42. adapter.notifyDataSetChanged();
  43. }
  44. });
  45. }
  46.  
  47. private void setDataForAdapter() {
  48. if (mDatabase.getAllProducts().isEmpty()) {
  49. Product[] products2 = Product.products;
  50. for (Product product : products2)
  51. mDatabase.addProduct(product);
  52. }
  53. List<Product> products = mDatabase.getAllProducts();
  54. mProducts1 = new Product[products.size()];
  55. for (Product product : products)
  56. mProducts1[products.indexOf(product)] = product;
  57. }
  58.  
  59. private void setUpItemTouchHelper(final Context context) {
  60. ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
  61.  
  62. Drawable background;
  63. Drawable deleteMark;
  64. int deleteMarkMargin;
  65. boolean isInitialised;
  66.  
  67. private void initialise() {
  68. background = new ColorDrawable(Color.RED);
  69. deleteMark = ContextCompat.getDrawable(context, R.drawable.delete_24dp);
  70. deleteMark.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
  71. deleteMarkMargin = (int) context.getResources().getDimension(R.dimen.delete_margin) + 20;
  72. isInitialised = true;
  73. }
  74.  
  75. @Override
  76. public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
  77. int position = viewHolder.getAdapterPosition();
  78. ProductAdapter productAdapter = (ProductAdapter) recyclerView.getAdapter();
  79. if (productAdapter.isPendingRemoval(position))
  80. return 0;
  81. return super.getSwipeDirs(recyclerView, viewHolder);
  82. }
  83.  
  84.  
  85. @Override
  86. public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isActiveNow) {
  87. View itemView = viewHolder.itemView;
  88.  
  89. if (viewHolder.getAdapterPosition() == -1)
  90. return;
  91.  
  92. if (!isInitialised)
  93. initialise();
  94.  
  95. background.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
  96. background.draw(canvas);
  97.  
  98. int itemHeight = itemView.getBottom() - itemView.getTop();
  99. int intrinsicWidth = deleteMark.getIntrinsicWidth();
  100. int intrinsicHeight = deleteMark.getIntrinsicWidth();
  101.  
  102. int deleteMarkLeft = itemView.getRight() - intrinsicWidth - deleteMarkMargin;
  103. int deleteMarkRight = itemView.getRight() - deleteMarkMargin;
  104. int deleteMarkTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
  105. int deleteMarkBottom = deleteMarkTop + intrinsicHeight;
  106. deleteMark.setBounds(deleteMarkLeft, deleteMarkTop, deleteMarkRight, deleteMarkBottom);
  107. deleteMark.draw(canvas);
  108. super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isActiveNow);
  109. }
  110.  
  111. @Override
  112. public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
  113. return false;
  114. }
  115.  
  116. @Override
  117. public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  118. int position = viewHolder.getAdapterPosition();
  119. ProductAdapter productAdapter = (ProductAdapter) mRecyclerView.getAdapter();
  120. productAdapter.pendingRemoval(position);
  121. }
  122. };
  123. ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(simpleCallback);
  124. mItemTouchHelper.attachToRecyclerView(mRecyclerView);
  125. }
  126.  
  127. private void setAnimationDecorator() {
  128. mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
  129.  
  130. Drawable background;
  131. boolean isInitialised;
  132.  
  133. private void initialise() {
  134. background = new ColorDrawable(Color.RED);
  135. isInitialised = true;
  136. }
  137.  
  138. @Override
  139. public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
  140. if (!isInitialised)
  141. initialise();
  142.  
  143. if (parent.getItemAnimator().isRunning()) {
  144. View firstViewUp = null;
  145. View lastViewDown = null;
  146.  
  147. int left = 0;
  148. int right = parent.getWidth();
  149. int top = 0;
  150. int bottom = 0;
  151.  
  152. int childCount = parent.getLayoutManager().getChildCount();
  153. for (int i = 0; i < childCount; i++) {
  154. View child = parent.getLayoutManager().getChildAt(i);
  155. if (child.getTranslationY() < 0)
  156. lastViewDown = child;
  157. else if (child.getTranslationY() > 0)
  158. if (firstViewUp == null)
  159. firstViewUp = child;
  160. }
  161.  
  162. if (lastViewDown != null && firstViewUp != null) {
  163. top = lastViewDown.getBottom() + (int) lastViewDown.getTranslationY();
  164. bottom = firstViewUp.getTop() + (int) firstViewUp.getTranslationY();
  165. } else if (lastViewDown != null) {
  166. top = lastViewDown.getBottom() + (int) lastViewDown.getTranslationY();
  167. bottom = lastViewDown.getBottom();
  168. } else if (firstViewUp != null) {
  169. top = firstViewUp.getTop();
  170. bottom = firstViewUp.getTop() + (int) firstViewUp.getTranslationY();
  171. }
  172. background.setBounds(left, top, right, bottom);
  173. background.draw(canvas);
  174. }
  175. super.onDraw(canvas, parent, state);
  176. }
  177. });
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement