包含recyclerview的TabLayout在另一个标签上滑动时丢失了信息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了包含recyclerview的TabLayout在另一个标签上滑动时丢失了信息相关的知识,希望对你有一定的参考价值。
我在TabLayout中有一个RecyclerView,效果很好。。我充满了RecyclerView的数据,并且效果很好,但是当滑动到其他Tab并返回并返回上一个Tab时,recyclerView中的数据消失了。我做错了什么?
ScheduleAndGuestList.java
public class ScheduleAndGuestList extends AppCompatActivity
private TabLayout tabLayout;
private ViewPager viewPager;
private TabItem tabSchedale,tabGuests;
private pageAdapter pagerAdapter;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule_and_guest_list);
tabLayout =(TabLayout)findViewById(R.id.tableLayout);
tabSchedale=(TabItem)findViewById(R.id.tabSchedulePest);
tabGuests=(TabItem)findViewById(R.id.tabGuestsPest);
viewPager=findViewById(R.id.viewPager);
pagerAdapter=new pageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
if (tab.getPosition() == 0)
pagerAdapter.notifyDataSetChanged();
else if (tab.getPosition() == 1)
pagerAdapter.notifyDataSetChanged();
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
viewPager.setOffscreenPageLimit(tabLayout.getTabCount());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabSchedule.java
public class tabSchedule extends Fragment
private String horaMin,stringDescrp;
private Button btn, seleccHora;
private EditText descrpEt;
private ImageView img1,img2;
private HorizontalScrollView scrollView;
private ArrayList<String> arrayHora,arrayDescrp;
private RecyclerView recyclerViewtab;
listScheduleAdapter adapter=new listScheduleAdapter(arrayHora,arrayDescrp);
public tabSchedule()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab_schedule, container, false);
btn = (Button) view.findViewById(R.id.button);
recyclerViewtab=(RecyclerView)view.findViewById(R.id.recyclerViewScheduleId);
arrayHora=new ArrayList<String>();
arrayDescrp=new ArrayList<String>();
//RecyclerView
recyclerViewtab.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
recyclerViewtab.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
adapter=new listScheduleAdapter(arrayHora,arrayDescrp);
recyclerViewtab.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_addschedule, null);
scrollView=(HorizontalScrollView)view.findViewById(R.id.scrollView);
img1=(ImageView)view.findViewById(R.id.img1);
img2=(ImageView)view.findViewById(R.id.img2);
//Selecionar hora
seleccHora = (Button) view.findViewById(R.id.seleccionarHoraSheduleId);
seleccHora.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener()
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute)
horaMin = String.format("%02d:%02d", selectedHour, selectedMinute);
seleccHora.setText(horaMin);
, hour, minute, true);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
);
img1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
img1.setBackgroundColor(Color.parseColor("#FF4081"));
Toast.makeText(getActivity(), "Works 1!", Toast.LENGTH_SHORT).show();
);
builder.setTitle("Agregar programacion");
builder.setPositiveButton("Agregar", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
descrpEt=(EditText)view.findViewById(R.id.descripScheduleId);
stringDescrp=descrpEt.getText().toString();
arrayHora.add(horaMin);
arrayDescrp.add(stringDescrp);
adapter.notifyDataSetChanged();
).setNegativeButton("Cancelar", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
);
builder.setView(view);
builder.show();
);
return view;
pageAdapter.java
public class pageAdapter extends FragmentPagerAdapter
private int numOfTabs;
public pageAdapter(@NonNull FragmentManager fm, int numOfTabs)
super(fm);
this.numOfTabs = numOfTabs;
@NonNull
@Override
public Fragment getItem(int position)
switch (position)
case 0:
return new tabSchedule();
case 1:
return new tabGuests();
default:
return null;
@Override
public int getCount()
return numOfTabs;
@Override
public int getItemPosition(Object object)
return POSITION_NONE;
答案
[请尝试在MainActivity.java上遵循以下源代码
另一答案
尝试使用viewpager的属性viewPager.offscreenPageLimit = 2
。
以上是关于包含recyclerview的TabLayout在另一个标签上滑动时丢失了信息的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView不会进入tabLayout和工具栏下
Android tabLayout+recyclerView实现锚点定位
如何使用 TabLayout 在 Fragment 中使用 CardView 行实现 RecyclerView
如何在 TabLayout 中找到由 RecyclerView 打开的对话框的上下文?
ViewPager2 TabLayout Fragment RecyclerView滑动冲突
对多个 TabLayout 仅使用一个带有 RecyclerView 的 Fragment,无法在选项卡更改时正确更新数据