如何管理android中选项卡式活动中的布局文件?

时间:2016-06-29 09:28:49

标签: android android-fragments tabs

解释

我在创建项目时进行了选项卡式活动。其中viewpager中的所有片段只有一个布局文件。我想为不同的片段设置不同的布局,并使用动态片段动态添加标签。

假设,我有5个大小,那么将创建5个制表符,如果我有2个则会创建两个制表符。

这是我的MainActivity.java,其中内部类也是

public class MainActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;

    public ViewPager mViewPager;

    String[] title={"SHOES","BAG","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);

        mSectionsPagerAdapter.noOfFragment(10);
        mSectionsPagerAdapter.addPageTitle(title);
        mViewPager.setAdapter(mSectionsPagerAdapter);

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        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) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            return rootView;
        }
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        int val=0;
        List<String> pageTitle=new ArrayList<>();
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return PlaceholderFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return val;

        }

        public void addPageTitle(String[] title){
            for(int i=0;i<title.length;i++){
                pageTitle.add(title[i]);
            }
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return pageTitle.get(position);
        }
        public void noOfFragment(int size){
            val=size;
        }
    }
}

在上面的代码中,我已经创建了一个动态标签。我想分开每个标签的布局。

请帮我解决。

1 个答案:

答案 0 :(得分:2)

您应该为每个视图创建一个片段,并在onViewChanged上设置视图

foreach (var classMapping in configuration.ClassMappings)
{
  foreach (var property in classMapping.PropertyIterator)
  {
    Console.WriteLine("{0} access={1}",
      property.Name,
      property.PropertyAccessorName);
  }
}

你应该创建一个看起来像这样的自定义适配器

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

然后你的活动你应该创建片段列表并将其传递给适配器

public class MyAdapter extends FragmentPagerAdapter {

    List<Fragment> fragments;
    List<String> titles;

    public MyAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);

        this.fragments = fragments;
        this.titles = titles;

    } 


    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

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