我将如何在 Android 中实现片段的通用 ArrayList? [英] How would i implement a Generic ArrayList of Fragments in Android?
问题描述
所以我试图实现一种方法,该方法使用我所有的 Fragment
类来创建一个 Array ListMainActivity.java
类中扩展 Fragment>,它是一个 ArrayList
,具有扩展 Fragment
类的不同对象.
So I am trying to implement a method that uses all of my Fragment
classes to create an Array List<? extends Fragment>
in my MainActivity.java
class which is an ArrayList
with different objects that extend the Fragment
class.
我这样做的主要原因是使用 android.support.v4.jar
支持库 ViewPager
视图加载片段.
The primary reason why I am doing this is to load fragments using the android.support.v4.jar
support library ViewPager
view.
这是我到目前为止所拥有的,但我无法将 extend Fragment
的不同对象添加到我的 ArrayList
Here is what i Have so far, but I cannot add the different Objects that extend Fragment
into my 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 和多个片段.如果有人能帮助我,我将不胜感激.
I am just trying to get my head around using a ViewPager and multiple fragments.If anyone could help me I would greatly appreciate it.
这是我的第一个 Fragment
类,其他的完全相同但命名不同并使用不同的 XML
布局
And here is my class for The first Fragment
, the others are exactly the same but named different and use different XML
layouts
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() 方法中.
And the full implementation of my activity. My error is occurring in the getFragments() method when trying to add an instance of a fragment.
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;
}
}
推荐答案
完全不需要使用通配符.只需将您的列表声明为:
There is no need to use wildcard at all. Just declare your list as:
List<Fragment> ipsumFragments = new ArrayList<Fragment>();
它不限制您只在列表中添加 Fragment
的实例.您也可以在其中添加 Fragment
子类的实例.然后将方法的返回类型更改为 List
.
It doesn't restrict you to add just the instances of Fragment
in the list. You will be able to add instances of subclasses of Fragment
in it too. And then change the return type of your method to List<Fragment>
.
将列表声明为List
是您可以分配该引用,Fragment
的任何子类的列表.所以,你可以赋值List
,List
,就是这样.这就是为什么您不能在此类列表中添加任何子类的实例的原因,因为您不知道下面实际上有什么列表.因此,您可能正在尝试将 BaconIpsumFragment
添加到 List
中,这当然是不正确的.
The use of declaring a list as List<? extends Fragment>
is that you can assign that reference, list of any subclass of Fragment
. So, you can assign List<BaconIpsumFragment>
, List<LoremIpsumFragment>
, that's it. And that is the reason why you can't add instances of any subclass in such lists, because you don't know what list do you have actually underneath. So, you might be trying to add a BaconIpsumFragment
into a List<LoremIpsumFragment>
, which is certainly not correct.
这篇关于我将如何在 Android 中实现片段的通用 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!