显示不同片段的选项菜单(主要在邻居片段中)

时间:2016-07-12 11:41:54

标签: android android-fragments android-optionsmenu fragmentmanager

我的查看寻呼机的适配器就像:

 model.CloseViewAction = new Action(view.Close);

我如何在java类中设置tablayout和viewpager:

    public Pager(FragmentManager fm, int tabCount) {
        super(fm);
        this.tabCount = tabCount;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new frag1();
            case 1:
                return new frag2();
            case 2:
                return new frag1();
            case 3:
                return new frag2();
            case 4:
                return new frag1();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return tabCount;
    }
}

我得到奇怪的错误看起来像这样:

 for (int i = 0; i < 5; i++) {
            tabLayout.addTab(tabLayout.newTab());
        }

    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.beginFakeDrag();
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

那我怎么解决这个问题呢? 我从堆栈中得到答案,但它没有解决我的问题, 所以任何人都对此有任何了解,请帮助我!

3 个答案:

答案 0 :(得分:0)

将此行viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));替换为tabLayout.setTabsFromPagerAdapter(adapter);

答案 1 :(得分:0)

尝试将=LOOKUP(B2, SheetA!B2:B160,SheetA!B2) tabLayoutviewPager连接,而不是在循环中手动添加标签。

答案 2 :(得分:0)

available_bus_fragment.xml

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabGravity="fill"
    android:background="#d4d4d4"
    app:tabMode="fixed" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e8e8e8"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

ViewPagerAdapter.java是ViewPager的适配器。

public class ViewPagerAdapter extends FragmentStatePagerAdapter  {


public ViewPagerAdapter(FragmentManager manager) {
    super(manager);
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

@Override
public int getCount() {
    return 3;
}


@Override
public CharSequence getPageTitle(int position) {
    return  String.valueOf(position);
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            Fragment tab1 = new YourFragment1();
            return tab1;
        case 1:
            Fragment tab2 = new YourFragment1();
            return tab2;
        case 2:
            Fragment tab3 = new YourFragment1();
            return tab3;
    }
    return null;
}}

这是您设置Viewpager适配器的类。

public class MyFragment extends Fragment {
View rootView;
ViewPagerAdapter adapter;
TabLayout tabLayout;
ViewPager viewPager;
private int[] tabIcons = {
        R.drawable.tabclock,
        R.drawable.clock,
        R.drawable.tab_rupee
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.available_bus_fragment, null);
    tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);


    viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(getChildFragmentManager());

    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabsFromPagerAdapter(adapter);
    setupTabIcons();

    return rootView;
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]).setTag("Departure").setText("Departure");
    tabLayout.getTabAt(1).setIcon(tabIcons[1]).setTag("Duration").setText("Duration");
    tabLayout.getTabAt(2).setIcon(tabIcons[2]).setTag("Price").setText("Price");
} }