我想在我的应用中使用JakeWharton库。我对样本中的示例SampleTabsDefault感兴趣: 现在,我卡住了。在这个示例中只更改了Text,我想更改片段(每个片段都有自己的布局,每个布局都有不同的控件,如按钮,复选框等) 我想我必须在FragmentPagerAdapter的类扩展中改变这个方法:
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
我想根据特定位置创建新片段(具有自己的布局)。
我已经实现了这段代码:
public class MainActivity extends FragmentActivity {
private static final String[] CONTENT = new String[] {"My Profile", "Statistics"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
class GoogleMusicAdapter extends FragmentPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int position) {
if (position==0)
return MyProfile.newInstance(CONTENT[position % CONTENT.length]);
else if (position==1)
return Statistics.newInstance(CONTENT[position % CONTENT.length]);
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override
public int getCount() {
return CONTENT.length;
}
}
}
和
public class MyProfile extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
public static MyProfile newInstance(String content) {
MyProfile fragment = new MyProfile();
fragment.mContent = content;
return fragment;
}
private String mContent = "???";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_basic, container, false);
Button button = (Button) view.findViewById(R.id.fragment_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity, "fragmen1", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
和此:
public class Statistics extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
public static Statistics newInstance(String content) {
Statistics fragment = new Statistics();
fragment.mContent = content;
return fragment;
}
private String mContent = "???";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_basic2, container, false);
Button button = (Button) view.findViewById(R.id.fragment_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity, "fragmen2", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
它不起作用...我无法切换页面,我的片段布局不会显示...
答案 0 :(得分:2)
在getItem()
内的FragmentPagerAdapter
方法中,您可以根据位置返回其他片段。例如:
public Fragment getItem(int position) {
switch(position)
{
case 0:
TestFragment fragment = new TestFragment();
return fragment;
case 1:
TestFragment2 fragment2 = new TestFragment2();
return fragment2;
}
DefaultFragment fragment3 = new DefaultFragment();
return fragment3;
}