我已经实现了其他内部的一个视图寻呼机。要求是在父视图寻呼机上具有选项卡,并在子视图寻呼机上显示其项目。例如,父视图分页器有两个选项卡(片段,视频),而我们需要在相应选项卡中显示的剧集和视频数量(子视图分页器)。问题是,当我从屏幕边缘滑动时,即使当前选项卡有更多项目要加载,它也会更改选项卡。但是,当我从屏幕中央滑动并在“情节”选项卡上显示下一个情节时,它可以正常工作。
这是一个代码:
public class ParentPagerAdapter extends FragmentStatePagerAdapter {
private List<String> tabList;
private boolean isPilot;
private ArrayList<?extends IVideoModel> videoList;
private ArrayList<?extends IEpisodeAndVideoModel> episodeList;
public ParentPagerAdapter(FragmentManager fm, List<String> tabList,
ArrayList<?extends IEpisodeModel> episodeList, ArrayList<?extends IVideoModel>
videoList) {
super(fm);
this.episodeList = episodeList;
this.videoList = videoList;
this.tabList = tabList;
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
Bundle bundle = new Bundle();
if(tabList.get(position).contains(VIDEOS_TAB_TITLE)){
fragment = new VideoTabFragment();
return fragment;
}else if(tabList.get(position).contains(EPISODES_TAB_TITLE)){
fragment = new EpisodeTabFragment();
return fragment;
}
return null;
}
@Override
public int getCount() {
return tabList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabList.get(position);
}
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}
子视图分页器1:
public class ProjectVideoPagerAdapter extends PagerAdapter {
public final String TAG = ProjectVideoPagerAdapter.class.getCanonicalName();
private Context mContext;
private LayoutInflater mLayoutInflator;
private RowProjectLandingPagerItemBinding itemBinding;
private List<? extends IVideoModel> projectVideoList;
public ProjectVideoPagerAdapter(Context context, List<? extends IVideoModel> projectVideoList) {
mContext = context;
this.projectVideoList = projectVideoList;
mLayoutInflator = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
debugLog("Entering instantiateItem with position = " + position);
itemBinding =
DataBindingUtil.inflate(mLayoutInflator, R.layout.row_project_landing_pager_item, null,
false);
if (projectVideoList == null) return null;
container.addView(itemBinding.getRoot());
debugLog("Exiting instantiateItem()");
return itemBinding.getRoot();
}
@Override
public int getCount() {
if (projectVideoList != null && !projectVideoList.isEmpty()) return projectVideoList.size();
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
//super.destroyItem(container, position, object);
}
}
子视图分页器2:
public class ProjectVideoPagerAdapter extends PagerAdapter {
public final String TAG = ProjectVideoPagerAdapter.class.getCanonicalName();
private Context mContext;
private LayoutInflater mLayoutInflator;
private RowProjectLandingPagerItemBinding itemBinding;
private List<? extends IEpisodeModel> projectEpisodeList;
public ProjectVideoPagerAdapter(Context context, List<? extends IEpisodeModel> projectList) {
mContext = context;
this.projectEpisodeList = projectList;
mLayoutInflator = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
debugLog("Entering instantiateItem with position = " + position);
itemBinding =
DataBindingUtil.inflate(mLayoutInflator, R.layout.row_project_landing_pager_item, null,
false);
if (projectEpisodeList == null) return null;
container.addView(itemBinding.getRoot());
debugLog("Exiting instantiateItem()");
return itemBinding.getRoot();
}
@Override
public int getCount() {
if (projectEpisodeList != null && !projectEpisodeList.isEmpty()) return projectEpisodeList.size();
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
//super.destroyItem(container, position, object);
}
}
最佳答案
对于内部视图寻呼机,应使用“ childfragmentmanager”而不是“ fragmentmanager”。工作正常。
关于android - 内部 View 寻呼机的实现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50286726/