简体   繁体   English

使用JSON创建动态操作栏选项卡

[英]Creating dynamic action bar Tabs using JSON

I have this JSON Config. 我有这个JSON配置。 Coming from server: 来自服务器:

     {"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"}
        ]
     }

Now to set tabs for this layout i have created this class : 现在为该布局设置选项卡,我创建了此类:

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


}

And Tabview Adapter: 和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());
    }





}

Here is my issue: How can i load different fragment for JSON where "st" = "picture" to load picture or "st" = "web" to load website with given url inside each tabs that i have created using json? 这是我的问题:如何为JSON加载不同的片段,其中"st" = "picture"以加载图片,或"st" = "web"以使用给定的url加载网站,这些标签在我使用json创建的每个选项卡中?

In which part of this two class i should be looking up json values from objects and placing those fragments on View or should i be creating new class? 我应该在这两个类的哪一部分中从对象中查找json值并将这些片段放在View上,还是应该创建新类?

(I do not need help with any json parsing) (我不需要任何json解析的帮助)

In TabviewAdapter class, 在TabviewAdapter类中,

public Fragment getItem(int position) 

this method will return the fragment for each page, with the position parameter, you can access the value in json. 此方法将返回每个页面的片段,带有position参数,您可以访问json中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM