2

嗨,我是 android 新手,我想寻求帮助。我在 android studio 中创建新项目,并使用带有 viewpager 的导航操作栏选项卡。Android Studio 生成此代码。我知道使用一项活动 atc,但现在我想学习使用带有标签的滑动布局。我的问题是:是否可以在其中使用不同的项目进行更多布局?例如,现在我在每个片段上都有一个文本视图,当我滑动时,我会在每一页上看到任何文本。但是我需要例如在页面(tab1)布局上使用textview,在tab2上我需要布局edittext,在tab3上我需要带有图像的布局。这可能吗?因为我可以更改文本,但现在所有选项卡的布局都相同。

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.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);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}
public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "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;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

}

4

2 回答 2

7

在这种情况下,您可以定义 3 个单独的片段类。然后你可以在getItem你的SectionsPagerAdapter

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new YourFragmentClass1();
        case 1:
            return new YourFragmentClass2();
        case 2:
            return new YourFragmentClass3();
    }
} 
于 2013-11-09T19:23:50.787 回答
1

详细说明 Szymon 的答案。

第 1 步删除内部类PlaceHolderFragment 第 2 步:创建您的片段类,并具有相应的布局。例如,ImageViewFragment(当然扩展了 Fragment )然后image_view_fragment_layout. Step 3 HaveImageViewFragmentonCreateView方法inflate image_view_fragment_layout

public ImageViewFragment extends Fragment {
  ...
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.image_view_fragment_layout, 
                                                    container, false);
    return rootView;
  }

按照这些步骤添加尽可能多的选项卡。

然后在getItem()方法中遵循 Szymon 的回答。请记住,您要首先出现的选项卡将位于位置 0,依此类推。

另一件重要的事情,在您的 getCount 方法中:

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

返回的数字应该反映您添加的选项卡的数量。

于 2015-06-10T11:21:13.350 回答