【问题标题】:Changing the display of fragments from a fragment pager adapter从片段寻呼机适配器更改片段的显示
【发布时间】:2014-01-28 04:49:50
【问题描述】:

我使用的是 Android Studio 中预先创建的示例。通过转到一个新项目,选择带有操作栏选项卡导航样式的空白项目并滑动来找到它。

它显然有效,它会更改 editText 的文本以表示您当前正在查看的选项卡的编号。

令人困惑的是我找不到如何为每个片段指定不同的布局。最终,我希望我的 3 个标签中的每一个都有一个完全不同的外观屏幕。我以为我会创建三个单独的片段,每个片段都有自己的布局,但这里似乎是通过示例即时完成的。

有没有人玩过这个例子,可以告诉我如何使用 XML 查看器/设计器自定义每个片段?

【问题讨论】:

    标签: android android-fragments android-studio


    【解决方案1】:

    以下是您需要在示例中进行的更改,或者也可以创建一个新项目。

    1.将您的所有视图写为片段,而不是您希望在导航选项卡中显示的活动,例如

    Fragment1, Fragment2,...
    

    Fragment 将是所有 Fragment 的超类。

    为每个片段创建 xml 布局并覆盖片段类的 onCreateView 方法,例如:

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
         // Get the view from respectivelayout.xml
    
         View view = inflater.inflate(R.layout.respectivelayoput, container, false);
    
        // Do whatever you want to do like an activity here for all the tabs
    
        return rootView;
      }
    

    2.创建一个覆盖 FragmentPagerAdapter 的新类,用于在选项卡之间滑动,类似这样

    public class ViewPagerAdapter extends FragmentPagerAdapter {
    
        // Declare the number of ViewPager pages
        final int PAGE_COUNT = 2;
        Context context;
    
        public ViewPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                // Open FragmentTab1.java
                case 0:
                    FragmentTab1 fragmenttab1 = new FragmentTab1(context);
                    return fragmenttab1;
                // Open FragmentTab2.java
                case 1:
                    FragmentTab2 fragmenttab2 = new FragmentTab2(context);
                    return fragmenttab2;
               //And so on.., make sure cases must be equal to page count you specified
            }
            return null;
        }
    
        @Override
        public int getCount() {
            return PAGE_COUNT;
        }
    
    }
    

    3.现在为您的活动创建一个 xml 布局,仅在其中包含一个 viewpager(我正在使用支持 v4 库作为片段)

     <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
    

    4.现在这里是您的 MainActivity.java 的代码 sn-p(示例中相同,没有任何变化)

         @Override
         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Set up the action bar.
            final ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            // Create the adapter that will return a fragment for each of the three
            // primary sections of the activity.
            yourAdaperInstance= new YourFragmentPagerAdapter(getSupportFragmentManager());
    
            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);  //created in xml
    
            mViewPager.setAdapter(mSectionsPagerAdapter);
    
            // When swiping between different sections, select the corresponding
            // tab. We can also use ActionBar.Tab#select() to do this if we have
            // a reference to the Tab.
            mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    
            // For each of the sections in the app, add a tab to the action bar.
            for (int i = 0; i < yourAdaperInstance.getCount(); i++) {
                // Create a tab with text corresponding to the page title defined by
                // the adapter. Also specify this Activity object, which implements
                // the TabListener interface, as the callback (listener) for when
                // this tab is selected.
                actionBar.addTab(
                        actionBar.newTab()
                                .setText(mSectionsPagerAdapter.getPageTitle(i))
                                .setTabListener(this));
            }
    
            // getCount and getPageTitle are defined in your FragmentPagerAdapter, This is the best practice or else you can add tabs and define names everything in Activity also
        }
    

    您还需要设置 TabListener,但它与示例一起提供,无需更改任何内容。

    所有这些类(MainActivity、YourFragmentPagerAdapter、Fragment1、Fragment2..)都在 Studio 示例的同一个 Activity 类中,但您可以将它们全部分开,我更喜欢这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多