在我的应用程序中,父视图传呼器有5个片段,其中2个使用其中的第二个视图传呼器,其中包含另外5个片段。用于导航在“父视图”寻呼机中,我正在使用“底部导航”。在子级导航中,我使用的是Tab布局。
问题是全部视图,但子视图分页器的视图显示在父视图分页器内部。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;
}
}
与“父viewpager初始化”有关的所有方法:
// ************-设置视图寻呼机-********************* 调用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;
}
}
与子视图分页器相关的方法: 在create上调用此方法
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);
}
我在子视图分页器的addOnPagerListen的各种事件中尝试过viewpager.getAdapter.notifyDatasetChanges(),但是冻结了应用程序。
答案 0 :(得分:1)
getChildFragmentManager()
返回私有的FragmentManager,用于在Fragment内放置和管理Fragment。
getSupportFragmentManager()
返回用于与与此片段的活动相关联的片段进行交互的FragmentManager。
在与子视图分页器相关的方法中
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);