获取选项卡式活动中的当前选项卡的编号

时间:2018-05-10 11:25:47

标签: android android-tabbed-activity

首先,我是Android开发新手,感谢您的帮助。

我在android studio中有一个标签式活动,我有一个FloatingActionButton在标签式活动的每个片段中添加一个Item。然后,此按钮调用一个新的Activty,我想将选项卡号传递给新活动,以便我可以将其存储在数据库中。

如何获取生成新活动的标签号?

以下是代码:

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


      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);

      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

      mViewPager.addOnPageChangeListener(new 
     TabLayout.TabLayoutOnPageChangeListener(tabLayout));
     tabLayout.addOnTabSelectedListener(new 
     TabLayout.ViewPagerOnTabSelectedListener(mViewPager));


      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
            startNewPostActivity.putExtra("Fragment_Position", position_fragment);   

            startActivity(startNewPostActivity);
        }
    });



}

为了获得这个职位我正在做以下事情: 设置position_fragment = position; 但是这会返回错误的标签号。

公共类SectionsPagerAdapter扩展了FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
         position_fragment = position;  // not good
        return PlaceholderFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        // Show 5 total pages.
        return 5;
    }
}

}

3 个答案:

答案 0 :(得分:2)

试试这个:

{{1}}

答案 1 :(得分:2)

您可以从这些开关案例中识别您的标签ID,并在

中调用必要的功能
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
                switch (tab.getPosition()) {
                    case 0:
                        //Log.e("TAG","TAB1");
                        break;
                    case 1:
                        //Log.e("TAG","TAB2");
                        break;
                    case 2:
                        //Log.e("TAG","TAB3");
                        break;
                    case 3:
                        //Log.e("TAG","TAB4");
                        break;
                    case 4:
                        //Log.e("TAG","TAB5");
                        break;
                    case 5:
                        //Log.e("TAG","TAB6");
                        break;
                    case 6:
                        //Log.e("TAG","TAB7");
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

答案 2 :(得分:1)

有两种方法可以获取选项卡式活动中当前选项卡的编号

1。使用ViewPager.getCurrentItem()

  

返回在空闲状态下视图层次结构中将保留到当前页面任一侧的页面数。默认为1。

示例代码

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Intent startNewPostActivity = new Intent(getApplicationContext(), NewPostActivity.class);
        startNewPostActivity.putExtra("Fragment_Position", mViewPager.getCurrentItem());   

        startActivity(startNewPostActivity);
    }

2。使用TabLayout.addOnTabSelectedListener()

  

添加TabLayout.OnTabSelectedListener,当选项卡选择更改时将调用该TabLayout.OnTabSelectedListener。   样本代码

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });