Load files while scrolling in a Recycler View with retrofit

0

I am new to the community and I have a problem, I want to implement the Endless Scroll or Infinite Scroll as some call it in a recycler view to load items, since I currently show the 11500 results in recycler view , this same within a fragment .

I forgot to comment that I use Butterknife .

This is my code in fragment :

    private RecyclerView recyclerView;
    private SolicitudesGeneradasAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    private Call<SolicitudesGeneradasList> call;
    private SolicitudesGeneradasService service;

    private Unbinder unbinder;

    @BindView(R.id.swipeSolicitudesGeneradas)
    SwipeRefreshLayout swipeEventosRecientes;
    @BindView(R.id.tvResultados)
    TextView tvResultados;

    public SolicitudesGeneradasFragment() {}

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_solicitudes_generadas, container, false);

        unbinder = ButterKnife.bind(this, view);

        service = API.getApi().create(SolicitudesGeneradasService.class);

        call = service.obtenerSolicitudesGeneradas(API.KEY, getUUID());

        swipeEventosRecientes.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        call.enqueue(new Callback<SolicitudesGeneradasList>() {
            @Override
            public void onResponse(Call<SolicitudesGeneradasList> call, Response<SolicitudesGeneradasList> response) {
                setSolicitudesGeneradas(response.body().getSolicitudes(), container, inflater);
            }

            @Override
            public void onFailure(Call<SolicitudesGeneradasList> call, Throwable t) {
                Toast.makeText(getActivity(), "Al parecer algo salio mal", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    private void setSolicitudesGeneradas(final ArrayList<SolicitudesGeneradas> solicitudes, ViewGroup container, LayoutInflater inflater) {

        recyclerView = (RecyclerView) container.findViewById(R.id.recyclerView);

        adapter = new SolicitudesGeneradasAdapter(solicitudes, R.layout.recycler_view_manttos_correctivos, new SolicitudesGeneradasAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String idSolicitud, int position) {
                Intent intent = new Intent(getActivity(), DetalleSolicitudActivity.class);
                intent.putExtra("idSolicitud", idSolicitud);
                startActivity(intent);
            }
        });

        layoutManager = new LinearLayoutManager(inflater.getContext());

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setAdapter(adapter);

        tvResultados.setText(Integer.toString(solicitudes.size()) + " Resultados");

    }

    @OnClick(R.id.tvFiltros)
    public void filtros() {
        Toast.makeText(getActivity(), "Has pulsado los filtros!", Toast.LENGTH_SHORT).show();
    }

    private void refresh() {
        getFragmentManager()
                .beginTransaction()
                .detach(SolicitudesGeneradasFragment.this)
                .attach(SolicitudesGeneradasFragment.this)
                .commit();
    }

    public String getUUID() {

        String uuid;

        uuid = Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.ANDROID_ID);

        return uuid;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}

This is the code of my Adapter:

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

    private ArrayList<SolicitudesGeneradas> solicitudesGeneradas;
    private int layout;
    public OnItemClickListener itemClickListener;

    public SolicitudesGeneradasAdapter(ArrayList<SolicitudesGeneradas> solicitudes, int layout, OnItemClickListener listener) {
        this.solicitudesGeneradas = solicitudes;
        this.layout = layout;
        this.itemClickListener = listener;
    }

    @Override
    public SolicitudesGeneradasAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(SolicitudesGeneradasAdapter.ViewHolder holder, int position) {

        SolicitudesGeneradas solicitudesGeneradas = this.solicitudesGeneradas.get(position);

        holder.bind(solicitudesGeneradas.getIdSolicitud(),
                solicitudesGeneradas.getNombreSucursal(),
                solicitudesGeneradas.getFechaRegistro(),
                solicitudesGeneradas.getFolioSolicitud(),
                solicitudesGeneradas.getNivel(),
                solicitudesGeneradas.getSubcategoriaDescripcion(),
                solicitudesGeneradas.getUltimoStatus(),
                itemClickListener);

    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvNombreSucursal)
        TextView tvNombreSucurusal;
        @BindView(R.id.tvFechaHoraRegistro)
        TextView tvFechaHoraRegistro;
        @BindView(R.id.tvFolio)
        TextView tvFolio;
        @BindView(R.id.tvNivelEstatus)
        TextView tvNivelEstatus;
        @BindView(R.id.tvSubcategoriaDescripcion)
        TextView tvSubcategoriaDescripcion;
        @BindView(R.id.tvEstatus)
        TextView tvEstatus;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bind(final String id, final String sucursal, final String fechaRegistro, final String folio, final String nivel, final String subcategoriaDescripcion, final String ultimoEstatus, final OnItemClickListener listener) {
            tvNombreSucurusal.setText(sucursal);
            tvFechaHoraRegistro.setText(fechaRegistro);
            tvFolio.setText(folio);
            tvNivelEstatus.setText(nivel);
            tvSubcategoriaDescripcion.setText(subcategoriaDescripcion);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(id, getAdapterPosition());
                }
            });
            tvEstatus.setText(ultimoEstatus);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String name, int position);
    }

The truth is that I have followed several tutorials, but I do not load, I do not know if I'm wrong or I'm wrong, I thought if I have to put a LIMIT in my MySQL query and go passing the amount that I want to add as if it were the pagination of a website.

I hope you have explained me well, anyway I appreciate your attention.

Thank you!

    
asked by Jordy Santamaria 09.05.2018 в 00:42
source

1 answer

1

Hello that for Endless Scroll you need to implement it in the following way:

The counter:

int page = 0

Two Arrangements:

private ArrayList<History> listPage = new ArrayList<>();
private ArrayList<History> listPageCopy = new ArrayList<>();

and use the recyclerView method that is addOnScrollListener with that we can know when the end of the scroll arrives:

 listRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!recyclerView.canScrollVertically(1)) {
               page++
               //Haga puedes icorporar la logica que deseas
            }
        }
    });

The recyclerView.canScrollVertically (1) lets you know when you reach the end of the recyclerView when it happens that you call the paged service to load the copy as follows:

listPageCopy.add(x)

then you pass it to the original arrangement in this way:

listPage.addAll(listPageCopy);
listPageCopy.clear();

And at the end you delete the copy arrangement for when you call the service again by the scroll of the recyclerView only download the new data and do not keep the previous one

- I hope this helps you

    
answered by 10.09.2018 в 18:36