0

这是一个多点点击活动>>我正在尝试为每个选项卡设置一个布局,但它不起作用!它在两个选项卡中都没有显示任何内容!

这是代码

 public class Game extends Activity { 

    public static Context appContext;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        //ActionBar gets initiated
        ActionBar actionbar = getActionBar();
       //Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
       //initiating both tabs and set text to it.
        ActionBar.Tab roundTab = actionbar.newTab().setText("Round 55");
        ActionBar.Tab scoreTab = actionbar.newTab().setText("Score 55");

      //create the two fragments we want to use for display content
        Fragment roundFragment = new roundFragment();
        Fragment scoreFragment = new scoreFragment();

     //set the Tab listener. Now we can listen for clicks.
        roundTab.setTabListener(new MyTabsListener(roundFragment));
        scoreTab.setTabListener(new MyTabsListener(scoreFragment));
      //add the two tabs to the actionbar
        actionbar.addTab(roundTab);
        actionbar.addTab(scoreTab);
    }



    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment;
        }


        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {

        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

    }

    public class roundFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
             // Inflate the layout for this fragment
            return inflater.inflate(R.layout.round, container, false);
        }

    }

    public class scoreFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
             // Inflate the layout for this fragment
            return inflater.inflate(R.layout.score, container, false);
        }

    }


 }
4

2 回答 2

0

onTabSelected()的是空的。这就是为什么它在选择时没有显示任何内容。

将此代码添加到您onTabSelected()MyTabsListener.

 public void onTabSelected(Tab tab, FragmentTransaction ft) {
           ft.add([id the container /viewgroup in which you have to load your     fragment],fragment)
        }
于 2013-07-31T13:53:28.170 回答
-1

这不是这样做的方法,而且似乎过于复杂......对于带有选项卡的 UI,我建议您使用ViewPagerIndicator库,它众所周知,可靠且易于使用。

于 2013-07-31T12:12:32.393 回答