2013-07-17 67 views
0

我有這個JSON配置。從服務器來:使用JSON創建動態操作欄選項卡

TabView的:

public class TabView extends FragmentActivity implements ActionBar.TabListener { 



    //Class Vars 
    ViewPager dViewPager; 
    TabviewAdapter dAdapter; 
    private ArrayList<JsonToDataGen> dSource = new ArrayList<JsonToDataGen>(); 


    //Class Vars: 
    FileOperation fo = new FileOperation(); 
    PluginManager pm = new PluginManager(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //** MUST RUN BELOW THREAD BEFOR ANY THING ELSE STARTS **// 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       JsonToDataGenParser parser = new JsonToDataGenParser(fo.load(getApplicationContext(), "config.json")); 
       dSource = parser.getSource(); 
       Message msg = dHandler.obtainMessage(); 
       dHandler.sendMessage(msg); 
      } 
     }).start(); 


     setContentView(R.layout.activity_tab_view); 


     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 


     dAdapter = new TabviewAdapter(getSupportFragmentManager(), dSource); 

     // Set up the ViewPager with the sections adapter. 
     dViewPager = (ViewPager) findViewById(R.id.pager); 
     dViewPager.setAdapter(dAdapter); 


     //Get Titles and Fragments 
     for (int i = 0; i < dAdapter.getCount(); i++) { 
      actionBar.addTab(actionBar.newTab() 
        .setText(dAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 

     //Swipe-effect 
     dViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 



    }//End onCreate 


    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     dViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 

    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) { 

    } 

    //Layout view Handler 
    private Handler dHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      dAdapter.setObjects(dSource); 
      dAdapter.notifyDataSetChanged(); 
     } 
    }; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     return false; 
    } 


} 

而且TabView的適配器:

public class TabviewAdapter extends FragmentPagerAdapter{ 


    //Class Ref. 
    PluginManager pm = new PluginManager(); 

    //Local Objects 
    String clickedObjectRowJson = null; 
    private ArrayList<JsonToDataGen> dObject; 




    public TabviewAdapter(FragmentManager fm, ArrayList<JsonToDataGen> dObject) { 
     super(fm); 
     this.dObject = dObject; 
    } 

    public void setObjects (ArrayList<JsonToDataGen> dObject){ 
     this.dObject = dObject; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     JsonToDataGen item = dObject.get(position); 
     switch (position) { 
     case 0: 
      return item.getT().toUpperCase(l); 
     case 1: 
      return item.getT().toUpperCase(l); 
     case 2: 
      return item.getT().toUpperCase(l); 
     case 3: 
      return item.getT().toUpperCase(l); 
     case 4: 
      return item.getT().toUpperCase(l); 
     } 
     return null; 
    } 


    @Override 
    public Fragment getItem(int position) { 

     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position); 
     args.putString("clikedObject",clickedObjectRowJson); 
     fragment.setArguments(args); 
     return fragment;  
    } 


    @Override 
    public int getCount() { 
     Log.i("TAB ICON ------","TabICON Object Size"+dObject.size()); 
     return (this.dObject.size()); 
    } 





} 

這裏是

 {"t":"AppName", 
     "tabs":[ 
     {"tbico":"10-medical.png","t":"Twitter","st":"web", "url":"http://www.google.com"}, 
     {"tbico":"101-gameplan.png","t":"GoogleMaps","st":"doc", "file":"abountus.pdf"}, 
     {"tbico":"113-navigation.png", "t":"FacebookPhotos", "st":"picture", "file":"me.png"}, 
     {"tbico":"113-navigation.png", "t":"FacebookPhotos", "st":"picture", "file":"me.png"}, 
     {"tbico":"113-navigation.png", "t":"FacebookPhotos", "st":"picture", "file":"me.png"} 
     ] 
    } 

我們此佈局我創造了這個類設置選項卡我的問題:我如何加載不同的JSON片段,其中"st" = "picture"加載圖片或"st" = "web"在我使用json創建的每個選項卡內加載具有給定url的網站?

這兩個類的哪一部分我應該從對象中查找json值並將這些片段放在視圖上,還是應該創建新類?

(我不需要任何JSON解析幫助)

回答

0

在TabviewAdapter類,

public Fragment getItem(int position) 

這種方法將與位置參數返回片段的每一頁, ,你可以訪問json中的值。

+0

所以你的意思是從這裏加載不同的片段?怎麼樣? :) – star18bit

+0

通過使用「JsonToDataGen item = dObject.get(position)」,您可以獲取json,然後根據此值從項目獲取「st」值,然後實例化您想要的片段。 – kvh