Answer the question
In order to leave comments, you need to log in
Why is the first Fragment in Tablayout loaded only after clicking on the 3rd and further tabs?
I have a Tablayout with 6 days of the week tabs. Each tab has a RecyclerView of the list of items. The first time it appears in the first tab, only the first item in the list is displayed. The first element is simple, followed by classes. When switching to the 3rd tab and returning to the first, all elements are displayed correctly. Probably a problem with the RecyclerView. What should I do to make it work correctly? Thanks in advance!
PageAdapter.java
package com.example.schedule;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
public class PageAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 6;
private String tabTitles[] = new String[] { "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"};
private Context context;
public PageAdapter(@NonNull FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@NonNull
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override public CharSequence getPageTitle(int position) {
// генерируем заголовок в зависимости от позиции
return tabTitles[position];
}
}
package com.example.schedule;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.schedule.utils.RecyclerTable;
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
RecyclerView recyclerView;
static String str="";
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPage = getArguments().getInt(ARG_PAGE);
}
}
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.tablelist);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
switch (mPage){
case 1: Table.Week = Table.Mnd; break;
case 2: Table.Week = Table.Tue; break;
case 3: Table.Week = Table.Wed; break;
case 4: Table.Week = Table.Th; break;
case 5: Table.Week = Table.Fri; break;
case 6: Table.Week = Table.Sat; break;
}
RecyclerTable tableAdapter = new RecyclerTable(Table.Week, Table.Week.size());
recyclerView.setAdapter(tableAdapter);
return view;
}
}
package com.example.schedule.utils;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.schedule.Lesson;
import com.example.schedule.R;
import java.util.ArrayList;
public class RecyclerTable extends RecyclerView.Adapter<RecyclerTable.TableViewHolder>{
ArrayList<Lesson> Lesson;
int i;
public RecyclerTable(ArrayList<Lesson> Lesson, int i){
this.Lesson = Lesson;
this.i = i;
}
@NonNull
@Override
public TableViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table, parent, false);
return new TableViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TableViewHolder holder, int position) {
//if(i!=0) {
holder.textTime.setText("Время");
//holder.textLesson.setText("Ghtlvtn");
holder.textLesson.setText(Html.fromHtml(Lesson.get(position).getFull_name_lesson()));
holder.textKorpus.setText(Lesson.get(position).getStation());
//Log.i("myTag","Set text on position " + position);
//}
}
@Override
public int getItemCount() {
return i;
}
//////////////////////////////////////////////////////
public class TableViewHolder extends RecyclerView.ViewHolder{
TextView textTime;
TextView textLesson;
TextView textKorpus;
public TableViewHolder(@NonNull View itemView) {
super(itemView);
textTime = (TextView) itemView.findViewById(R.id.textTime);
textLesson = (TextView) itemView.findViewById(R.id.textLesson);
textKorpus = (TextView) itemView.findViewById(R.id.textKorpus);
}
}
///////////////////////////////////////////////////////////
public static ArrayList<Lesson> NewLesson(ArrayList<Lesson> Lesson){
Log.i("MyTagAboutSize","Size Array: " + Lesson.size());
return Lesson;
}
}
package com.example.schedule;
import android.util.Log;
public class Lesson {
private String subgroup = "";
private String name_lesson = "";
private String type_lesson = "";
private String building = "";
private String room = "";
private String Family = "";
private String Name = "";
private String Secondname = "";
private String special = "";
private String full_name_lesson;
private String station;
private int day;
private int number;
public Lesson(String subgroup,String name_lesson, String special,
String type_lesson,String building,
String room,String Family,String Name, String Secondname, int day, int number){
this.name_lesson = name_lesson + " ";
if(!special.equals("")){
this.special = "(" + special + ") ";
}
if(subgroup.equals("0"))
this.subgroup = "";
else
this.subgroup = "подгруппа " + subgroup;
if(!type_lesson.equals(""))
this.type_lesson = "(" + type_lesson + ")";
if(building.equals("0"))
this.building = "";
else
this.building = "Корпус " + building + "\n";
if(!room.equals("ДОТ")&&!room.equals(""))
this.room = "ауд" + room;
this.Family = Family + " \n";
if(!Name.equals(""))
this.Name = Name.charAt(0) + ". ";
if(!Secondname.equals(""))
this.Secondname = Secondname.charAt(0) + ".";
this.day = day;
this.number = number;
//Log.i("myTag","Lesson add " + name_lesson);
full_name_lesson = "<b>" + this.name_lesson + "</b> " + this.special + this.type_lesson + "\n<b>" + this.Family + this.Name + this.Secondname + "</b>\n"+ this.subgroup;
station = this.building + this.room;
//Log.i("myTag","Full name " + full_name_lesson + "\nstation " + station);
}
public String getFull_name_lesson(){
return full_name_lesson;
}
public String getStation(){
return station;
}
public int getDay(){ return day; }
public int getNumber(){ return number; }
}
Answer the question
In order to leave comments, you need to log in
In general, your problem is in asynchronous data loading and in statics.
Everything happens like this: you go to the screen, the adapter is created instantly and filled from Mnd. The data there is 1 element. Then the data is loaded, the static is updated, but no one notifies the adapter about this (notify*). Then you start walking through other pages, at some point you return, and voila, you create a new adapter with fresh data.
What to do. Repository pattern and Observer pattern (or spin RxJava). No static nafig. You will immediately have to think about what you are doing, it will be difficult at first, but it is necessary. Throw out the asynctask and forget about it like a bad dream. retrofit+okhttp+moshi. No parsing by json, especially on the main thread.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question