我想在一个类中有一个带有4个可滚动选项卡的选项卡。我实现了以下示例,它可以工作:
mAdapter = new MyAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
...
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS; //NUM_ITEMS=4
}
@Override
public ArrayListFragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0:
return "TAB1";
case 1:
return "TAB2";
case 2:
return "TAB3";
case 3:
return "TAB4";
default:
return "ERROR";
}
}
}
...
public static class ArrayListFragment extends ListFragment {
int mNum;
static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
switch(mNum) {
case 0:
View v0 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v0;
case 1:
View v1 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v1;
case 2:
View v2 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v2;
case 3:
View v3 = inflater.inflate(R.layout.fragment_pager_list, container, false);
return v3;
default:
View vd = inflater.inflate(R.layout.fragment_pager_list, container, false);
return vd;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Titles));
}
//Titles={"TAB1", "TAB2", "TAB3", "TAB4"}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
我在每个视图中放置的fragment_pager_list布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb">
<TextView android:id="@+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HELLO WORLD"/>
<!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it -->
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="No items."/>
</FrameLayout>
问题是我想在每个选项卡的每个视图中放置不同的布局。 我试图修改代码,以便“ArrayListFragment”继承“Fragment”而不是“ListFragment”来加载不包含“ListView”但“RelativeLayout”或“LinearLayout”的布局,但无法使其工作。
如果有人可以帮助我,我需要在选项卡上加载不同的视图,并且它们包含除ListView之外的其他元素。
非常感谢。
答案 0 :(得分:3)
你最好为不同的布局制作每个片段。
您可以将addFragment(Fragment fragment)
方法添加到MyAdapter
。
mAdapter = new MyAdapter(getSupportFragmentManager());
for(int i=0; i<4; i++) {
Fragment fragment = null;
switch(i) {
case 0:
fragment = FirstFragment.newInstance();
break;
case 1:
fragment = SecondFragment.newInstance();
break;
case 2:
fragment = ThirdFragment.newInstance();
break;
case 3:
fragment = FourthFragment.newInstance();
break;
}
mAdapter.addFragment(fragment);
}
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
MyAdapter:适配器是一个简单的容器。
public static class MyAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList;
public MyAdapter(FragmentManager fm) {
super(fm);
mFragmentList = new ArrayList<Fragment>();
}
publid void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return ((CustomFragment) mFragmentList.get(position)).getPageTitle();
}
}
CustomFragment:为每个片段添加常用方法。
public abstract class CustomFragment extends Fragment {
public abstract Fragment newInstance();
public abstract String getPageTitle();
}
FirstFragment:为每个片段增加不同的布局。
public class FirstFragment extends CustomFragment {
@Override
public Fragment newInstance() {
Fragment f = new FirstFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.LAYOUT_ID_FOR_FIRST_FRAGMENT, container, false);
// Some codes for layout such as findViewById
return view;
}
@Override
public String getPageTitle() {
// return TITLE_FOR_FRAGMENT
}
}
我希望这个答案可以帮到你。