繁体   English   中英

在RecyclerView Android Java中分页

[英]Paging in RecyclerView Android Java

目前有一个我在其中显示的列表视图:

动作

很好 问题是现在我想添加分页,所以我显示10个“动作”,然后用箭头或其他内容链接到下一个10个“动作”。

这是我的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);
    }*/
}

在此处输入图片说明

在这里,我将复制动作的类别:

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

从现在开始,任何帮助都将受到欢迎。

您可以尝试在当前列表的末尾在列表中添加另一个数据模型。 将此模型的视图类型设置为按钮。 单击此按钮后,您可以使用查询中的偏移量和限制从服务器获取下一组移动。

通过扩展或实现父类型,使泛型类型的列表为Parcelable,并使列表中的所有元素成为子元素。

例如,列表可以是A类类型

ArrayList<A>

并且其中的元素可以是类型

Class B extends A

Class C extends A

现在在您的getView中,使用operator的实例检查数据的类型并填充正确的视图布局

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

对于上述加载更多项按钮的每次单击,都使用offset作为列表的当前长度减去一,并且限制始终为10。对于第一个查询,将offset作为0使用,否则sql将引发异常,其余后续查询的偏移量=列表大小减去一(减去一,因为列表的最后一个元素是“加载更多”按钮,而不是实际数据)

例如在mysql中,这可能是:

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

如果没有更多数据,只需更新负载模型,说没有更多项目并禁用按钮即可。 如果找到更多项目,请将它们插入最后一个元素(即“加载更多”按钮)上方的位置,并通知列表适配器。

您将必须找到具有多个视图类型和视图持有者的此类异构列表视图的教程。 这是如何做的一个总体思路,我本人将此方法与具有类似逻辑的recyclerview一起使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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