我将如何实现片段在Android上通用的ArrayList? [英] How would i implement a Generic ArrayList of Fragments in Android?

查看:151
本文介绍了我将如何实现片段在Android上通用的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想实现使用我所有的片段类来创建一个数组列表℃的方法;?扩展片断> 在我的 MainActivity.java 类,这是一个的ArrayList 不同的对象延长片段类。

为什么我这样做的主要原因是加载使用 android.support.v4.jar 支持库 ViewPager 视图。

下面是我到目前为止,但我不能添加不同的对象扩展片段进入我的的ArrayList

 公开名单&LT ;?延伸片断> getFragments(){
    名单,LT ;?延伸片断> ipsumFragments =新的ArrayList<片断>();
    ipsumFragments.add(BaconIpsumFragment.newInstance());
    ipsumFragments.add(BuseyIpsumFragment.newInstance());
    ipsumFragments.add(LoremIpsumFragment.newInstance());    返回ipsumFragments;
}

我只是想把我的头周围采用ViewPager和多个fragments.If有人可以帮助我,我将不胜AP preciate它。

这是我的第一个片段类,其他都一模一样,但命名不同,使用不同的 XML 布局

 公共类BaconIpsumFragment扩展片段{公共静态最终BaconIpsumFragment的newInstance(){    BaconIpsumFragment baconFragment =新BaconIpsumFragment();
    捆绑ARGS =新包();
    baconFragment.setArguments(参数);
    返回baconFragment;
}@覆盖
公共无效onAttach(活动活动){
    //试图将附加到父活动
    super.onAttach(活动);
}//当父活动产生的片段调用
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    //执行默认行为
    super.onCreate(savedInstanceState);
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    super.onCreateView(充气器,容器,savedInstanceState);
    返回inflater.inflate(R.layout.bacon_fragment,集装箱,FALSE);
}@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
    //最后试图改变任何变量
    super.onActivityCreated(savedInstanceState);
}}

和全面实施我的活动。尝试添加一个片段的实例时我的错误是在getFragments发生()方法。

 公共类MainActivity扩展FragmentActivity {民营动作条tabbedActionBar = NULL;
私有String [] ipsumsArray = NULL;
私人资源资源= NULL;
私人FragmentManager经理= NULL;
私人FragmentTransaction交易= NULL;私人列表<片断> mIpsumFragments = NULL;
私人LoremIpsumViewPager pagerAdapter = NULL;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    //获取资源对象
    资源= getResources();
    //从资源的String数组
    ipsumsArray = resources.getStringArray(R.array.ipsums_array);    //获取所有的,我们将尝试显示片段
    mIpsumFragments = getFragments();    //获取的动作条的引用
    tabbedActionBar = getActionBar();
    //设置导航模式的操作栏
    tabbedActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    的for(int i = 0; I< ipsumsArray.length;我++){
        TAB键的话= tabbedActionBar.newTab();
        tab.setText(ipsumsArray [I]);
        tab.setTabListener(tabListener);
        tabbedActionBar.addTab(标签);
    }// setDefaultTab();
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}私人TabListener tabListener =新TabListener(){    @覆盖
    公共无效onTabReselected(标签选项卡,FragmentTransaction ARG1){
        // TODO自动生成方法存根    }    @覆盖
    公共无效onTabSelected(标签选项卡,FragmentTransaction ARG1){        //获取片段经理
        经理= getFragmentManager();
        交易= manager.beginTransaction();
        INT tabSelected = tab.getPosition();
    @覆盖
    公共无效onTabUnselected(标签选项卡,FragmentTransaction ARG1){
        // TODO自动生成方法存根    }};私人列表<片断> getFragments(){
    清单<片断> ipsumFragments =新的ArrayList<片断>();    ipsumFragments.add(BaconIpsumFragment.newInstance());
    ipsumFragments.add(BuseyIpsumFragment.newInstance());
    ipsumFragments.add(LoremIpsumFragment.newInstance());    返回ipsumFragments;
}}


解决方案

有没有必要使用通配符的。刚刚宣布的名单:

 列表<片断> ipsumFragments =新的ArrayList<片断>();

这不会限制你在列表中添加片段刚刚实例。您将可以在其加入片段的子类的实例了。然后你的方法的返回类型更改为列表<片断>

使用声明一个列表,名单,LT的;?扩展片断> 是可以分配的参考,片段的任何子类的列表。所以,你可以指定列表< BaconIpsumFragment> 列表< LoremIpsumFragment> ,仅此而已。这就是为什么你不能添加任何子类的实例在这样的名单,因为你不知道你有真正的下方列表什么做的原因。所以,你可能会尝试添加 BaconIpsumFragment 列表< LoremIpsumFragment方式> ,这肯定是不正确的

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.

The primary reason why I am doing this is to load fragments using the android.support.v4.jar support library ViewPager view.

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;
}

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.

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);
}

}

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>();

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>.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆