Paginacion in RecyclerView Android Java

0

I find the following RecyclerView

         RecyclerView lstMovsWallet = (RecyclerView) findViewById(R.id.lstMovsWallet);
         lstMovsWallet.setLayoutManager(new 
         LinearLayoutManager(MovsMobileWallet.this));
         AdapterCobrosPendientesListado adapter = new 
         AdapterCobrosPendientesListado(MovsMobileWallet.this, items);
         lstMovsWallet.setAdapter(adapter);

My Adapter class

    public class AdapterCobrosPendientesListado extends RecyclerView.Adapter<AdapterCobrosPendientesListado.ViewHolder> {


    private LayoutInflater mInflater;
    protected List<MovimientoCuenta> items;

    public AdapterCobrosPendientesListado(Context context, List<MovimientoCuenta> data) {
        this.mInflater = LayoutInflater.from(context);
        this.items = data;
    }
    @Override
    public AdapterCobrosPendientesListado.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.activity_adapter_billings_listhistory, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AdapterCobrosPendientesListado.ViewHolder holder, int position) {
        DecimalFormat formater = new DecimalFormat("###.00");

        String numero =  items.get(position).getNumber();
        String cantidad =  items.get(position).getMonto();
        String fecha =  items.get(position).getFecha();
        String referencia =  items.get(position).getReferencia();
        String debitoCredito = items.get(position).getDebitoCredito();

        holder.number.setText(numero);
        holder.mount.setText(cantidad);
        holder.date.setText(fecha);
        holder.ref.setText(referencia);


        if(debitoCredito.compareTo("DBT")==0){
            holder.title.setText("Pago");
            holder.auxBilling.setImageResource(R.mipmap.signonegativo);
        }
        else {
            holder.title.setText("Cobro");
            holder.auxBilling.setImageResource(R.mipmap.signomas);
        }

    }

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


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        public TextView number;
        public TextView mount;
        public TextView date;
        public ImageView auxBilling;
        public TextView ref;
        public TextView title ;




        public ViewHolder(View itemView) {
            super(itemView);
             number =  itemView.findViewById(R.id.txtNumberPhoneBilling);
             mount =  itemView.findViewById(R.id.txtMountBillingNotifications);
             date =  itemView.findViewById(R.id.txtDateBillingNotifications);
             auxBilling = itemView.findViewById(R.id.btnCancelBillingNotifications);
             ref =  itemView.findViewById(R.id.txtDateBillingRef);
            title =  itemView.findViewById(R.id.TitleMovs);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
          //  if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

 /*   // convenience method for getting data at click position
    public String getItem(int id) {
        return mData.get(id);
    }

    // allows clicks events to be caught
    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }*/
}

I would like to be able to add a page type something like that even though it is paged and it's enough for me

I leave the Movement Class to replicate if someone needs

> public class MovimientoCuenta {

    private String number;
    private String monto;
    private String moneda;
    private String fecha;
    private String ID;
    private String referencia ;
    private String filtro ;
    private String debitoCredito ;
    private String nombreMov;

    public MovimientoCuenta(String number, String monto, String moneda, String fecha, String ID, String referencia, String filtro, String debitoCredito,String nombreMov) {
        this.number = number;
        this.monto = monto;
        this.moneda = moneda;
        this.fecha = fecha;
        this.ID = ID ;
        this.filtro =filtro;
        this.referencia=referencia;
        this.debitoCredito =debitoCredito;
        this.nombreMov =nombreMov;
    }
    
asked by Bruno Sosa Fast Tag 18.01.2018 в 15:05
source

1 answer

1

What you should do, is to create 2 arrangements of type MovimientoCuenta , one as you already have items that would contain all the elements you need and another for example itemsMostrados which would be the items that you would be showing per page .

Besides, you need to calculate how many items per page you are going to display, something for example like that (for example I assume that you will display 10 items per page)

public int numPaginas(int numItems){
    int numPaginas = numItems/10;
    numItems-=numPaginas*10;
    if(numItems!=0){
        numPaginas++;
    }
    return numPaginas;
}

You would also have to change the value of items that you are displaying, in this case, the size of displayed items

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

Finally, you need a variable that tells you which page you are in and you would need a public method on your adapter so that when you change the page, update the items that are in movimientosMostrados

public void setPaginaActual(int paginaActual){
    this.paginaActual = paginaActual;
    itemsMostrados.clear();
    for(int i=0;i<10;i++){
        if(i+(paginaActual*10)<items.size()){
            itemsMostrados.add(items.get(i+(paginaActual*10))); 
        }
    }
    notifyDataSetChanged();
}

I do not have a compiler at hand but I think you can get a good idea of what you should implement.

    
answered by 20.01.2018 / 01:25
source