如何在android中的Fragment中添加导航视图?

时间:2016-04-28 07:55:20

标签: android

我有一个名为DealMailListig的片段并且有主要活动。我正在尝试使用抽屉切换图标在我的片段中添加导航视图而不是主活动。但是不要成功.pls指导我。

任何帮助都会受到赞赏。

这是我的Deal MainListing代码 : -

public class CDealMainListing extends Fragment {
// --Commented out by Inspection (11-04-2016 10:45):public LinearLayout m_MainLayout;
private ViewPager m_ViewPager;// declare view pager variable
private View m_Main;// declare View Main variable

@SuppressWarnings("ConstantConditions")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    m_Main = inflater.inflate(R.layout.deals_main_screen, container, false);
    //noinspection ConstantConditions
    ((AppCompatActivity) getActivity()).getSupportActionBar().show(); // show tool bar
    init();// initlize controls
    return m_Main;
}

private void init() {
    TabLayout m_TabLayout = (TabLayout) m_Main.findViewById(R.id.tab_layout);// finding Id of tablayout
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Deals"));// add deal listin tab
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Stories"));// add stories tab
    m_TabLayout.setTabGravity(TabLayout.GRAVITY_FILL);// setting Gravity of Tab

    m_ViewPager = (ViewPager) m_Main.findViewById(R.id.pager);//finding Id of ViewPager
    CDealMainListingPager m_oDealMainScreenPager = new CDealMainListingPager
            (getActivity().getSupportFragmentManager(), m_TabLayout.getTabCount());
    m_ViewPager.setAdapter(m_oDealMainScreenPager);// adiing adapter to ViewPager
    m_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(m_TabLayout));// performing action of page changing
    m_TabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            m_ViewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

我认为你必须创建一个活动,在这个活动中你必须创建导航视图和标签布局。然后你需要一个适配器(ViewPagerAdapter)并调用片段管理器,如下所示:

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

然后你必须像这样添加片段到你的适配器:

adapter.addFragment(new WhateverFragment(), 'title');
viewPager.setAdapter(adapter);

(视图寻呼机在之前声明)

然后你必须使用viewpager设置tabLayout:

tabLayout.setupWithViewPager(viewPager);

您可以尝试这个并随意询问更多细节:)