如何在带有3个选项卡的滑动选项卡布局中使用3个片段

时间:2016-05-02 11:47:56

标签: android fragment

我想在toolbar的顶部和下方创建一个工具栏,其中有一个带有3个标签的滑动tablayout。 我想在每个标签中使用fragment。像这样:

在tab1内的片段1 tab2内的片段2 tab3中的片段3

我该如何定义它。

我自己的pagerAdapter代码有3个不同的fragment,但我不知道它是否正确:

    class MyPagerAapter extends FragmentPagerAdapter {
        String[] tabs;
        public MyPagerAapter(FragmentManager fm) {
            super(fm);
            //tabs = getResources().getStringArray(R.array.tabs);
            tabs = new String[]{"پیامهای دریافتی", "پیامهای ارسالی", "ارسال پیام"};
        }
        @Override
        public Fragment getItem(int position) {
            Fragment myFragment=null;
            if (position == 0) {
                myFragment = MyFragment0.getInstance(0);
            }
            if (position == 1) {
                myFragment = MyFragment1.getInstance(1);
            }
            if (position == 2) {
                myFragment = MyFragment2.getInstance(2);
            }
            return myFragment;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
        @Override
        public int getCount() {
            return 3;
        }
    }
    public static class MyFragment0 extends Fragment {
        private TextView textView;
        public static MyFragment0 getInstance(int position) {
            MyFragment0 myFragment = new MyFragment0();
            Bundle args = new Bundle();
            args.putInt("position", position);
            myFragment.setArguments(args);
            return myFragment;
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_one, container, false);
            textView = (TextView) layout.findViewById(R.id.position);
            Bundle bundle = getArguments();
            if (bundle != null) {
                textView.setText("The Page Selected Is 0000000" + bundle.getInt("position"));
            }
            return layout;
        }
    }
    public static class MyFragment1 extends Fragment {
        private TextView textView;
        public static MyFragment1 getInstance(int position) {
            MyFragment1 myFragment = new MyFragment1();
            Bundle args = new Bundle();
            args.putInt("position", position);
            myFragment.setArguments(args);
            return myFragment;
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_two, container, false);
            textView = (TextView) layout.findViewById(R.id.position);
            Bundle bundle = getArguments();
            if (bundle != null) {
                textView.setText("The Page Selected Is 1111111" + bundle.getInt("position"));
            }
            return layout;
        }
    }
    public static class MyFragment2 extends Fragment {
        private TextView textView;
        public static MyFragment2 getInstance(int position) {
            MyFragment2 myFragment = new MyFragment2();
            Bundle args = new Bundle();
            args.putInt("position", position);
            myFragment.setArguments(args);
            return myFragment;
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_three, container, false);
            textView = (TextView) layout.findViewById(R.id.position);
            Bundle bundle = getArguments();
            if (bundle != null) {
                textView.setText("The Page Selected Is 22222222" + bundle.getInt("position"));
            }
            return layout;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

This正是您在寻找什么

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(), "ONE");
        adapter.addFragment(new TwoFragment(), "TWO");
        adapter.addFragment(new ThreeFragment(), "THREE");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = 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, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}