Wednesday, 15 May 2019

How to add dividers, spacers, or header with text to a recylerview

I've got a RecylerView that is displaying a list of data. I need to add dividers or spacers that I need to add two Dividers or spacers too that I can add text to as shown in the image below(with the text DEFAULT or OTHER).

*NOTE Headers are in the image below with the text "DEFAULT" and another that says "OTHER".

The CC's shown are drag and drop items but i don't want the "spaces or headers" marked as default and other to be moved.

enter image description here

I've started adding the divider as shown in the code below but I cannot figure out how to add text.

    @Override
   public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
       super.onViewCreated(view, savedInstanceState);

       ButterKnife.bind(this, view);

       LinearLayoutManager mAdapterLayoutManager = new LinearLayoutManager(context);
       recyclerView.setLayoutManager(mAdapterLayoutManager);
       billingMethodRecyclerViewAdapter = new BillingMethodRecyclerViewAdapter(context, billingMethods, paymentService, ListPaymentMethods.this );
       billingMethodRecyclerViewAdapter.setMasterpassService(masterpassService);
       billingMethodRecyclerViewAdapter.setChoosePaymentMode(choosePaymentMode);
       DividerItemDecoration itemDecor = new DividerItemDecoration(context, mAdapterLayoutManager.getOrientation());

       recyclerView.addItemDecoration(itemDecor);
       recyclerView.setAdapter(billingMethodRecyclerViewAdapter);
       billingMethodRecyclerViewAdapter.clear();

       if (!choosePaymentMode) {
           setupSwipeListener(billingMethodRecyclerViewAdapter);
       }

       if (!BuildConfig.SHOW_AVAILABILITY_INRIX) {
           parkmobileProAd.setVisibility(View.GONE);
       }

       if (getArguments() != null) {
           isAddPaymentZoneDetails = getArguments().getBoolean(AddPaymentActivity.ADD_PAYMENT_FROM_ZONE_DETAIL);
       }
   }

and the xml for the recylcerview

<androidx.recyclerview.widget.RecyclerView
           android:id="@+id/recycler_view"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>

EDIT

Adapter:

public class BillingMethodRecyclerViewAdapter extends RecyclerView.Adapter<BillingMethodHolder> {

                private static final int PENDING_REMOVAL_TIMEOUT = 3000; // in milliseconds

                private ArrayList<BillingMethod> billingMethods;
                private Handler handler = new Handler(); // handler for running delayed runnables
                private HashMap<BillingMethod, Runnable> pendingRunnables = new HashMap<>(); // map of items to pending runnables, so we can cancel a removal if need be

                private BillingMethodHolder.BillingMethodListener billingMethodListener;
                private PaymentService paymentService;
                private MasterpassService masterpassService;

                private boolean choosePaymentMode = false;
                private Context mContext;

                private int maxPaymentMethods = 6;

                public BillingMethodRecyclerViewAdapter(Context context, ArrayList<BillingMethod> objects, PaymentService paymentService, BillingMethodHolder.BillingMethodListener billingMethodListener) {
                    this.billingMethods = objects;
                    this.billingMethodListener = billingMethodListener;
                    this.mContext = context.getApplicationContext();
                    this.paymentService = paymentService;
                }

                public int getMaxPaymentMethods() {
                    return maxPaymentMethods;
                }

                public void setMaxPaymentMethods(int maxPaymentMethods) {
                    this.maxPaymentMethods = maxPaymentMethods;
                }

                public void setMasterpassService(MasterpassService masterpassService) {
                    this.masterpassService = masterpassService;
                }

                public boolean onItemMove(int fromPosition, int toPosition) {
                    if (fromPosition < toPosition) {
                        for (int i = fromPosition; i < toPosition; i++) {
                            Collections.swap(billingMethods, i, i + 1);
                        }
                    } else {
                        for (int i = fromPosition; i > toPosition; i--) {
                            Collections.swap(billingMethods, i, i - 1);
                        }
                    }
                    notifyItemMoved(fromPosition, toPosition);
                    return true;
                }


                public List<BillingMethod> getBillingMethods() {
                    return billingMethods;
                }

                public void setChoosePaymentMode(boolean choosePaymentMode) {
                    this.choosePaymentMode = choosePaymentMode;
                }

                @NonNull
                @Override
                public BillingMethodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_payment_methods, parent, false);
                    return new BillingMethodHolder(v);
                }

                @Override
                public void onBindViewHolder(@NonNull BillingMethodHolder holder, final int position) {
                    holder.bind(getBillingMethods().get(position), billingMethodListener, choosePaymentMode);
                }


            @Override
            public int getItemCount() {
                    return billingMethods.size();
            }

            public void pendingRemoval(int position) {

                final BillingMethod billingMethod = billingMethods.get(position);
                    billingMethod.setPendingDeletion(true);
                    this.notifyItemChanged(position); // redraw row in "undo" state

                    Runnable pendingRemovalRunnable = () -> {
                        deleteBillingMethod(position);
                    };
                    handler.postDelayed(pendingRemovalRunnable, PENDING_REMOVAL_TIMEOUT);
                    pendingRunnables.put(billingMethod, pendingRemovalRunnable);

            }

            public void savePaymentOrder() {

                List<BillingMethod> newOrder = new ArrayList<>();
                int order = 0;

                for (BillingMethod method : billingMethods) {
                    if (order == 0) {
                        method.setPreferred(true);
                        LocalyticsUtil.getInstance(mContext).setPrimaryPaymentMethod(method);
                    } else {
                        method.setPreferred(false);
                    }
                    method.setSortOrder(order);
                    newOrder.add(method);
                    order++;
                }

                paymentService.setPaymentMethodsOrder(newOrder, new PaymentService.GenericListener() {
                    @Override
                    public void onSuccess() {
                        Log.d("TAG", "Saved");
                    }

                    @Override
                    public void onError(String errorMessage) {
                        Log.d("TAG", "Saved");
                    }
                });
            }

            private void deleteBillingMethod(int position) {

                BillingMethod method = billingMethods.get(position);

                if (method.getBillingType() == BillingType.CREDIT_CARD) {
                    paymentService.deleteCreditCard(method.getCreditCard().getCardStatus(), new PaymentService.GenericListener() {
                        @Override
                        public void onSuccess() {
                            billingMethods.remove(position);
                            notifyItemRemoved(position);
                        }

                        @Override
                        public void onError(String errorMessage) {
                        }
                    });
                } else if (method.getBillingType() == BillingType.PREPAID) {
                    paymentService.deleteWallet(new PaymentService.GenericListener() {
                        @Override
                        public void onSuccess() {
                            billingMethods.remove(position);
                            notifyItemRemoved(position);

                        }

                        @Override
                        public void onError(String errorMessage) {
                        }
                    });
                } else if (method.getBillingType() == BillingType.PAYPAL) {
                    paymentService.deletePaypal(new PaymentService.GenericListener() {
                        @Override
                        public void onSuccess() {
                            billingMethods.remove(position);
                            notifyItemRemoved(position);
                        }

                        @Override
                        public void onError(String errorMessage) {

                        }
                    });

                } else if (method.getBillingType() == BillingType.CHASEPAY) {
                    paymentService.deleteChasepay(new PaymentService.GenericListener() {
                        @Override
                        public void onSuccess() {
                            billingMethods.remove(position);
                            notifyItemRemoved(position);
                        }

                        @Override
                        public void onError(String errorMessage) {
                        }
                    });
                } else if (method.getBillingType() == BillingType.MASTERPASSV7) {
                    masterpassService.deleteMasterpassV7(String.valueOf(method.getBillingMethodId()), new MasterpassService.GenericListener() {
                        @Override
                        public void onSuccess() {
                            billingMethods.remove(position);
                            notifyItemRemoved(position);
                        }

                        @Override
                        public void onError(String errorMessage) {

                        }
                    });

                } else {
                        notifyDataSetChanged();
                }

            }

            public void clear() {
                billingMethods.clear();
                notifyDataSetChanged();
            }

            public void stopRemoval(BillingMethod billingMethod) {
                Runnable pendingRemovalRunnable = pendingRunnables.get(billingMethod);
                pendingRunnables.remove(billingMethod);
                if (pendingRemovalRunnable != null) {
                    handler.removeCallbacks(pendingRemovalRunnable);
                }
                billingMethod.setPendingDeletion(false);
                this.notifyItemChanged(billingMethods.indexOf(billingMethod));
            }
        }



from How to add dividers, spacers, or header with text to a recylerview

No comments:

Post a Comment