在我的应用中,我使用寻呼机适配器加载2个片段,片段未加载,视图寻呼机保持为空。我哪里错了?任何帮助将不胜感激 这是我的代码 我正在将此适配器设置为来自活动
的视图寻呼机public class ViewPagerActivityAdapter extends PagerAdapter {
ArrayList<Fragment> tablist;
public ViewPagerActivityAdapter(ArrayList<Fragment> tablist) {
this.tablist = tablist;
}
@Override
public int getCount() {
return tablist.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
return null;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
}
}
活动部分
private ViewPagerActivityAdapter viewPagerAdapter;
private ArrayList<Fragment> fragments_faq;
fragments_faq = new ArrayList<>();
FragmentOne frag1 = new FragmentOne();
tablayout.addTab(tablayout.newTab().setText("fragone"));
fragments_faq.add(frag1);
FragmentTwo frag2 = new FragmentTwo();
tablayout.addTab(tablayout.newTab().setText("fragtwo"));
fragments_faq.add(frag2);
private void setPagerAdapter() {
viewPagerAdapter = new ViewPagerActivityAdapter(fragments_faq);
viewpager.setAdapter(viewPagerAdapter);
viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
viewpager.setOffscreenPageLimit(2);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
答案 0 :(得分:0)
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
详细信息请查看此要点 https://gist.github.com/vinay6kr/33359c3f3b0e9f28e33bd383b01e9d9f
答案 1 :(得分:0)
如果要在viewpager中显示片段,则应使用FragmentStatePagerAdapter
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
public MyViewPagerAdapter(FragmentManager fm, CharSequence mTitles[]) {
super(fm);
this.Titles = mTitles;
}
@Override
public Fragment getItem(int position) {
if (position == 0)
return new Fragment1().getInstance();
else if (position == 1)
return new Fragment2().getInstance();
}
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
@Override
public int getCount() {
return Titles.length;
}
}
并设置它们
void setUpTabs() {
String Titles[] = {"Frag1","Frag2"};
MyViewPagerAdapter adapter = new MyViewPagerAdapter(this.getSupportFragmentManager(), Titles);
viewPager.setAdapter(adapter);
}
答案 2 :(得分:0)
您可以尝试像适配器一样
class ViewPagerActivityAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public ViewPagerActivityAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
@Override
public int getCount() {
return this.fragments.size();
}
}
添加片段
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,
FragmentOne.class.getName()));
fragments.add(Fragment.instantiate(this,
FragmentTwo.class.getName()));
添加到查看寻呼机
ViewPagerActivityAdapter mPagerAdapter;
this.mPagerAdapter = new ViewPagerActivityAdapter(
super.getSupportFragmentManager(), fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
答案 3 :(得分:0)
您应该使用FragmentPagerAdapter
或FragmentStatePagerAdapter
。
以下是示例代码。
public class ViewPagerActivityAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> tablist;
public ViewPagerActivityAdapter(ArrayList<Fragment> tablist) {
this.tablist = tablist;
}
@Override
public int getCount() {
return tablist.size();
}
@Override
public Fragment getItem(int position) {
return tablist.get(position);
}
}