java – ANDROID:在instantiateItem方法中具有不同片段的ViewPager

前端之家收集整理的这篇文章主要介绍了java – ANDROID:在instantiateItem方法中具有不同片段的ViewPager前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用ViewPager创建向左滑动的片段,现在我想在每个片段页面中插入片段.

activityMain.xml

还有这个:
tab0_fragment.xml

我在mainActivity.java中有一个ViewPager

final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
viewPager.setAdapter(new PagerAdapter() {
...

...
}

我尝试通过片段布局在viewpager中打开一个屏幕.

我试试这个:

final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
viewPager.setAdapter(new PagerAdapter() {
    ...
    public Object instantiateItem(final ViewGroup container,final int position) {
           if (position == 0)
           {
                final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.fragment_tab0,null,false);
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.myviewpager,new tab0Fragment()).commit();
                container.addView(view);
                return view;
           }
           else if (position == 1)
           ...

   } 

...
}

AND tab0Fragment.java

public class tab0Fragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab0,null);
    }
}

我是机器人中的菜鸟.怎么能纠正这个代码

解:
答案不是我的答案,但这对我帮助很大.

根据@Mr. Rabbit ans @Amit Upadhyay我将顶级代码更改为:

@Override
            public Object instantiateItem(final ViewGroup container,final int position) {

                if (position == 0)
                {
                    final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.fragment_tab0,false);
                    MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
                    adapter.addFragment(new tab0Fragment(),getResources().getString(R.string.tab0));
                    container.addView(view);
                    return view;
                }
                else if (position == 1)
                {

并根据@Mr添加MyAdapter类.兔子的答案.

希望能帮助别人.

最佳答案
尝试将适配器和代码分开.

MyAdapter.java

public class MyAdapter extends FragmentPagerAdapter{

    private final List

创建你的碎片

FragmentOne.java

public class FragmentOne extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState)    {
      View  view = inflater.inflate(R.layout.fragment_one,container,false);
        return view;
    }
}

FragmentTwo.java

public class FragmentTwo extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable Bundle savedInstanceState)    {
     View  view = inflater.inflate(R.layout.fragment_two,false);
        return view;
    }
}

在您的活动中设置您的viewpager.

final ViewPager viewPager = (ViewPager) findViewById(R.id.myviewpager);
    MyAdapter adapter = new MyAdapter(getSupportFragmentManager());

    // Add your fragments in adapter.
    FragmentOne fragmentOne = new FragmentOne();
    adapter.addFragment(fragmentOne,getResources().getString(R.string.fragment_one_title));

    FragmentTwo fragmentTwo = new FragmentTwo();
    adapter.addFragment(fragmentTwo,getResources().getString(R.string.fragment_two_title));

    viewPager.setAdapter(adapter);

然后最后使用ViewPager设置NavigationTabBar.

final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal);
navigationTabBar.setViewPager(viewPager,2);
原文链接:https://www.f2er.com/android/430104.html

猜你在找的Android相关文章