我在底部使用BottomNavigationView创建了一个活动。 bottomnavigationview的第二个选项卡包含TabLayout(一周中的某几天)。 但是有一些奇怪的错误。当我运行应用程序时,没关系,然后我进入了带有tablayout的第二个选项卡,没关系,然后我又回到了第一个选项卡(第四张图片-好像应该这样),没关系,然后我转到了第二个标签,它是空的。但是,如果我要滑动(在TabLayout中第二天和第二天),那一天的数据就会出现。而且打开TabLayout中的哪个标签都没有关系,您总是应该两次滑动(向后或向后)此标签才会出现 例如,我在第二个选项卡上。我刷卡3(现在为空),我刷卡4(它已经不为空)。然后回到3(空),回到2(非空)。因此,如果我想显示3,则应该将其滑动到5或1。我做错了什么?
这是我在 activity_main.xml
中添加bottomNavigationView的方式<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation"
android:background="?android:attr/windowBackground" />
这是 MainActivity
public class MainActivity extends AppCompatActivity {
private Fragment f1;
private Fragment f2;
private Fragment f3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f1 = new NotificationsFragment();
f2 = new SheduleFragment();
((SheduleFragment) f2).setGroups(groups); // it's the second panel with TabLayout
f3 = new CabinetsFragment();
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f1).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.navigation_notifications: {
selectedFragment = f1;
break;
}
case R.id.navigation_shedule:
selectedFragment = f2;
break;
case R.id.navigation_cabinets:
selectedFragment = f3;
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
这是TabLayout的片段,它将位于BottomNavigationView的第二个标签中
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="25dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="@color/colorPrimary"
app:tabIndicatorColor="@color/colorPrimary"
android:background="@color/colorBackgroundLL">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
Fragment中的onCreateView函数(TabLayout中的标签为
)public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) {
View view = inflater.inflate(R.layout.day_layout, container, false);
rv = (RecyclerView) view.findViewById(R.id.recycler_days_view);
llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
ra = new DayRecycleAdapter(teachers,subjects, cabinets,timeBegs, timeEnds );
rv.setAdapter(ra);
return view;
}
底部导航视图中第二个标签的片段
public class SheduleFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private Group[] groups;
ViewPagerAdapter vpa;
DayFragment df1;
DayFragment df2;
DayFragment df3;
DayFragment df4;
DayFragment df5;
DayFragment df6;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState ) {
View view = inflater.inflate(R.layout.fragment_shedule, container, false);
df1 = new DayFragment();
df2 = new DayFragment();
df3 = new DayFragment();
df4 = new DayFragment();
df5 = new DayFragment();
df6 = new DayFragment();
tabLayout = (TabLayout)view.findViewById(R.id.tab_layout);
viewPager = (ViewPager)view.findViewById(R.id.viewpager);
setNewFragments(df1, df2, df3, df4, df5, df6);
viewPager.setAdapter(vpa);
tabLayout.setupWithViewPager(viewPager);
return view;
}
public void setGroups(Group[] groups) {
this.groups = groups;
}
public void setNewFragments(DayFragment df1, DayFragment df2, DayFragment df3, DayFragment df4, DayFragment df5, DayFragment df6) {
vpa = new ViewPagerAdapter(this.getFragmentManager());
setInfo(df1, df2, df3, df4, df5, df6);
vpa.addFragment(df1, "пн");
vpa.addFragment(df2, "вт");
vpa.addFragment(df3, "ср");
vpa.addFragment(df4, "чт");
vpa.addFragment(df5, "пт");
vpa.addFragment(df6, "сб");
}
public void changeFragments(DayFragment df1, DayFragment df2, DayFragment df3, DayFragment df4, DayFragment df5, DayFragment df6) {
setOneDayInfo(df1, 0);
setOneDayInfo(df2, 1);
setOneDayInfo(df3, 2);
setOneDayInfo(df4, 3);
setOneDayInfo(df5, 4);
setOneDayInfo(df6, 5);
}
public void setInfo(DayFragment df1, DayFragment df2, DayFragment df3, DayFragment df4, DayFragment df5, DayFragment df6) {
setOneDayInfo(df1, 0);
setOneDayInfo(df2, 1);
setOneDayInfo(df3, 2);
setOneDayInfo(df4, 3);
setOneDayInfo(df5, 4);
setOneDayInfo(df6, 5);
}
public void setOneDayInfo(DayFragment df, int i) {
ArrayList<Lesson> lessons = groups[groupNumber-1].getDays()[i].getLessons();
int n = lessons.size();
String[] teachers = new String[n];
String[] subjects = new String[n];
String[] cabinets = new String[n];
String[] timeBegs = new String[n];
String[] timeEnds = new String[n];
int j=0;
for (Lesson les: lessons) {
teachers[j] = les.teachersToString();
subjects[j] = les.subjectsToString();
cabinets[j]= les.cabinetsToString();
timeBegs[j] = les.timeBegToSting();
timeEnds[j] = les.timeEndToSting();
++j;
}
df.setTeachers(teachers);
df.setSubjects(subjects);
df.setCabinets(cabinets);
df.setTimeBegs(timeBegs);
df.setTimeEnds(timeEnds);
}
}
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentListTitles = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentListTitles.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentListTitles.get(position);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentListTitles.add(title);
}
}