如何根据标签栏项启动活动?

时间:2015-07-12 02:29:41

标签: android android-tabhost android-fragmentactivity

我想根据标签栏项目的点击开始不同的活动,但我在“windows类”中得到一个空白页面作为已启动的活动。我打算做的是点击标签栏项目开始不同的活动。

标签栏活动类

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;


public class TabBar extends FragmentActivity {

    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbardefault);


        try {

            TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

            Tab = (ViewPager) findViewById(R.id.pager);
            Tab.setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {

                            actionBar = getActionBar();
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });
            Tab.setAdapter(TabAdapter);

            actionBar = getActionBar();
            //Enable Tabs on Action Bar
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        }
        catch (Exception e)
        {

        }
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                Tab.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }};


        //Add New Tab
        actionBar.addTab(actionBar.newTab().setText("Home").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Celeb").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Videos").setTabListener(tabListener));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_tab_bar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

TabPagerAdapter

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

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            //Fragement for Android Tab

            return new Android();
        case 1:
           //Fragment for Ios Tab
            return new Ios();
        case 2:
            //Fragment for Windows Tab
            return new Windows();
        }
        return null;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3; //No of Tabs
    }


    }

    public class Windows extends Fragment {
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View windows = inflater.inflate(R.layout.feed_main, container, false);
            //((TextView)windows.findViewById(R.id.textView)).setText("Windows");
            return windows;
}}

1 个答案:

答案 0 :(得分:0)

要回答你的问题,你实际上并没有启动你切换片段的活动,Fragment只是一个自定义视图,如果你想在之后启动一个活动,你可以膨胀一个xml代表自定义视图,这是片段。您的标签已被选中,然后您拨打startActivity(Intent); startActivity(new Intent(classname.this,the_class_i_want_to_start.class));

并且您的Windows类扩展Fragment它应该是Activity如果您想要的是Fragment的功能,那么您就不会遇到问题

希望其清醒