对Android中的每个碎片选项卡使用单独的意图
我正在尝试使用fragment创建具有滑动视图的选项卡。我找到了一个解决方案,但它为每个选项卡提供了一个单一的虚拟文本布局,而不是使用单独的布局。我想用三种不同的布局创建三个不同的类,并在选项卡中使用它们,如果可能,然后为每个选项卡使用自定义背景(选中/未选中)。我可以不用碎片来做所有这些事情,但使用碎片似乎很有挑战性对Android中的每个碎片选项卡使用单独的意图,android,android-fragments,android-tabs,Android,Android Fragments,Android Tabs,我正在尝试使用fragment创建具有滑动视图的选项卡。我找到了一个解决方案,但它为每个选项卡提供了一个单一的虚拟文本布局,而不是使用单独的布局。我想用三种不同的布局创建三个不同的类,并在选项卡中使用它们,如果可能,然后为每个选项卡使用自定义背景(选中/未选中)。我可以不用碎片来做所有这些事情,但使用碎片似乎很有挑战性 public class MainActivity extends FragmentActivity { SectionsPagerAdapter mSectionsP
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
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;
}
@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;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
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;
}
}
}
在以下链接中,可以帮助您在指定的意图中创建选项卡片段:
要实现您的目标,您必须使用ActionBarSherlock库,要调用其他片段,您不需要使用Intent 几个月前,我编写了同样的程序来调用我在下面代码中使用的不同片段
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
要为每个片段显示不同的布局,您应该调用不同的片段,还需要编写XML
FragmentTab1.java:-
public class FragmentTab1 extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
fragment1.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Your Layout Goes Here for Fragment1" />
</RelativeLayout>
谢谢你解释清楚!这真的很有帮助!还有一个问题,我可以自定义标签背景吗@吉隆坡埃米特酒店Suri@ProkashSarkar你的欢迎:)上帝保佑,是的,我建议你使用图像按钮,使您的标签智能
http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/