因此,我正在尝试实现一种方法,该方法使用我的所有类在我的类Fragment
中创建一个具有扩展类的不同对象的方法。Array List<? extends Fragment>
MainActivity.java
ArrayList
Fragment
我这样做的主要原因是使用android.support.v4.jar
支持库ViewPager
视图加载片段。
这是我到目前为止所拥有的,但我无法将不同的对象添加extend Fragment
到我的ArrayList
public List<? extends Fragment> getFragments(){
List<? extends Fragment> ipsumFragments = new ArrayList<Fragment>();
ipsumFragments.add(BaconIpsumFragment.newInstance());
ipsumFragments.add(BuseyIpsumFragment.newInstance());
ipsumFragments.add(LoremIpsumFragment.newInstance());
return ipsumFragments;
}
我只是想了解一下使用 ViewPager 和多个片段。如果有人可以帮助我,我将不胜感激。
这是我的第一个类Fragment
,其他类完全相同,但名称不同,使用不同的XML
布局
public class BaconIpsumFragment extends Fragment{
public static final BaconIpsumFragment newInstance(){
BaconIpsumFragment baconFragment = new BaconIpsumFragment();
Bundle args = new Bundle();
baconFragment.setArguments(args);
return baconFragment;
}
@Override
public void onAttach(Activity activity){
// Attempt to attach to the parent activity
super.onAttach(activity);
}
// Called when the parent activity creates the fragment
@Override
public void onCreate(Bundle savedInstanceState){
// Perform the default behavior
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.bacon_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
// Last attempt to change any variables
super.onActivityCreated(savedInstanceState);
}
}
并全面执行我的活动。尝试添加片段实例时,我的错误发生在 getFragments() 方法中。
public class MainActivity extends FragmentActivity {
private ActionBar tabbedActionBar = null;
private String [] ipsumsArray = null;
private Resources resources = null;
private FragmentManager manager = null;
private FragmentTransaction transaction = null;
private List<Fragment> mIpsumFragments = null;
private LoremIpsumViewPager pagerAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the resources object
resources = getResources();
// get the String array from resources
ipsumsArray = resources.getStringArray(R.array.ipsums_array);
// Get all the fragments that we will attempt to display
mIpsumFragments = getFragments();
// get a reference to the action bar
tabbedActionBar = getActionBar();
// set the navigation mode for the action bar
tabbedActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 0; i < ipsumsArray.length; i++){
Tab tab = tabbedActionBar.newTab();
tab.setText(ipsumsArray[i]);
tab.setTabListener(tabListener);
tabbedActionBar.addTab(tab);
}
// setDefaultTab();
}
@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;
}
private TabListener tabListener = new TabListener(){
@Override
public void onTabReselected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
// Get the fragment manager
manager = getFragmentManager();
transaction = manager.beginTransaction();
int tabSelected = tab.getPosition();
@Override
public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
};
private List<Fragment> getFragments(){
List<Fragment> ipsumFragments = new ArrayList<Fragment>();
ipsumFragments.add(BaconIpsumFragment.newInstance());
ipsumFragments.add(BuseyIpsumFragment.newInstance());
ipsumFragments.add(LoremIpsumFragment.newInstance());
return ipsumFragments;
}
}