在我的应用程序中,父视图寻呼机有 5 个片段,其中 2 个使用第二个视图寻呼器,其中包含另外 5 个片段。对于导航 在父视图寻呼器中,我使用的是底部导航。和在子导航中,我正在使用选项卡布局。
问题是所有视图,除了子视图寻呼机的视图显示在父视图页面中.https://ibb.co/2ScB3N6
我注意到,如果子视图寻呼机的项目在到达最后一个项目后显示。 例如,如果我到达最后一项,那么如果我转到第二项,则它会显示.https://ibb.co/jLdT9Zf
此外,如果我将项目(里面有视图寻呼机)位置更改为第一个,那么当盯着那个时间时,我只显示它是项目,但是当我将父视图寻呼机的项目更改为第二个然后转到第一个时,会出现同样的问题。
父视图寻呼机适配器的代码:
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
@Override
public int getItemPosition(Object object) {
// POSITION_NONE makes it possible to reload the PagerAdapter
return POSITION_NONE;
}
}
与父视图页器初始化相关的所有方法:
//******--设置视图寻呼机--***** 将 BottomNavigationmethod() 和 viewPagerMethod() 调用到 onCreate
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
home=new Home();
home.setArguments(HomeBundle);
speed=new Speed();
speed.setArguments(HomeBundle);
rpm=new Rpm();
rpm.setArguments(HomeBundle);
spdRpm=new SpdRpm();
spdRpm.setArguments(HomeBundle);
dataRecord=new DataRecord();
adapter.addFragment(home);
adapter.addFragment(rpm);
adapter.addFragment(dataRecord);
adapter.addFragment(spdRpm);
adapter.addFragment(speed);
viewPager.setAdapter(adapter);
}
//*******--波顿导航方法--
*************public void BottomNavigationmethod(){
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_home:
viewPager.setCurrentItem(0);
break;
case R.id.action_rpm:
viewPager.setCurrentItem(1);
break;
case R.id.action_dataRecord:
viewPager.setCurrentItem(2);
break;
case R.id.action_spdRpm:
viewPager.setCurrentItem(3);
break;
case R.id.action_speed:
viewPager.setCurrentItem(4);
break;
}
return false;
}
});
}
//******-- 查看寻呼机方法--******
public void viewPagerMethod(){
GetCurrentItem=String.valueOf(viewPager.getCurrentItem());
Log.d("getcurrntinside",GetCurrentItem);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// viewPager.getAdapter().notifyDataSetChanged();
if (prevMenuItem != null) {
prevMenuItem.setChecked(false);
}
else
{
bottomNavigationView.getMenu().getItem(0).setChecked(true);
}
Position=String.valueOf(position);
bottomNavigationView.getMenu().getItem(position).setChecked(true);
prevMenuItem = bottomNavigationView.getMenu().getItem(position);
OnViewPager=String.valueOf(bottomNavigationView.getMenu().getItem(position));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
setupViewPager(viewPager);
}
菜单 xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_home"
android:checked="true"
android:icon="@drawable/cartab"
android:title="@string/HomeTab"
app:showAsAction="always" />
<item
android:id="@+id/action_rpm"
android:checked="false"
android:icon="@drawable/rpmtab"
android:title="@string/RpmTab"
app:showAsAction="always" />
<item
android:id="@+id/action_dataRecord"
android:checked="false"
android:icon="@drawable/rpmtab"
android:title="@string/DataRecordTab"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_spdRpm"
android:checked="false"
android:icon="@drawable/spdrpmtab"
android:title="@string/SpeedRpmTab"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_speed"
android:checked="false"
android:icon="@drawable/speedtab"
android:title="@string/SpeedTab"
app:showAsAction="always" />
</menu>
子视图寻呼机的视图寻呼机适配器:
public class ViewpageAdapterFrg extends FragmentPagerAdapter {
public List<Fragment> fragmentList=new ArrayList<>();
public List<String> fragmentTittle=new ArrayList<>();
public ViewpageAdapterFrg(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentTittle.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTittle.get(position);
}
public void fragmentAdd(Fragment fragment, String text){
fragmentList.add(fragment);
fragmentTittle.add(text);
}
@Override
public int getItemPosition(Object object) {
// POSITION_NONE makes it possible to reload the PagerAdapter
return POSITION_NONE;
}
}
与子视图寻呼机相关的方法:在创建时调用此方法
public void apptabview(){
tabLayout=(TabLayout)view.findViewById(R.id.tabs);
viewPager=(ViewPager)view.findViewById(R.id.viewpage);
ViewpageAdapterFrg FirstAdapter=new ViewpageAdapterFrg(getActivity().getSupportFragmentManager());
FirstAdapter.fragmentAdd(new NormalRpmFragment(),"Normal");
FirstAdapter.fragmentAdd(new AwesomeRpmFragment(),"Awesome");
FirstAdapter.fragmentAdd(new PointerRpmFragment(),"Pointer");
FirstAdapter.fragmentAdd(new ProgressRpmFragment(),"Pointer");
FirstAdapter.fragmentAdd(new DeluxeRpmFragment(),"Deluxe");
viewPager.setAdapter(FirstAdapter);
Log.d("speedViewFragemnt",String.valueOf(viewPager.getCurrentItem()));
tabLayout.setupWithViewPager(viewPager);
}
我尝试了viewpager.getAdapter.notifyDatasetChanges()在子视图寻呼机的addOnPagerListen的各种事件中,但它冻结了应用程序。
getChildFragmentManager()
返回专用片段管理器,用于在片段中放置和管理片段。
getSupportFragmentManager()
返回片段管理器,用于与与此片段的活动关联的片段进行交互。
在您的方法中与子视图寻呼机相关
ViewpageAdapterFrg FirstAdapter=new ViewpageAdapterFrg(getChildFragmentManager());
FirstAdapter.fragmentAdd(new NormalRpmFragment(),"Normal");
FirstAdapter.fragmentAdd(new AwesomeRpmFragment(),"Awesome");
FirstAdapter.fragmentAdd(new PointerRpmFragment(),"Pointer");
FirstAdapter.fragmentAdd(new ProgressRpmFragment(),"Pointer");
FirstAdapter.fragmentAdd(new DeluxeRpmFragment(),"Deluxe");
viewPager.setAdapter(FirstAdapter);
Log.d("speedViewFragemnt",String.valueOf(viewPager.getCurrentItem()));
tabLayout.setupWithViewPager(viewPager);