动态实例化新类并将其强制转换为Fragment类型

时间:2016-02-23 15:21:27

标签: java android

我在使用getItem() method的ViewPagerAdapter类中的Tablayout时遇到问题。我有多个寻呼机,我想根据当前位置变量动态实例化新类(Tab1,Tab2,Tab3,Tab4)。

原始方法:

@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        Tab2 tab2 = new Tab2();

        return tab2;
    }
 }

如何使用Fragment返回Class.forName(stringName)个对象?我尝试过以下操作,但我不知道如何将对象强制转换回Fragment类型:

@Override
public Fragment getItem(int position) {

    if(position == 0)
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else
    {
        String className = "Tab"+position;

        Fragment tab;
        try {

            Class cl = Class.forName(className); //?????????
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return cl;

    }
 }

全班:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
    int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created

    // Build a Constructor and assign the passed Values to appropriate values in the class
    public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
        super(fm);
        this.Titles = mTitles;
        this.NumbOfTabs = mNumbOfTabsumb;

    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {

        if(position == 0) // if the position is 0 we are returning the First tab
        {
            Tab1 tab1 = new Tab1();
            return tab1;
        }
        else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            Tab2 tab2 = new Tab2();

            return tab2;
        }
     }

    // This method return the titles for the Tabs in the Tab Strip

    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }

    // This method return the Number of tabs for the tabs Strip

    @Override
    public int getCount() {
        return NumbOfTabs;
    }
}

更新:

现在工作正常。重要的是使用完全限定名称,例如forName(com.package.Tab1)

02-23 23:44:20.198: I/System.out(2696): Tab1
02-23 23:44:20.208: I/System.out(2696): Can't3


public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {

        String className = "com.package.Tab"+position;

        try {

            System.out.println(className);

            Class cl = Class.forName(className);

            return (Fragment) cl.newInstance();


        } catch (InstantiationException e) {
            System.out.println("Can't1");

        } catch (IllegalAccessException e) {
            System.out.println("Can't2");

        } catch (ClassNotFoundException e) {
            System.out.println("Can't3");

        }

    }
    return null;
}

1 个答案:

答案 0 :(得分:1)

为了实际创建课程的实例,您需要在课堂上调用newInstance。例如,您的代码可以更改为如下所示:

Class cl = Class.forName(className);
SomeClass someClass = (SomeClass) cl.newInstance();