安卓:"导航类型:固定标签+刷卡" [英] Android: "Navigation Type: Fixed Tabs + Swipe"
问题描述
我试图使用标签+刷卡在一个应用程序,并希望使用导航类型固定标签+刷卡创建活动时,该ADT提供了我。
现在的sooo的ADT吐出来很好code,我稍微修改
我完全理解了code和正在发生的事情......但我怎么能教应用程序使用愚蠢的假破片我的三个片段呢? :(
我找不到它处理了ADT的导航类型的任何教程...
感谢您的帮助!
公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//设置操作栏。
最后的动作条动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//创建将返回一个片段为三个的适配器
//应用程序的主要部分。
mSectionsPagerAdapter =新SectionsPagerAdapter(getSupportFragmentManager());
//设置的ViewPager与部分适配器。
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//当不同部分之间的刷卡,选择相应的
//标签。我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有
//引用的标签。
mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效onPageSelected(INT位置){
actionBar.setSelectedNavigationItem(位置);
}
});
//添加标签
actionBar.addTab(actionBar.newTab()的setText(标签1)setTabListener(本)。);
actionBar.addTab(actionBar.newTab()的setText(标签2)setTabListener(本)。);
actionBar.addTab(actionBar.newTab()的setText(标签3)setTabListener(本)。);
}
@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
//当选择给定的标签,切换到对应页面
//该ViewPager。
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
}
@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
}
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {
公共SectionsPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(INT位置){
//的getItem被称为实例化片段给定的页面。
//返回一个DummySectionFragment(定义为静态内部类
下图)与页面数量作为其唯一的参数//。
片段片段=新DummySectionFragment();
捆绑的args =新包();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,位置+ 1);
fragment.setArguments(参数);
返回片段;
}
@覆盖
公众诠释getCount将(){
//显示3总页数。
返回3;
}
}
公共静态类DummySectionFragment扩展片段{
/ **
*片段参数重新presenting本章节号
* 分段。
* /
公共静态最后弦乐ARG_SECTION_NUMBER =section_number标;
公共DummySectionFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main_dummy,集装箱,假);
TextView的dummyTextView =(TextView中)rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments()调用getInt(ARG_SECTION_NUMBER)));
返回rootView;
}
}
}
但我怎么能教应用程序使用愚蠢的假破片我的三个片段呢?
您会注意到 DummySectionFragment
在的getItem引用()
的 SectionsPagerAdapter
:
@覆盖
公共片段的getItem(INT位置){
//的getItem被称为实例化片段给定的页面。
//返回一个DummySectionFragment(定义为静态内部类
下图)与页面数量作为其唯一的参数//。
片段片段=新DummySectionFragment();
捆绑的args =新包();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,位置+ 1);
fragment.setArguments(参数);
返回片段;
}
如果您要使用不同的片段,修改的getItem()
返回你想要的片段,由于所提供的位置
(0为基础的页码)。
I'm trying to use Tabs + Swipe in an App and want to use the Navigation Type "Fixed Tabs + Swipe" which the ADT provides me when creating an Activity.
Sooo now the ADT spits out nice Code, which I slightly modified...
I completely understand the code and what's going on... But how can I teach the App to use my three Fragments instead of the stupid Dummy Frag? :(
I cannot find any tutorial which deals with the ADTs "Navigation Types"...
Thanks for your help!
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
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);
}
});
//Adding Tabs
actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab 3").setTabListener(this));
}
@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) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
But how can I teach the App to use my three Fragments instead of the stupid Dummy Frag?
You will notice that DummySectionFragment
is referenced in getItem()
of the SectionsPagerAdapter
:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
If you want to use different fragments, modify getItem()
to return the fragment you want, given the supplied position
(0-based page number).
这篇关于安卓:"导航类型:固定标签+刷卡"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!