简体   繁体   中英

Paging in RecyclerView Android Java

Currently had a listview where I showed:

moves

That works fine. The problem is that now I would like to add pagination, so I show 10 "moves", and then, with an arrow or something, link to the next 10 "moves".

This is my ListView:

RecyclweView Java:

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

Adapter for de RecyclerView : 

    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);
    }*/
}

在此处输入图片说明

Here I leave the class of movements to replicate:

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;
}

Any help will be welcome from now thanks.

You can try adding another data model in your list at the end of the current list. Have the view type of this model be a button. On clicking this, you can fetch the next set of moves from your server using offset and limit in your query.

Make the list of a generic type say Parcelable and have all the elements in the list be child via either extending or implementing the parent type.

Eg the list can be of type class A

ArrayList<A>

And the elements in it can be of types

Class B extends A

Class C extends A

Now in your getView , check the type of data using instance of operator and inflate the correct view layout

If(yourlist.get(positionpassedingetview) instanceof B){
    //Inflate the view for B item if not already inflated
}else{
    //Inflate view of type C
}

For every click on the above said load more item button, use the offset as the current length of list minus one and the limit will always be 10. For the first ever query, use offset as 0 or the sql will throw an exception, rest subsequent queries will have the offset = list size minus one (minus one because the last element of the list is a load more button and not the actual data)

Eg in mysql, this could be:

Select * from yourtable where yourcondition order by yourorder limit youroffset,10;

If there's no more data, just update the model of the load more saying no more items and disabling the button. If you find more items, insert them at the poisition above the last element which is the load more button and notify the list adapter.

You will have to find a tutorial for such a heterogenous list view with more than one viewtypes and viewholders. This is a general idea of how to do it, i myself use this approach with a recyclerview with a similar logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM