我可以在选项卡视图的viewpager中为每个页面分隔java类文件

时间:2016-11-24 07:51:13

标签: android android-viewpager

我从android studio导入了选项卡式活动。 在静态占位符片段下我有oncreate视图,我已经切换每个选项卡的大小写,并为每个选项卡分别给出了布局。我想在选项卡视图中为每个页面的单独java类工作。我可以将每个案件分开进行单独的课程吗?

public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
         int tabNumber= getArguments().getInt(ARG_SECTION_NUMBER);
          switch (tabNumber)  {
            case 1: {

                View rootView = inflater.inflate(R.layout.tab1, container, false);


                return rootView;
            }
              case 2: {

                  View rootView = inflater.inflate(R.layout.tab2, container, false);



                  return rootView;
              }
              case 3: {

                  View rootView = inflater.inflate(R.layout.tab3, container, false);




                  return rootView;
              }

3 个答案:

答案 0 :(得分:1)

private class DesignDemoPagerAdapter extends FragmentStatePagerAdapter {

        public DesignDemoPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            //
            if (position == 0) {

                    BaseFragment f = new SalesBlankTab();
                    salesStack.get(AppConstant.SALES_REPORT_TAB).push(f);
                    return f;

            } else if (position == 1) {

                    BaseFragment f = new MemberBlankTab();
                    salesStack.get(AppConstant.MEMBER_ORDER).push(f);
                    return f;

            } else {
                BaseFragment f = new  StockBlankTab();//MemberBlankTab,StockBlankTab
                salesStack.get(AppConstant.STOCK_MANAGEMENT).push(f);
                return f;
            }
        }

        @Override
        public int getCount() {
            return AppConstant.TAB_COUNT;
        }

    }

答案 1 :(得分:0)

你可以这样做(来自Android Hive):

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);
        }
    }
}

有关详细信息,您可以尝试以下链接:http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

答案 2 :(得分:0)

所以有一个geItem方法,它将变量返回为super。(.....)

只是在这个地方返回片段。

ADFS WS-Federation Sign-Out