Имею Tablayout с 6 вкладками дней недели. В каждой вкладке имеется RecyclerView списка предметов. При первом появлении в первой вкладке выводится только первый элемент списка. Первый элемент простой, далее идут классы. При переходе на 3-ю вкладку и возврате на первую, все элементы отображаются корректно. Вероятно проблема в RecyclerView. Что мне следует сделать для корректной работы? Заранее спасибо!
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];
}
}
PageFragment.java
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;
}
}
RecyclerTable.java
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;
}
}
Lesson.java
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; }
}
Table.java
Ссылка на Хабр:
https://github.com/AnnaDesert/Schedule1.0
Так как на могу уместить всё