如何在另一个片段中包含另一个Tab ViewPager?

时间:2017-01-13 03:07:49

标签: android android-fragments android-viewpager

所以我在导航抽屉里面的片段上成功实现了一个Tab查看器。现在我的问题是,如何在单个PagerAdapter中的另一个单独片段中包含另一组选项卡(使用此分页器:您可以在tabhost不执行的任何选项卡上滑动)。

继承我的“其他”片段的代码:

AttendeesFragment.java:

package com.example.guitarista.citem;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class AttendeesFragment extends Fragment {

    public AttendeesFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getActivity().setTitle("Attendees");
        View v = inflater.inflate(R.layout.fragment_attendees, container, false);

        TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("All"));
        tabLayout.addTab(tabLayout.newTab().setText("VIP"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); //check gravity center

        final ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getFragmentManager(), 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) {

            }
        });

        return v;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return  true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return  true;
        }
        return  super.onOptionsItemSelected(item);
    }



}

这是 PagerAdapter.java

的代码
package com.example.guitarista.citem;

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

/**
 * Created by smdojt on 1/11/2017.
 */

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

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                TabInternationalFragment tab1 = new TabInternationalFragment();
                return tab1;
            case 1:
                TabLocalFragment tab2 = new TabLocalFragment();
                return tab2;
            default:
                return null;
        }
    }

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

请注意,公共片段中已有选项卡(即TabsInternationalFragment,TabsLocalFragment),现在我想在其他片段上添加其他选项卡(即AllTab,VIPTab)

1 个答案:

答案 0 :(得分:0)

这次你必须创建一个不同的PagerAdapter,我认为这只是一个临时解决方案。