0

I have a tabbed activity with three fragments and each fragment make a request to an API that shows the matches of the selected day in a date picker, the previous and the next one. When doing that, the entire title is not put on the tabs and to see the data I have to swipe down. If I comment the API petition lines, the title of the tabs change like I want to do.

This is the part when I change the fragments.

DatePickerDialog.OnDateSetListener listenerFecha = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String fechaSeleccionada = String.format("%04d-%02d-%02d", year, monthOfYear + 1, dayOfMonth);

                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.YEAR, year);
                    calendar.set(Calendar.MONTH, monthOfYear);
                    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    
                    Calendar calendarAnterior = (Calendar) calendar.clone();
                    calendarAnterior.add(Calendar.DATE, -1);
    
                    Calendar calendarSiguiente = (Calendar) calendar.clone();
                    calendarSiguiente.add(Calendar.DATE, 1);
    
                    SimpleDateFormat formatoDiaSemana = new SimpleDateFormat("EEE dd MMM", new Locale("es"));
    
                    String textoAnterior = formatoDiaSemana.format(calendarAnterior.getTime());
                    String textoSeleccionado = formatoDiaSemana.format(calendar.getTime());
                    String textoSiguiente = formatoDiaSemana.format(calendarSiguiente.getTime());
    
    
                    tabLayout.removeAllTabs();
    
                    viewPagerCalendario = new ViewPagerCalendario(MenuPrincipalTabbed.this, fechaSeleccionada);
                    viewPager2.setAdapter(viewPagerCalendario);
    
                    TabLayout.Tab tabAnterior = tabLayout.newTab().setText(textoAnterior);
                    TabLayout.Tab tabSeleccionado = tabLayout.newTab().setText(textoSeleccionado);
                    TabLayout.Tab tabSiguiente = tabLayout.newTab().setText(textoSiguiente);
    
                    tabLayout.addTab(tabAnterior);
                    tabLayout.addTab(tabSeleccionado);
                    tabLayout.addTab(tabSiguiente);
                    viewPager2.setAdapter(viewPagerCalendario);
    
                    viewPager2.setCurrentItem(1);
                    tabSeleccionado.select();
                    
    
                }
            };

This is the viewPager

public class ViewPagerCalendario extends FragmentStateAdapter {

    private String fecha;
    private String fechaAnterior;
    private String fechaSiguiente;
    private Retrofit retrofit;
    
    public ViewPagerCalendario(@NonNull FragmentActivity fragmentActivity, String fecha) {
        super(fragmentActivity);
        this.retrofit = APIClient.getRetrofitInstance();
    
    
        if (fecha != null){
            this.fecha = fecha;
            // Obtener la fecha anterior y la fecha siguiente
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(fecha);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, -1);
                fechaAnterior = sdf.format(calendar.getTime());
                calendar.add(Calendar.DATE, 2);
                fechaSiguiente = sdf.format(calendar.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position) {
            case 0:
                return new AnteriorFragment(fechaAnterior, retrofit);
            case 1:
                return new SeleccionadoFragment(fecha, retrofit);
            case 2:
                return new SiguienteFragment(fechaSiguiente, retrofit);
            default:
                return null;
        }
    }
    
    @Override
    public int getItemCount() {
        return 3;
    }

}

And this one of the three fragments. All have the same structure

public class SeleccionadoFragment extends Fragment {
String fecha;
Retrofit retrofit;
public SeleccionadoFragment(String fecha, Retrofit retrofit) {
this.fecha = fecha;
this.retrofit = retrofit;
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_ayer, container, false);
    
        System.out.println("AYER");
        RecyclerView recyclerView = rootView.findViewById(R.id.recyclerViewTabbedAyer);
    
        AdaptadorListaLigas adaptadorListaLigas = new AdaptadorListaLigas(getContext());
    
        recyclerView.setAdapter(adaptadorListaLigas);
    
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    
        ServicioAPI apiService = retrofit.create(ServicioAPI.class);
    
        Call<PartidosResponse> call = apiService.getPartidosPorDia(fecha);
        call.enqueue(new Callback<PartidosResponse>() {
            @Override
            public void onResponse(Call<PartidosResponse> call, Response<PartidosResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    List<com.example.footballscore.entidades.Response> partidos = response.body().getResponse();
    
                    ArrayList<LigaDTO> ligas = new ArrayList<>();
    
                    for (com.example.footballscore.entidades.Response partido : partidos) {
                        // Obtener la información de la liga
                        int ligaId = partido.getLeague().getId();
                        String ligaNombre = partido.getLeague().getName();
                        String ligaLogo = partido.getLeague().getLogo();
                        String ligaBandera = partido.getLeague().getFlag();
    
                        // Buscar la liga en el ArrayList de ligas
                        LigaDTO liga = null;
                        for (LigaDTO l : ligas) {
                            if (l.getId() == ligaId) {
                                liga = l;
                                break;
                            }
                        }
    
                        // Si la liga no está en el ArrayList, crear una nueva Liga y agregarla al ArrayList
                        if (liga == null) {
                            liga = new LigaDTO(ligaId, ligaNombre, ligaLogo, ligaBandera);
                            ligas.add(liga);
                        }
    
                        // Agregar el partido a la liga correspondiente
                        PartidoDTO partidoNuevo = new PartidoDTO(
                                partido.getFixture().getId(),
                                partido.getFixture().getReferee(),
                                partido.getFixture().getTimezone(),
                                partido.getFixture().getDate(),
                                partido.getFixture().getTimestamp(),
                                partido.getFixture().getStatus().getElapsed(),
                                partido.getTeams().getHome().getName(),
                                partido.getTeams().getAway().getName(),
                                partido.getGoals().getHome(),
                                partido.getGoals().getAway(),
                                partido.getFixture().getStatus().getIsAcabadoLargo(),
                                partido.getLeague().getRound(),
                                partido.getTeams().getHome().getLogo(),
                                partido.getTeams().getAway().getLogo()
                        );
    
                        liga.getPartidos().add(partidoNuevo);
    
    
                    }
                    adaptadorListaLigas.agregarListaLigas(ligas);
                }
            }
    
            @Override
            public void onFailure(Call<PartidosResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    
        return rootView;
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

}

I want to select a date in a DatePicker and that the title of the tabs shows the selected day, the previous and the next, also making the request to the API and showing me the data without having to slide down

0 Answers0