2016-02-23 59 views
0

我有在ViewPagerAdapter類與Tablayout工作的getItem() method一個問題。我有多個傳呼機,我想基於當前位置變量動態實例化新類(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; 
    } 
} 

我怎樣才能返回使用Class.forName(stringName)Fragment對象?我曾嘗試以下,但我不知道怎麼投的對象背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

剛剛閱讀這篇簡單的文章,您將瞭解如何根據位置和getItem返回來創建新的片段。 [使用選項卡創建滑動視圖](http://developer.android.com/training/implementing-navigation/lateral.html) – Vivart

回答

1

爲了實際創建類的實例,您需要在您的類上調用newInstance。例如,您的代碼可以改爲如下所示:

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