如何在一个 Activity 中使用两个标签栏(和查看寻呼机)?

How to use two tab bars (and view pager) in one Activity?

我将这个库用于标签栏:link 根据图片我使用它并且它有效但有一个问题我应该在另一个视图寻呼机中使用视图寻呼机并且它会产生一些问题,例如你在图片中看到的。 我该如何解决?

A Screen shot from my app and the problem

编辑:这是我的代码

MainActivity.js


public class MainActivity extends AppCompatActivity {

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


    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getSupportFragmentManager(), FragmentPagerItems.with(this)
            .add("1111", PageFragment.class)
            .add("222", PageFragment.class)
            .add("33333", PageFragment.class)
            .add("444", PageFragment.class)
            .create());

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(adapter);

    SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
    viewPagerTab.setViewPager(viewPager);
}

}

此代码用于第一个标签栏

PageFragment.js

public class PageFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_demo, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int position = FragmentPagerItem.getPosition(getArguments());
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getActivity().getSupportFragmentManager(), FragmentPagerItems.with(getActivity())
                .add("One", TestFragment.class)
                .add("Two", TestFragment.class)
                .add("Three", TestFragment.class)
                .add("Four", TestFragment.class)
                .add("Five", TestFragment.class)
                .add("Six", TestFragment.class)

                .create());

        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager2);
        viewPager.setAdapter(adapter);

        SmartTabLayout viewPagerTab = (SmartTabLayout) view.findViewById(R.id.viewpagertab2);
        viewPagerTab.setViewPager(viewPager);
    }
}

这里是第二个标签栏片段的代码

TestFragment.js

public class TestFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

您可能会被否决,因为您没有努力自行解决这个问题,或者您没有自己的发现!

但是,我最近实施了这个并且对我的情况来说效果很好。 它在一个 activity.

中有两个选项卡(作为两个片段)

代码如下。

主要活动:

public class MainActivity extends AppCompatActivity {

TabLayout.Tab Tab1,Tab2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    Tab1 = tabLayout.newTab().setText("Monthly Expenses");
    Tab2 = tabLayout.newTab().setText("Daily Expenses");

    tabLayout.addTab(Tab1);
    tabLayout.addTab(Tab2);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });
}
}

PagerAdapter.java :

public class PagerAdapter extends FragmentStatePagerAdapter
{
int NumOfTabs;
public PagerAdapter (FragmentManager fm, int NumberOfTabs)
{
    super(fm);
    this.NumOfTabs = NumberOfTabs;
}

@Override
public Fragment getItem(int position)
{
    switch (position)
    {
        case 0:
            TabFragment1 tab1 = new TabFragment1();
            return tab1;

        case 1:
            TabFragment2 tab2 = new TabFragment2();
            return tab2;

        default:
            return null;
    }
}

public int getCount()
{
    return NumOfTabs;
}
}

TabFragment1.java :

public class TabFragment1 extends Fragment{
FloatingActionButton fab;
MyBaseAdapter baseAdapter;
static ArrayList<ListRowItem> listitem = new ArrayList<>();
ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.tab_fragment_1, container, false);
}
}

TabFragment2.java :

public class TabFragment2 extends Fragment{
FloatingActionButton fab;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.tab_fragment_2, container, false);
}
}

希望这就是您要找的!

使用getChildFragmentManager()class代替

解决了问题
 getActivity().getSupportFragmentManager() 

PageFragment.js文件中